Reputation: 186
I need to concatenate two strings and then show it on the screen. When i run this program the output is not what i was expected. Can you tell me what is wrong in this code?
example:
expected input
first string = 'my'
second string = 'string'
expected output = 'my string'
assume ds:date, cs:cod
date segment
message1 db "Enter the fisrt string $"
message2 db 0ah, 0dh , "Enter the second string $"
message3 db 0ah, 0dh, "The string is $"
string1 db 20, ?, 20 dup('')
string2 db 20, ?, 20 dup('')
string3 db 40 dup('')
date ends
cod segment
start :
mov ax, date
mov ds, ax
lea dx, message1
mov ah, 09h
int 21h
lea dx, string1
mov ah, 0ah
int 21h
lea dx, message2
mov ah, 09h
int 21h
lea dx, string2
mov ah, 0ah
int 21h
mov si, 2 ; index of the first element
mov di, 0 ; final string first element index
mov cl, string1[1] ; string lenght
loopp:
mov bl, string1[si]
mov string3[di], bl
inc si
inc di
loop loopp
mov cl, string2[1] ; second tring lenght
mov si, 2
loopp2:
mov bl, string2[si]
mov string3[di], bl
inc si
inc di
loop loopp
mov string3[di], '$'
lea dx, message3
mov ah, 09h
int 21h
lea dx, string3
mov ah, 09h
int 21h
mov ax, 4c00h
int 21h
cod ends
end start
Upvotes: 0
Views: 1723
Reputation: 39166
string1 db 20, ?, 20 dup('') string2 db 20, ?, 20 dup('') string3 db 40 dup('')
You don't have the necessary buffers with the above db
directives! What you've placed between the dup
parenthesis is the empty string. Not sure if this assigns any memory at all...
This should work:
string1 db 20, ?, 20 dup(0)
string2 db 20, ?, 20 dup(0)
string3 db 40 dup(0)
and this too:
string1 db 20, ?, 20 dup(' ') ; ' ' is a space character
string2 db 20, ?, 20 dup(' ')
string3 db 40 dup(' ')
The loop
instruction uses the whole CX
register but your code only fills in the CL
part!
Use this:
mov cl, string1[1] ; string lenght
mov ch, 0
...
If you want a separating space character between my and string, you'll have to write it in between copying both strings.
Upvotes: 2