简体   繁体   中英

Concatenate two strings in assembly

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
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM