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

Because div divides edx:eax by whatever. The result lands in eax and the remainder in edx. If the result doesn't fit in eax (likely if edx contains garbage), an interrupt is raised, which the OS translates into SIGFPE.

于 2013-11-04T04:05:49.017 に答える