我正在尝试编写一个汇编程序,使用过程(max_min)查找数组的最小值和最大值。数组以零结束,告诉程序已到达数组的末尾。
结果应如下: 对于数字DW 3,4,5,2,6,0 最大:6 分钟:2
然而,我收到的结果是 最多6: min 0:
Lp2 - 循环2应该找到最小值,因此它应该给出2。但是,它不会,结果为0。我如何纠正这个问题,以便最少得到2个?
%include "io.mac"
.STACK 100H
.DATA
Numbers DW 3,4,5,2,6,0
msg1 db "Max",0
msg2 db "Min",0
.CODE
.STARTUP
call max_min
nwln
PutStr msg1
nwln
PutInt ax
nwln
PutStr msg2
nwln
PutInt bx
done:
.EXIT
max_min:
mov si, Numbers ;point to array
mov ax, si
lp1:
cmp word [si],0
je next
add si,2
cmp ax, [si]
jl max
continue:
jmp lp1
next:
mov si, Numbers ;point to array
mov bx, si
lp2:
cmp word [si],0
je complete
add si,2
cmp bx, [si]
jg min
go:
jmp lp2
max:
mov ax, [si] ;keep track of max
jmp continue
min:
mov bx, [si] ;keep track of min
PutInt[si]
jmp go
complete:
ret
答案 0 :(得分:1)
我在这里犯了一个严重的罪 - 盲目编码我的一种较弱的汇编语言而不编译/汇编或测试。请使用此作为上述注释的参考,以便更简单地理解和维护格式。即使这里存在错误,它也表明了将代码分解为可管理的块并使用注释来解释正在发生的事情的想法。 (编者注:x86汇编程序很难看!我想要我的ARM代码:))
max_min:
mov si, Numbers ;point to array
; Set Min/Max to first array entry and exit if value is 0
mov ax, [si]
mov bx, [si]
cmp word [si],0
je done
; Increment to next value
add si,2
main_loop:
; if number is 0, we are done
cmp word [si],0
je done
; if number is greater than or equal to max, don't assign to ax
cmp ax, [si]
jge not_max
mov ax, [si]
not_max:
; if number is less than or equal to min, don't assign to bx
cmp bx, [si]
jle not_min
mov bx, [si]
not_min:
; Next array entry and loop
add si,2
jmp main_loop
done:
ret