汇编语言 实现1.将数据区buf1中的10个数,传送到数据区buf2 2.计算buf1数据的累加和

1、 将数据区buf1中的10个数,传送到数据区buf2

; multi-segment executable file template.

data segment
    buf1 dw 1,2,3,4,5,6,7,8,9,10
    buf2 dw ?
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax
    
    lea si,buf1
    lea di,buf2
    mov cx,10 
    rep movsw;拷贝
     
     
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

串传送指令有四种格式
movs DST,SRC
movsb 字节
movsw 字
movsd 双字 (适合386后继机型)

如果不用传送指令
用循环实现

; multi-segment executable file template.

data segment
    buf1 dw 1,2,3,4,5,6,7,8,9,10
    buf2 dw ?
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax
    
    lea si,buf1   ;得到buf1的偏移地址
    mov di,0  	 ;定义buf2的偏移地址
    mov cx ,10    ;计数器
     
again:
    mov bx,buf1[si]
    mov buf2[di],bx
    add si,2
    add di,2
    loop again
     
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

运行结束不清楚为什么buf1后面还有多余的数据

如果有知道原因的希望可以告诉我,刚接触汇编。

2.计算buf1数据的累加和

; multi-segment executable file template.

data segment
    buf1 dw 1,2,3,4,5,6,7,8,9,10
    count dw ?
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax
    
    mov si,offset buf1
    mov cx,10 ;计数器
    mov ax,0  ;求和结果保存

again:
    add ax,buf1[si]  ;
    add si,2       ;因为是字,所以占两个字节
    loop again

    mov count,ax   ;将和传值给count
    
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

也是通过循环来实现。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/JulianBrandt/article/details/88770152