我对汇编 8086 很陌生。我需要编写一个程序,仅将地址 0-10h 中的正数复制到从 20h 开始的内存块,并将正数的数量保存在 dh 中。
我认为最好的方法是创建一个数组并将其复制到 20h 的第二个数组,但我不知道如何使 arr2 在 20h 开始,我还尝试创建一个值“size”每次循环执行时都会包含其中。
这是我到目前为止的代码:
org 0h
.DATA
arr1 DB 0h,-1h,2h,3h,-4h,5h,6h,-7h,8h,9h,10h
arr2 DB 11 dup(?)
size DB 1h
.CODE
main:
mov ax,@DATA
mov si,0
copyloop:
mov al, arr1[si]
mov arr2[si], al
inc size
inc si
cmp si, 9
jne copyloop
move dh,size
ret
最佳答案
因为您正在复制/移动数组条目,所以您可以充分利用 LODSB instruction和 STOSB instruction比较后。
如果比较结果为“小于零”,JL
就会跳转。
org 0h
.DATA
; here the position is 0h
arr1 DB 0h,-1h,2h,3h,-4h,5h,6h,-7h,8h,9h,10h
org 20h ; set position to 20h
; here we are at position 20h
arr2 DB 11 dup(?)
size DB 1h
.CODE
main:
mov ax,@DATA
mov ds, ax ; set data segment
lea si,arr1 ; address of source array (at 0h)
lea di,arr2 ; address of destination array (at 20h)
copyloop:
lodsb ; load number in si and inc si
cmp al, 0 ; check if number is positive
jl copyloop ; jump next if AL is less than zero
stosb ; store positive number in [di] and inc di
cmp si, 10 ; check if maximum length of 9 is reached
jbe copyloop ; if SI is below or equal to 9, continue loop
mov dh, size ; unknown function (!!!) - you didn't address this function
ret
关于assembly - 如何从汇编中的不同内存位置启动数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48195175/