I have this piece of assembly code to accept a string and display the string back.
My problem is I’m not able to figure out how exactly the name is getting stored in name1 as we are using buff to store the input from the user.
I know that
buff label byte
maxchar db 50
readchar db 0
name1 db 48 dup(0)
is got something to do with this. But I’m not able to understand the working.
.model small
.stack
.data
buff label byte
maxchar db 50
readchar db 0
name1 db 48 dup(0)
m1 db 10,13,"enter name: $"
m2 db 10,13,"your name is: $"
.code
mov ax, @data
mov ds, ax
lea dx, m1
mov ah, 09
int 21h
lea dx, buff
mov ah, 10
int 21h
mov ah,0
mov al, readchar
add ax, 2
mov si, al
mov buff[si],24H ;ascii code for $ to terminate string
lea dx, m2
mov ah, 9
int 21h
lea dx, name1
mov ah, 09
int 21h
mov ah, 4ch
int 21h
end
please help!
thank you.
The input is read using DOS function 0x0a (or decimal 10 in your code) which performs buffered input. The DS:DX parameter points to a buffer that has the following format, which is at the location labeled
buff(or equivalentlymaxchar) in your program:So in your code, DS:DX points to
buffwhich indicates that up to 50 characters can be put into the buffer atname1. It looks like the code has a potential problem since there are only 48 bytes in the buffer, but the data structure indicates that there are 50 bytes. So it’s possible that the input will overwrite the first two bytes ofm1. Now, assembly programs – especially old ones – are known for performing all sorts of tricks to save space.m1isn’t used after the call to DOS function 0x0a, so this might have been intentional (but if so, I’m not sure why more ofm1wasn’t made available). I’d guess that it’s unintentional, and the bug just never manifested into anything noticeable.