1

我正在处理一项任务,以获取用户提供的整数并确定它是否为素数。我编写的程序运行良好我只是不完全理解为什么我需要在每个周期将 edx 设置为 0。

    ;--------------------------------------------------------------------------
IsPrime PROC
;
; This determines if the integer is a prime
;--------------------------------------------------------------------------
mov ebx,eax                 ;Copying eax -> ebx
sar eax,1                   ;Arithmetic shift right to make eax = eax/2
mov esi,eax                 ;Setting esi = half of our number
mov ecx,1

    isPrimeLoop:
        add ecx, 1          ;increments ecx, starts at 2
        cmp ecx,esi
        ja itsPrime

        mov edx,0
        mov eax,ebx
        div ecx             ;dividing to see if it has a remainder or not
        cmp edx,0
        jz itsNotPrime
        jmp isPrimeLoop

    itsNotPrime:                ;displays results for numbers that are not prime
        mov eax,ebx
        call WriteDec
        mWrite " is not a prime number it is divisible by "
        mov eax,ecx
        call WriteDec
        call Crlf
        jmp endPrime



    itsPrime:                   ;displays results for numbers that are prime
        mov eax,ebx
        call WriteDec
        mWrite " is a prime number."
        call Crlf
        jmp endPrime

    endPrime:
ret
IsPrime ENDP
4

1 回答 1

2

因为 div 将 edx:eax 除以任何东西。结果落在 eax 中,其余的落在 edx 中。如果结果不适合 eax(可能 edx 包含垃圾),则会引发中断,操作系统将其转换为 SIGFPE。

于 2013-11-04T04:05:49.017 回答