I'm making a program in TASM assembly (i honestly have no idea if it's 16 bit, x86, 64 bit maybe 8086?. I'm using TASM inside DOSBox for assembling, linking, and testing.
The user inputs characters until the user presses Enter key. then echos what they typed back to the console. This is my code:
IDEAL
model small
STACK 100h
DATASEG
wholeinput db 00
CODESEG
start:
mov ax,@data
mov ds,ax
mov bx, 0 ; bx is the counter
input:
mov ah, 1 ; input moves the input to al
int 21h
cmp al, 13 ; 0xD is enter key
je enterPressed ; if its not 'enter' then continue
add [wholeinput+bx], al ; add the given character to the end of the string
inc bx ; inc counter of string length
jmp input ; continue loop
enterPressed:
add [wholeinput+bx], '$' ; add to the last byte of the thing a string terminator
mov ah, 9
mov dx, offset wholeinput ; print the whole thing
int 21h
exit:
mov ax,4c00h
int 21h
END start
The first time I run the program it works as expected. When I run the same program a second time with the same input it prints gibberish to the console.
My theory is that the memory from my previous run is somehow copied to the next run. this could be a very stupid thing to say but I'm a noob...
What could be my problem? How can I solve it?
Edit:
Thanks to Mike Nakis and Peter Cordes for this solution:
The problem was that I did not reserve enough space for the input.
wholeinput db 00
only reserve one byte. Fix:
wholeinput db 100 dup(0)