我想编写一个8086汇编语言程序来实现strcat函数,该函数将一个字符串连接到另一个字符串

问题描述 投票:0回答:1

我正在学习汇编,在这个例子中我想连接来自用户的两个字符串

data segment 
    str1 db "enter the first string : $"
    str2 db "enter the second string : $" 
    str3 db "output is : $"
    arr db 200dup('$')
ends
stack segment 
    dw 128 dup(0)
ends
code segment 
    start:
    mov ax,@data
    mov ds,ax
    
    lea dx,str1
    mov ah,09h
    int 21h             
    
    mov ah,10
    lea dx,arr 
    mov arr,8
    int 21h    
    mov ax,@data
    mov ds,ax
    
    lea dx,str2
    mov ah,09h
    int 21h  
    
     mov ah,10
    lea dx,arr 
    mov arr,8
    int 21h    
    mov ax,@data
    mov ds,ax
    
    lea dx,str3
    mov ah,09h
    int 21h
     
    lea dx,arr+2
    mov ah,09h
    int 21h
     
    mov ax,4c00h
    int 21h
input 1 : hello 
input 2 : world
output : world 

但我希望输出为:

hello world 

assembly x86-16 string-concatenation emu8086 strcat
1个回答
0
投票

嗯,你应该稍微研究一下存储单词的内存区域。这是如何运作的:

  1. 缓冲输入,0ah,读取第一个字。
               08 05 68-65 6C 6C 6F 0D 24 24 24 s : $..hello.$$$
  1. 移至第一个单词的末尾。第二个缓冲区从 0dh 开始。
  2. 缓冲输入,0ah,读取第二个字。
               08 05 68-65 6C 6C 6F 08 05 77 6F s : $..hello..wo
72 6C 64 0D 24 24 24 24-24 24 24 24 24 24 24 24 rld.$$$$$$$$$$$$
  1. 我们不再需要第二个缓冲区的第一个字节,因此在其中插入空间。
               08 05 68-65 6C 6C 6F 20 05 77 6F s : $..hello .wo
72 6C 64 0D 24 24 24 24-24 24 24 24 24 24 24 24 rld.$$$$$$$$$$$$
  1. 还有我们不需要的字节,即第二个字的大小。将字母向左移动并在末尾添加 $。
               08 05 68-65 6C 6C 6F 20 77 6F 72 s : $..hello wor
6C 64 24 0D 24 24 24 24-24 24 24 24 24 24 24 24 ld$.$$$$$$$$$$$$
  1. 显示结果。

data segment 
    newline db 13,10,'$'
    str1 db "enter the first string : $"
    str2 db "enter the second string : $" 
    str3 db "output is : $"
    arr db 200 dup('$')
data ends

dstack segment stack
    dw 128 dup(0)
    top label word 
dstack ends

assume cs:code, ds:data, ss:dstack

code segment 
    start:
    mov ax,data
    mov ds,ax
    
    mov ax,dstack
    mov ss,ax
    mov sp,top
    
    lea dx,str1
    mov ah,09h
    int 21h             
    
    mov ah,10
    lea dx,arr 
    mov arr,8
    int 21h    

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

;;;; Buffer mod 
    
    lea bx, arr + 1         ; read size of Input 1
    xor ax, ax              ; ax = 0
    mov al, [bx]            ; al = input 1 length
    add al, 1               ; add byte 
    add bx, ax              ; move to the end of input 1    
    
    mov dx,bx               ; dx = buffer of Input 2, size + 0dh, chars in buffer, buffer
    
    mov ah,10       
    mov byte ptr [bx], 8    ; max number of letters
    int 21h    
    
    lea dx,newline
    mov ah,09h
    int 21h     
    
    mov al,32               ; space between words
    mov [bx],al                     
    inc bx                  ; length of input 2
    
    xor cx,cx               ; save in cx
    mov cl,[bx]
    
    mov di,bx               ; copy from [bx] to [di]
    inc bx
    
    moveInput2:
        mov al,[bx]
        mov [di],al
        inc bx
        inc di
        
        dec cx
        jnz moveInput2
    
    dec bx
    mov byte ptr [bx],'$'   ; end of texts
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   

    lea dx,str3
    mov ah,09h
    int 21h
     
    lea dx,arr+2
    mov ah,09h
    int 21h
     
    mov ax,4c00h
    int 21h
    
code ends   
end start


© www.soinside.com 2019 - 2024. All rights reserved.