assembly - 如何从汇编中的不同内存位置启动数组?

标签 assembly emu8086

我对汇编 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 instructionSTOSB 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/

相关文章:

c - 缓冲区溢出(返回地址)

c - NASM ORG 指令有 GCC 版本吗?

assembly - 是否有逻辑算法为给定的输入生成唯一的输出?

assembly - 汇编8086 EQU指令

assembly - emu8086的汇编语言回文程序

assembly - 如何获取 VESA BIOS 信息

algorithm - 变质发生器

汇编 8086 光标放置

assembly - 汇编器无法打开文件