在8086组件中执行DIV指令



我的程序打印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

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

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

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

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

  • chem_prime 循环中,只有第一个迭代缺少DX的零以提供正确的商。

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

,由于DX在DIV之前未被归零,因此您可能会溢出。我不知道您的环境如何处理溢出。

最新更新