Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 为什么这个过程没有“成功”就失败了;mov edx,0;?_Assembly_X86 - Fatal编程技术网

Assembly 为什么这个过程没有“成功”就失败了;mov edx,0;?

Assembly 为什么这个过程没有“成功”就失败了;mov edx,0;?,assembly,x86,Assembly,X86,我正在做一个赋值,以获取用户提供的整数并确定它是否为素数。我写的程序运行得很好,我只是不太明白为什么我每个周期都需要将edx设置为0 ;-------------------------------------------------------------------------- IsPrime PROC ; ; This determines if the integer is a prime ;-------------------------------------------

我正在做一个赋值,以获取用户提供的整数并确定它是否为素数。我写的程序运行得很好,我只是不太明白为什么我每个周期都需要将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

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

对,但如果有余数,edx应为余数,如果没有余数,则应将其设置为0,因为edx是零标志。Uhh div有64位输入。在该过程中,是否有比每个周期都必须将0传递给edx更好的方法来避免问题?否,在
div ecx
之前的代码中(或在任何
div reg32/mem32
之前),您需要
mov edx,0
/
xor edx,edx
/
sub edx,edx
来清除前一
div
之后的
edx,因为红利总是在
edx:eax
中,并且
edx
用于存储剩余部分。无论如何,如果您正在学习x86汇编,那么担心优化是没有用的,因为现在x86汇编优化比试图省略一条指令要困难得多。此外,您还混淆了术语,
edx
不是零标志(ZF)。根据许多指令的结果设置零标志。在
div
之后,ZF是未定义的。将寄存器设置为0实际上总是通过
xor reg,reg
完成的。所以我建议你习惯这样做,而不是
mov-reg,0
。首先,它使阅读他人的代码变得更容易,其次它更高效。