装配最大和最小程序未产生预期结果

assembly max and min procedure not producing expected result

我正在尝试编写一个汇编程序,使用过程 (max_min) 查找数组的最小值和最大值。数组以零结尾,告诉程序已到达数组末尾。

结果应该如下: 对于数字 DW 3,4,5,2,6,0 max:6 min:2

但是我收到的结果是 最多 6 个: 分钟 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     

我在这里犯下了严重的错误 - 在没有 compiling/assembling 或测试的情况下盲目编码我的一种较弱的汇编语言。请将其用作我上面关于如何以更易于理解和维护的格式组织代码的评论的参考。即使这里有错误,它也展示了将代码分解为可管理的块并使用注释来解释正在发生的事情的想法。 (编辑说明: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