mov ax, @data
mov dx, ax
You want to initialize the DS segment register here but have erroneously written DX. Honest typo, but your code will have corrupted the Program Segment Prefix in this manner.
I am trying to get a string of numbers from user terminated by ENTER key or the length of string reaches 20
It's clear that you need a loop to do this and that you will need 2 tests to decide about when to stop!
- test the character in
AL to see if it is 13
- test a counter (e.g.
CX) to see if it reached 20
xor cx, cx ; Empty counter
mov si, offset var1
TheLoop:
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
cmp al, 13
je programend
mov [si], al
inc si
inc cx
cmp cx, 20
jb TheLoop
programend:
But wait, didn't the task say that it had to be a string of numbers? You need to make sure that the input is indeed a number.
Numbers "0" through "9" have ASCII codes 48 through 57.
xor cx, cx ; Empty counter
mov si, offset var1
TheLoop:
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
cmp al, 13
je programend
cmp al, 48
jb TheLoop ; Not a number
cmp al, 57
ja TheLoop ; Not a number
mov [si], al
inc si
inc cx
cmp cx, 20
jb TheLoop
programend:
Without using a separate counter and using the assembler's ability to translate characters into codes:
mov si, offset var1
TheLoop:
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
cmp al, 13
je programend
cmp al, "0"
jb TheLoop ; Not a number
cmp al, "9"
ja TheLoop ; Not a number
mov [si], al
inc si
cmp si, offset var1 + 20
jb TheLoop
programend: