Concatenate two given strings in 8086 + MOVSB does not work

assemblyx86-16

I'm trying to write a 8086 assembly program to concatenate two given strings.
In order to do this, I used a "REP MOVSB" instruction, but the program didn't work well. So I wrote a program that should statically concatenate two strings, but it seems that "REP MOVSB" does not affect on strings at all.
Here's the part of code I wrote for test:

                                          
data    segment

string1 db  "Lotfi", 0
string2 db  "Ali ", 0 

data ends


code segment    

ASSUME  CS: code, DS: data

start:
    cld
    mov     ax  , data
    mov     DS  , ax

    mov     SI  , offset string1
    mov     DI  , offset string2
    add     DI  , 3 ; Adding the length of destination string to the DI


    mov     cx  , 5
    rep movsb ; This should concat two strings

    ; Printing the result character by character on the console
    mov     SI  , offset string2
l:  lodsb           ; Printing loop
    mov dl, al
    mov ah, 2h
    int 21h
    jmp l


    hlt 

    code ends
end start

The result of the code is something like:

Ali             ΓΌ,Z0???... (And so)

What's wrong with my code?
Tanx

Best Answer

movsb moves from DS:SI to ES:DI. You've loaded the DS but not the ES register.

You need only add a single line:

cld
mov     ax  , data
mov     DS  , ax
mov     ES  , ax   ; here!
Related Topic