2
votes

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

3
Random notes: 1) Your loop is infinite. 2) There's no allocated memory after the end of String2. 3) After the concatenation the resulting string isn't null-terminated. 4) Just in case: Are you sure your code operates in a 16bit environment? If it's 32bit you have random garbage in upper 16 bits of all registers. - Ondrej Tucny
Thank you, I considered all of your recommendation in my real program (except the last one about 16 and 32 bit, which I didn't get it!), But here I deleted them all for making it easier to read - AliLotfi
@OndrejTucny considering there are calls to the DOS kernel (int 21h) I think it's safe to assume the code runs in a 16-bit environment. - davmac
@davmac "I have… seen things you people wouldn't believe…" (However, yes it's safe to assume.) - Ondrej Tucny
This method could, theoretically, overwrite data in memory. Better to reserve a buffer in memory, and concatenate the strings in the reserved space. - Andrew Hardiman

3 Answers

7
votes

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!
-1
votes
DATA SEGMENT
STR1 DB "Tanay$"
STR2 DB "Patil$"
LEN DB ?
DATA ENDS
CODE SEGMENT
START:ASSUME CS:CODE,DS:DATA
MOV AX,DATA
MOV DS,AX
LEA SI,STR1
LEA DI,STR2
MOV AL,"$"
NEXT1:CMP[SI],AL
JZ EXIT
INC LEN
INC SI
JMP NEXT1
EXIT:MOV BL,"$"
NEXT:MOV AL,[DI]
CMP AL,BL
JZ EXIT1
MOV [SI],AL
INC SI
INC DI
JMP NEXT
EXIT1:MOV[SI],BL
MOV AH,09H
LEA DX,STR1
INT 21H
MOV AH,0AH
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START

The output will show in your TASM window

-4
votes

org 100h jmp start firstN: db "Jaymar", 0Ah, 0Dh, 24h lastN: db "Porras", 0Ah, 0Dh, 48h

start: mov dx, firstN, lastN

mov ah, 09h int 21h mov ah, 0 int 16h

mov ah, 09h int 21h mov ah, 0 int 16h

ret

Tanx ^_^