Reputation: 2881
I need to get the string length number, which my program succeeds in doing, however it also outputs the initial string without first letter.
.model small
.stack 200h
.data
nuskaitymoBuferis db 11
.code
pr1:
mov ax, @data
mov ds, ax
mov dx, offset nuskaitymoBuferis
mov ah, 0Ah
int 21h
mov nuskaitymoBuferis+11, '$'
mov dl, nuskaitymoBuferis+1
add dl, 30h
mov ah, 02h
int 21h
mov ah, 4ch
mov al, 00h
int 21h
end pr1
For example if the input is: 'test', the program outputs: '4est'
Upvotes: 1
Views: 1740
Reputation: 58427
int 21h / ah=02h
is not outputting more than one character. The string "test" is echoed to the console when you type it. Then you print the character '4' at the start of the same line, which gives you "4est".
Print a linefeed character if you want the '4' to appear on a new line. I.e. before you print the string length, do:
mov dl,10 ; linefeed
mov ah,2
int 21h
You've a couple of other issues in your code. nuskaitymoBuferis db 11
does not reserve space for 11 bytes; it reserves space for a single byte with the value 11. To reserve space for 11 bytes you'd use nuskaitymoBuferis db 11 dup(0)
.
Even with that change, you'd still have a buffer overflow at mov nuskaitymoBuferis+11, '$'
, because you're trying to write to the 12th byte of an 11-byte buffer (remember offsets start at zero).
Upvotes: 3
Reputation: 14399
In MSDOS a "goto to the start of the next line" is performed in two steps "goto start column" and "goto next line", hexadecimal: 0Dh (Carriage Return = CR), 0Ah (Line Feed = LF). When you press the ENTER button the computer gets only a CR, which is performed by INT 21h / AH=0Ah
and stored into nuskaitymoBuferis
. The cursor is now at the beginning of the line, but not at the next line - and there the '4' is printed.
TL;DR... Insert a Line Feed behind INT 21h / AH=0Ah
:
...
mov dx, offset nuskaitymoBuferis
mov ah, 0Ah
int 21h
mov nuskaitymoBuferis+11, '$' ; not really good ;-)
mov dl, 0Ah
mov ah, 02h
int 21h
...
Upvotes: 1