在 8086 汇编中执行 DIV 指令

Executing the DIV instruction in 8086 assembly

我的程序打印了 msg3 语句 (PutStr msg3),但没有继续执行

DIV CX

我程序中的指令。
我对那个寄存器有什么不正确的地方吗?
或者指令应该是

DIV [CX] 

或者我没有正确设置比较和跳转条件?

prime_loop:
        sub AX,AX       ;clears the reg to allow the next index of the array
        sub CX,CX      ;clears counter to decrement starting from number of the value for array
        mov AX, [test_marks+ESI*4]      ;copy value of array at index ESI into reg
        mov CX, [test_marks+ESI*4]     ;copy value of array at index ESI into reg for purposes of counting down
    check_prime:
        dec CX 
        nwln
        PutStr msg3
        div WORD CX   ;divide value of EAX by ECX
        cmp DX,0     ;IF the remainder is zero
        je chck_divisor    ;check to see divisor 'ECX'
        sub AX,AX   ;else clear quotient register EAX
        sub DX,DX    ;clear remainder register
       mov AX,[test_marks+ESI*4]     ;move the number of the current iteration back into EAX
        jmp check_prime    ;start again from loop
    chck_divisor:
        cmp CX,1
        jne prime_loop     ;if the divisor is not 1 then it is not a prime number so continue with iterations
        PutInt AX     ;else print the prime_num
        PutStr
        inc ESI
        jmp prime_loop
    done:
        .EXIT

可能是因为 DX 在 div 之前没有归零,所以你会溢出。我不知道你的环境如何处理溢出。

这些是关于您的代码的一些要点:

  • 如果这确实是 8086 汇编 那么像 mov AX, [test_marks+ESI*4] 这样使用缩放索引寻址 的指令根本不存在!

  • 4 的比例表明您的数组中充满了双字,但您只使用了一个字。这可能是您想要的,但它看起来很可疑。

  • 我们希望没有数组元素是 1,因为如果是这样,那么 div cx 指令将触发异常 (#DE)。因为你没有测试CX寄存器变成0.

  • check_prime 循环中,只有第一次迭代缺少 DX 归零以给出正确的商数。

解决方案将取决于目标架构 8086x86。现在您的程序是两者的结合!