Int 21 User Input

So finally, I’ve decided to use an assembler other than debug.exe. The one I’m using is known as fasm, Flat Assembler. It seems simple enough for me to understand and use so far. So now I’m making MZ format executable files that run in command line. Most notably now is that there is no longer a file size limit for the program (unlike com files). And MZ exe files have a header of bytes that give information about the file (information about the MZ format found here: http://www.delorie.com/djgpp/doc/exe/). When viewed through a disassembler, the MZ header is not included and the offset of 0 starts right after the header. FASM is portable too!

So now I’m going to provide some examples (now in FASM examples, no longer debug.exe) of getting user input in assembly.
First we have the obvious #8 function of int 21 where you read a single character of input:

format MZ
push cs
pop ds        ;Code and Data inside same segment
mov ah,08h    ;Read character function
int 21h       ;Do it
mov ah,02h    ;Write character function
mov dl,al     ;Character read from ah=08 into dl to print
int 21h       ;Do it
mov ax,4c00   ;ah=4c Exit function,al=00 Return code
int 21h       ;Do it

The above code will read a character of input and output the character you pressed.

Next is the buffered input function (#0x0a) where the user inputs data (ending with CrLf) into a buffer:

format MZ
push cs
pop ds             ;Code and Data inside same segment
mov ah,0ah         ;Buffered input function
mov dx,buff        ;Points to buffer to write to
int 21h            ;Do it
mov ax,0900h       ;Print string function
mov dx,crlf        ;Points to data to print new line
int 21h            ;Do it
mov cl,[buff+1]    ;Byte offset 1 of buffer contains number of chars read
mov bx,buff        
add bx,3           ;bx points to start of read chars in buffer + 1
add bx,cx          ;bx points to end of read chars in buffer + 1
mov byte [bx],'$'  ;Terminate read chars with '$' for ah=09 function
mov ax,0900h       ;Print string function
mov dx,buff+2      ;dx points to start of '$' terminated read chars in buffer
int 21h            ;Do it
mov ax,4c00h       ;ah=4c Exit function, al=00 return code
int 21h            ;Do it

buff db 20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
crlf db 0dh,0ah,'$'

So this one is a little more complicated. The 0x0a function means to read buffered input ending with CrLf from the user. The input will be stored in a buffer within the program pointed to by DS:DX. The format of the buffer is as follows:

+--------+---------+---------------------------------------------------------------------+
| Offset |  Type   | Data                                                                |
+--------+---------+---------------------------------------------------------------------+
|  0x00  |  BYTE   | Size of buffer                                                      |
+--------+---------+---------------------------------------------------------------------+
|  0x01  |  BYTE   | Reserved. Number of characters read from last call to this function |
+--------+---------+---------------------------------------------------------------------+
|  0x02  | BYTE(s) | Characters values read                                              |
+--------+---------+---------------------------------------------------------------------+

Using this format, we will get the number of characters read from the first offset of the buffer and add that value to where the character values start in this buffer + 1. From there we will insert a ‘$’ at that address to terminate the characters in that buffer for the print string function, then display it.

For reference try search for “ralf brown interrupt list” and it should be the first link where you get to look for functions for interrupts. The interrupt used in this thread is #21.

13 thoughts on “Int 21 User Input

    • Using debug? ok, here’s a debug script:

      a 100
      mov ah,09
      mov dx,011a
      int 21
      push ax
      mov ah,09
      mov dx,012a
      int 21
      mov ah,02
      pop dx
      int 21
      jmp 100
      db 0d,0a,"Press a key. $"
      db 0d,0a,"You pressed: $"

      r cx
      003a
      n keys.com
      w
      q

  1. Is a wrong example. I try this change :
    mov dx,0116
    and
    mov dx,0126
    and is need to change this
    mov ah,02
    pop dx
    int 21
    because not working .
    I don’t understand how you try use stack .
    The easy way is to use :
    a 100
    mov ah, 1 ; keyboard input subprogram
    int 21 ; read character into al
    mov dl, al ; copy character to dl
    mov ah, 2 ; character output subprogram
    int 21 ; display character in dl

    rcx
    003a
    n keys2.com
    w
    q
    But is need to create messages and show the second char after press ENTER.
    Not very easy … :)

  2. How can i input a string
    example when i type “hello world” then i press enter the
    program will display the text “hello world”

  3. I will show a complex example… if I remember well.
    -a
    mov ah,08
    int 21
    cmp AL,59
    jz 010E
    cmp AL,4E
    jz 010E
    jmp 0100
    :010E
    where 010E is last adress ( see :010E)
    This programm read input Y or N….

  4. so can you help me on my problem?
    can you give an example program that will let me to input a string and display it! please help me this code is our assignment.. thank you..

    • push cs
      pop ds ;Code and Data inside same segment
      mov ah,0ah ;Buffered input function
      mov dx,buff ;Points to buffer to write to
      int 21h ;Do it
      mov ax,0900h ;Print string function
      mov dx,crlf ;Points to data to print new line
      int 21h ;Do it
      mov cl,[buff+1] ;Byte offset 1 of buffer contains number of chars read
      mov bx,buff
      add bx,3 ;bx points to start of read chars in buffer + 1
      add bx,cx ;bx points to end of read chars in buffer + 1
      mov byte [bx],’$’ ;Terminate read chars with ‘$’ for ah=09 function
      mov ax,0900h ;Print string function
      mov dx,buff+2 ;dx points to start of ‘$’ terminated read chars in buffer
      int 21h ;Do it
      mov ax,4c00h ;ah=4c Exit function, al=00 return code
      int 21h ;Do it

      what is buffer? and crlf , buffer+2 and buffer+1 i cannot under stand those can you teach me please..

  5. please help me with this,, if u input 1-4 ouput is below 5, if input 5-8 output is above 5, and if input 9 above the program will bw terminated.. using jumps.. thank you…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: