Concatenate two given strings in 8086 + MOVSB does not work

15,201

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!
Share:
15,201
AliLotfi
Author by

AliLotfi

Sharif University of Technology Information Technology Engineering Take a look at my website alilo.ir. profile for AliLotfi on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/3597738.png?theme=dark

Updated on June 04, 2022

Comments

  • AliLotfi
    AliLotfi almost 2 years

    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

  • AliLotfi
    AliLotfi over 9 years
    Thank you very much. I read the definition of "MOVSB" times and times, but I did not get the point! really thanks!