How to make string input in assembly language?

Please, does anyone know how to enter string input in assembly language? I use int 21 to display and enter characters.

+7
source share
2 answers

You can use the 0Ah function to read buffered input. Given the string buffer in ds:dx , it reads a string up to 255 in length. Buffer layout:

 Byte 0 String length (0-255) Byte 1 Bytes read (0-255, filled by DOS on return) Bytes 2-..Length+2 (The character string including newline as read by DOS). 

An example of a small COM file that reads a string and then returns it to the user:

  org 0x100 start: push cs pop ds ; COM file, ds = cs mov ah, 0x0A ; Function 0Ah Buffered input mov dx, string_buf ; ds:dx points to string buffer int 0x21 movzx si, byte [string_buf+1] ; get number of chars read mov dx, string_buf + 2 ; start of actual string add si, dx ; si points to string + number of chars read mov byte [si], '$' ; Terminate string mov ah, 0x09 ; Function 09h Print character string int 0x21 ; ds:dx points to string ; Exit mov ax, 0x4c00 int 0x21 string_buf: db 255 ; size of buffer in characters db 0 ; filled by DOS with actual size times 255 db 0 ; actual string 

Please note that it will overwrite the input line (and therefore may not look like the program is doing anything!)

Alternatively, you can use the 01h function and read the characters in a loop yourself. Something like this (note that it will overflow subsequent buffers if more than 255 characters are entered):

  org 0x100 start: push cs pop ax mov ds, ax mov es, ax; make sure ds = es = cs mov di, string ; es:di points to string cld ; clear direction flag (so stosb incremements rather than decrements) read_loop: mov ah, 0x01 ; Function 01h Read character from stdin with echo int 0x21 cmp al, 0x0D ; character is carriage return? je read_done ; yes? exit the loop stosb ; store the character at es:di and increment di jmp read_loop ; loop again read_done: mov al, '$' stosb ; 'Make sure the string is '$' terminated mov dx, string ; ds:dx points to string mov ah, 0x09 ; Function 09h Print character string int 0x21 ; Exit mov ax, 0x4c00 int 0x21 string: times 255 db 0 ; reserve room for 255 characters 
+7
source

A string is just a series of characters, so you can use the int 21 code inside the loop to get the string, one character at a time. Create a label in the data segment to hold the line, and each time you read a character, copy it to that label (increasing the offset each time so that your characters are stored sequentially). Stop the cycle when reading a specific character (possibly enter).

Doing all of this manually is tedious (think about how backspace will work), but you can do it. Alternatively, you can contact stdio, stdlib, etc. And act as a call library to do most of the work for you.

+1
source

All Articles