如果没有“mov edx,0”,为什么这个程序会失败?

时间:2013-11-04 04:04:14

标签: assembly x86

我正在进行一项任务,以获取用户提供的整数并确定它是否为素数。我编写的程序运行正常我只是不完全理解为什么我需要在每个循环中将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

1 个答案:

答案 0 :(得分:2)

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

相关问题