需要帮助使用 masm 以 80x86 汇编语言连接两个字符串

Need help concatenating two strings in 80x86 assembly language using masm

您好,我正在使用连接两个字符串的 masm 用 80x86 汇编语言编写程序。我想要做的是找到第一个字符串的结尾位置,然后将第二个字符串的内容添加到第一个字符串中。这是我目前的代码:


.586
.MODEL FLAT
.STACK 4096
INCLUDE io.h 

.DATA
prompt      BYTE    "Input String", 0
string1     BYTE    80 DUP (?)
string2     BYTE    80 DUP (?)
displayLbl  BYTE    "Concatenated string", 0

.CODE
_MainProc PROC
        input   prompt, string1, 80 ; ask for first string
        input   prompt, string2, 80 ; repeat for second string

        lea     eax, string1        
        push    eax                 
        lea     ebx, string2
        push    ebx

        call    strConcatenation    ; procedure to concatenate the strings

        add     esp, 8              ; remove parameters
        output  displayLbl, string1 ; display result

        mov     eax, 0              ; exit with return code 0
        ret
_MainProc ENDP

strConcatenation PROC

        push    ebp
        mov     ebp, esp
        push    edi
        push    esi
        pushfd

        mov     edi, [ebp+8]
        repnz   scasb               ; scan for null in string1
        
        mov     esi, [ebp+12]
        dec     edi

        cld
whileConcStr:
        cmp     BYTE PTR [esi], 0   ; null source byte?
        je      endWhile            ; stop copying if null

        lodsb                       ; load data
        stosb                       ; store data
        jmp     whileConcStr        ; go check next byte
endWhile:
        mov     BYTE PTR [edi], 0   ; terminate destination string

        popfd                       ; restore flags
        pop     esi                 ; restore registers
        pop     edi
        pop     ebp
        ret     
        
strConcatenation ENDP

END

当我输入 'assembly' 和 'language' 等字符串时,没有任何变化。感谢任何帮助,谢谢。

三个错误:

  • 你对 strConcatenation 的论点被颠倒了。就目前而言,您正在将 string1 连接到 string2.

    的末尾
  • repne scasb 扫描 al 寄存器中您尚未初始化的值。要扫描 nul,将其归零:xor al, al.

  • repne scasb 在找到 al 中的字节或当 ecx 达到零时终止(每次迭代都会递减)。您也没有初始化 ecx。要无限期扫描直到找到字节,可以将 ecx 设置为 -1