4

In MASM, I created a buffer variable to hold the user string input from keyboard. I am stuck on how to hold the string input into that buffer variable. I don't have any libraries linked like the irvine ones and want to do this with DOS interrupts. So far I have something along the lines of

            .model small

            .stack 100h

            .data
buff        db  25 dup(0), 10, 13
lbuff       EQU ($ - buff)              ; bytes in a string

            .code
main:
            mov ax, @data
            mov ds, ax              

            mov ah, 0Ah         ; doesn't work
            mov buff, ah        ; doesn't seem right
            int 21h                 


            mov     ax, 4000h       ; display to screen
            mov     bx, 1           
            mov     cx, lbuff           
            mov     dx, OFFSET buff     
            int     21h 

            mov ah, 4ch
            int 21h

            end main

I assume using 0Ah is correct as it is for reading array of input of buffered characters.

Jumpman
  • 145
  • 1
  • 2
  • 11
  • `int 21h/0Ah` needs the address of the buffer in `dx`. The first byte in the buffer needs to be max input. By stuffing `ah` in there, you've said you want ten characters (probably want more). After the interrupt returns, the second byte is actual number of characters entered. Input text starts at buffer + 2. – Frank Kotler Apr 08 '15 at 03:46
  • @FrankKotler Ok so I partially got it to work by doing mov dx, offset 25 mov ah, 0Ah int 21h (sorry I'm not sure how to format this in the comments). So the characters entered starts at dx + 2? How would I move that into my buff variable? – Jumpman Apr 08 '15 at 05:12
  • The input function need in the dx register the start of the buffer " mov dx, OFFSET buff" and the max input in the first byte of the buffer, example with "mov [buff], ah" for ten chars. The output function need "mov dx, OFFSET buff+2" and the actual number of characters entered "mov cl, [buff+1]". – Dirk Wolfgang Glomp Apr 08 '15 at 06:03
  • @DirkWolfgangGlomp So instead of `mov dx, OFFSET buff` it should be `mov dx, OFFSET buff+2` since the string starts 2 bytes over. And then `mov cx, lbuff` needs to become `mov cl, [buff+1]` because that will give you the correct length of the string after it's shifted +2, correct? Where then would I do the `mov [buff], ah`? Right before `mov ah, 0Ah`? – Jumpman Apr 08 '15 at 06:26

4 Answers4

11

I made some changes to your code. First, the "buff" variable needs the three level format (max number of characters allowed, another byte for the number of characteres entered, and the buffer itself) because that's what service 0AH requires. To use service 0AH I added "offset buff" (as Wolfgang said). Here it is:

            .model small

            .stack 100h

            .data

buff        db  26        ;MAX NUMBER OF CHARACTERS ALLOWED (25).
            db  ?         ;NUMBER OF CHARACTERS ENTERED BY USER.
            db  26 dup(0) ;CHARACTERS ENTERED BY USER.

            .code
main:
            mov ax, @data
            mov ds, ax              

;CAPTURE STRING FROM KEYBOARD.                                    
            mov ah, 0Ah ;SERVICE TO CAPTURE STRING FROM KEYBOARD.
            mov dx, offset buff
            int 21h                 

;CHANGE CHR(13) BY '$'.
            mov si, offset buff + 1 ;NUMBER OF CHARACTERS ENTERED.
            mov cl, [ si ] ;MOVE LENGTH TO CL.
            mov ch, 0      ;CLEAR CH TO USE CX. 
            inc cx ;TO REACH CHR(13).
            add si, cx ;NOW SI POINTS TO CHR(13).
            mov al, '$'
            mov [ si ], al ;REPLACE CHR(13) BY '$'.            

;DISPLAY STRING.                   
            mov ah, 9 ;SERVICE TO DISPLAY STRING.
            mov dx, offset buff + 2 ;MUST END WITH '$'.
            int 21h

            mov ah, 4ch
            int 21h

            end main

When 0AH captures the string from keyboard, it ends with ENTER (character 13), that's why, if you want to capture 25 characters, you must specify 26.

To know how many characters the user entered (length), access the second byte (offset buff + 1). The ENTER is not included, so, if user types 8 characters and ENTER, this second byte will contain the number 8, not 9.

The entered characters start at offset buff + 2, and they end when character 13 appears. We use this to add the length to buff+2 + 1 to replace chr(13) by '$'. Now we can display the string.

  • Yes : those 3 lines of code are all in the buff variable. Yes : if you want to modify the characters withing the string, you have to have SI point to buff+2. – Jose Manuel Abarca Rodríguez Apr 08 '15 at 19:10
  • Wouldn't it be easier instead of using the 09h function to use 40h or 4000h (I'm not sure why 4000h, it was part of my reference material) so that it doesn't have to have a '$' at the end of the string? The downside from what I can tell is that it will display all 25 characters even if the user didn't enter the full 25. – Jumpman Apr 08 '15 at 20:14
  • If you know the length, you can use ah=13h with int 10h (like this answer http://stackoverflow.com/questions/29460318/how-to-print-colored-string-in-assembly-language/29478158#29478158), but, if you don't want to bother about the length, you can use $. I have never tried 40h or 4000h. – Jose Manuel Abarca Rodríguez Apr 08 '15 at 20:17
  • 1
    @JoseManuelAbarcaRodriguez You don't have to do the step from 22 to 29 where you placed the $ at the CH(13) . You can simply replace `db 26 dup(0)` by this `db 26 dup('$')` – Hamza Anis Apr 04 '17 at 18:47
2

This is my code,maybe can help you.

;Input String Copy output

dataarea segment
    BUFFER db 81
           db ?
    STRING DB 81 DUP(?)
    STR1 DB 10,13,'$'   
dataarea ends

extra segment
    MESS1 DB 'After Copy',10,13,'$'
    MESS2 DB 81 DUP(?)
extra ends

code segment
main proc far
    assume cs:code,ds:dataarea,es:extra
start:
    push ds
    sub ax,ax
    push ax

    mov ax,dataarea   
    mov ds,ax

    mov ax,extra      
    mov es,ax

    lea dx,BUFFER     
    mov ah,0ah
    int 21h

    lea si,STRING
    lea di,MESS2
    mov ch,0
    mov cl,BUFFER+1
    cld
    rep movsb
    mov al,'$'
    mov es:[di],al

    lea dx,STR1            ;to next line
    mov ah,09h
    int 21h

    push es
    pop ds

    lea dx,MESS1           ;output:after copy
    mov ah,09h
    int 21h

    lea dx,MESS2
    mov ah,09h
    int 21h

    ret
main endp
code ends
    end start

And the result is:

c:\demo.exe
Hello World!
After Copy
Hello World!
Q10Viking
  • 982
  • 8
  • 9
0

You may follow this code :

; Problem : input array from user

.MODEL SMALL
.STACK
.DATA
   ARR DB 10 DUB (?)     

.CODE
 MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    XOR BX, BX
    MOV CX, 5

    FOR: 
      MOV AH, 1
      INT 21H
      MOV ARR[BX], AL
      INC BX
    LOOP FOR

    XOR BX, BX
    MOV CX, 5

    PRINT:  
     MOV AX, ARR[BX] ;point to the  current index 

     MOV AH, 2       ;output
     MOV DL, AX  
     INT 21H     

     INC BX           ;move pointer to the next element 
    LOOP PRINT       ;loop until done

 MAIN ENDP
rashedcs
  • 3,588
  • 2
  • 39
  • 40
0

;try this one, it takes a 10 character string input from user and displays it after in this manner, "Hello *10character string input"

.MODEL TINY
.CODE
.286
ORG 100h

    START:
     MOV DX, OFFSET BUFFER
     MOV AH, 0ah
     INT 21h
     JMP PRINT
     BUFFER DB 10,?, 10 dup(' ')

     PRINT:
     MOV AH, 02
     MOV DL, 0ah
     INT 21h
     MOV AH, 9
     MOV DX, OFFSET M1
     INT 21h
     XOR BX, BX
     MOV BL, BUFFER[1]
     MOV BUFFER [BX+2], '$'
     MOV DX, OFFSET BUFFER +2
     MOV AH, 9
     INT 21h

    M1: db 'Hello $'


 END START
 END      
Mario Camilleri
  • 1,457
  • 11
  • 24