程序集连接两个字符串

时间:2016-08-08 11:36:11

标签: assembly dos tasm x86-16

我想连接两个字符串但是在我的输出中而不是获取最终的连接字符串,我得到一行奇怪的字符和空格,也许有人可以帮我一点。我想将结果保存在s3中。这是代码

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

我找到了解决方案,希望它是一个好的,没有任何错误: 数据段     msg db 0Dh,0Ah,“String1:$”     msg2 db 0Dh,0Ah,“String2:$”     rev db 0Dh,0Ah,“结果:$”

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 个答案:

答案 0 :(得分:0)

在您的第一个代码中,您必须将si设置为在标记s3之前指向字符串l5:的开头(因为si }指向循环s3之后l4:的结尾:

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

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