assembly concatenate two strings

感情迁移 提交于 2021-01-29 04:02:04

问题


I want to concatenate two strings but in my output instead of getting the final concatenated string, I get a line of weird characters and spaces, maybe someone could help me a bit. I want to save the result in s3. Here is the code

DATA SEGMENT
    STR1 DB "ENTER FIRST STRING HERE ->$"
    STR2 DB "ENTER SECOND STRING HERE ->$"
    STR3 DB "CONCATEnatedD STRING :->$"
    STR11 DB "FIRST STRING : ->$"
    STR22 DB "SECOND STRING: ->$"

    s1 DB 20 DUP("$")
    s2 DB 20 DUP("$")
    s3 db 40 dup(?)
    NEWLINE DB 10,13,"$"

DATA ENDS

CODE SEGMENT

    ASSUME DS:DATA,CS:CODE
START:

    MOV AX,DATA
    MOV DS,AX


lea dx,str1
mov ah,09h
int 21h

lea si,s1

l1: mov ah,01h
int 21h

mov [si],al
inc si
cmp al,31h
jne l1

dec si

lea dx,str2
mov ah,09h
int 21h

lea di,s2

l2: mov ah,01h
int 21h

mov [di],al
inc di
cmp al,32h
jnz l2

lea si,s3
lea di,s1


lea si,s3
lea di,s1
l3: mov al,[di]
mov [si],al
inc si
inc di
cmp al,31h
jnz l3

dec si

lea di,s2

l4: mov al,[di]
mov [si],al
inc si
inc di
cmp al,32h
jnz l4

lea dx,str3
mov ah,09h
int 21h

l5: mov dl,[si]
cmp dl,32h
jz l6
mov ah,02h
int 21h
inc si
jmp l5


l6:MOV AH,4CH
    INT 21H


CODE ENDS
END START

I have found the solution, hope it is a good one without any mistake: data segment msg db 0Dh, 0Ah, "String1: $" msg2 db 0Dh, 0Ah, "String2: $" rev db 0Dh, 0Ah, "Result: $"

buffer label byte  
buffer2 label byte
str_maxlen db 255
str_length db 0

str_string db 255 dup(0)
str_string2 db 255 dup(0)
result db 255 dup('$')
num db 0
data ends

code segment
assume cs:code, ds:data
start:
mov ax,data
mov ds,ax

lea dx,msg
mov ah,09h
int 21h

lea dx,buffer
mov ah,0Ah
int 21h

mov cl,str_length
mov bh,cl
mov ch,0
lea si,str_string
lea di,result

op1:    mov bl,[si]
mov [di],bl
inc di
inc si
loop op1

mov cl,bh
op3:    dec si
loop op3

lea dx,msg2
mov ah,09h
int 21h

lea dx,buffer2
mov ah,0Ah
int 21h
mov cl,str_length

lea si,str_string
mov cl,str_length

mov ax,0
op2:    mov al,[si]
mov [di],al
inc di
inc si
loop op2

lea dx,rev
mov ah,09h
int 21h

lea dx,result
mov ah,09h
int 21h

mov ah,4Ch
int 21h
code ends

end start

回答1:


In your first code, you have to set si to point to the beginning of string s3 right before label l5: (because si was pointing to the end of s3 after loop l4:) :

.
.
.
lea dx,str3
mov ah,09h
int 21h

lea si,s3          ;◄■■■■■■■■■■■  
l5: mov dl,[si]
cmp dl,32h
jz l6
.
.
.


来源:https://stackoverflow.com/questions/38828212/assembly-concatenate-two-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!