如何从程序集中的不同内存位置启动数组?



我对汇编 8086 很陌生。我需要编写一个程序,仅将地址 0-10h 中的正数复制到从 20h 开始的内存块中,并将正数的数量保存在 dh 中。

我认为最好的方法是制作一个数组并在 20 小时将其复制到第二个数组,但我不知道如何使 arr2 从 20 小时开始,我也尝试制作一个值"大小",每次循环执行时都会包含

。这是我到目前为止的代码:

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 指令和 STOSB 指令。
如果比较值为"小于零",则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

但我不知道如何使 arr2 从 20h 开始

使第二个数组从地址 20h 开始的一种方法是填充第一个数组的末尾和第二个数组的开头之间的间隙。
计算如下所示:

db (TargetAddress - CurrentAddress) dup 0

目标地址当然是20h,当前地址由特殊符号$给出。

db 20h-$ dup (0)

将正数的数量保存在 DH 中。

如果您只对正数感兴趣,那么在循环的每次迭代中inc size没有帮助。如果复制到第二个数组的值为正数,只需递增计数器即可。

另外,为什么要将大小变量初始化为 1?从零开始更有意义。

我需要编写一个程序,该程序仅复制地址 0-10h 中的正数

.DATA
arr1 db 0h, -1h, 2h, 3h, -4h, 5h, 6h, -7h, 8h, 9h, 10h
db 20h-$ dup (0)
arr2 db 11 dup (?)
size db 0
.CODE
main:
mov  ax, @DATA
mov  ds, ax           ;You forgot this instruction!
xor  bx, bx    
xor  si, si
copyloop:
mov  al, arr1[si]
test al, al
js   NotInterested    ;Skip negative number
mov  arr2[bx], al     ;Copy positive number
inc  bx
inc  size
NotInterested:
inc  si
cmp  si, 11           ;Repeat for the 11 elements in first array
jb   copyloop  
mov  dh, size         ;Result to DH
mov  ax, 4C00h        ;Preferred way to end program on emu8086
int  21h

此解决方案可以轻松实现而无需大小变量。
计数已可从用于为第二个数组编制索引的BX值中获得。

.DATA
arr1 db 0h, -1h, 2h, 3h, -4h, 5h, 6h, -7h, 8h, 9h, 10h
db 20h-$ dup (0)
arr2 db 11 dup (?)
.CODE
main:
mov  ax, @DATA
mov  ds, ax           ;You forgot this instruction!
xor  bx, bx    
xor  si, si
copyloop:
mov  al, arr1[si]
test al, al
js   NotInterested    ;Skip negative number
mov  arr2[bx], al     ;Copy positive number
inc  bx
NotInterested:
inc  si
cmp  si, 11           ;Repeat for the 11 elements in first array
jb   copyloop  
mov  dh, bl           ;Result to DH
mov  ax, 4C00h        ;Preferred way to end program on emu8086
int  21h

最新更新