[org 0x0100]
jmp start
max1:
max: mov si, 0 ;for loop counter
mov ax, [arr + si] ;storing the first element of the array
mov bx, 2
mov cx, 0
mov dx, [arr + si + bx] ;storing the second element
cmp dx, ax ;comparing if dx is greater than ax
jg store1 ;if yes, jmp to store1
add si, 2 ;if not, inc the si
cmp si, 18 ;comparing for the loop counter
je exit1 ;if si=18, jmp to exit1
jmp max
store1: mov ax, dx ;if condition on line 13 is true, jmp here and move dx into ax
mov word[maxnum], ax ;mov the new value of ax into maxnum
add si, 2 ;inc si
jmp max ;go back to start of the loop
exit1: pop dx
pop cx
pop bx
pop ax
pop si
ret
start: mov si, 0
mov ax, 0
mov bx, 0
mov cx, 0
mov dx, 0
push si
push ax
push bx
push cx
push dx
call max1
mov ax, 0x4c00
int 0x21
arr: dw 6,4,1,7,9,10,11,13,3,2
maxnum: dw 0
我是汇编语言的新手。
我得到的只是
数组检查中断
该程序一直在崩溃。
这是什么意思?如何解决这个问题?
如果数组中有负号,我应该使用什么条件?
,这在没有子规范的情况下可以很好地工作。
您的程序 crashes 因为它进入了由错位的max:
标签引起的无限环路。
您需要在下面编写max:
标签 SI
的初始化。现在您一遍又一遍地重复:
max1: ...
mov si, 0 ;for loop counter
max: mov ax, [arr + si]
所有这些push
的S和pop
s都可以省略。由于您立即退出DOS,因此没有任何保留任何东西。
如果您坚持保存,请将这些push
的S移至子例程的开头:
max1: push si
push ax
push bx
push cx
push dx
mov si, 0
...
即使您存储了新的最大值,也必须检查结束条件:
store1: mov [maxnum], dx ;mov the new value of dx into maxnum
add si, 2
cmp si, 18
jne max ;go back to start of the loop
exit1: pop dx
...
您可以整理一下。您不使用CX
寄存器,也不需要BX
寄存器。更换
mov bx, 2
mov cx, 0
mov dx, [arr + si + bx] ;storing the second element
mov dx, [arr + si + 2] ;storing the second element
,如果数组中有负号,我应该使用什么条件?
您的程序已经处理了自编写jg store1
以来的可能性。(jg
用于签名数字,ja
适用于无符号的数字)