如何在Assembly中不使用div来查找余数

Sta*_*ovy 2 x86 assembly

我想知道如果不能使用div操作数,如何找到整数的余数.例如:

mov eax, 400    ; 400 / 4 
shr eax, 2      ; Remainder : 0


mov eax, 397    ; 397 / 4
shr eax, 2      ; Remainder : 1


mov eax, 394    ; 394 / 4
shr eax, 2      ; Remainder : 2
Run Code Online (Sandbox Code Playgroud)

移动截断剩余部分.

因此,如果不使用div(存储其余部分edx),可以做些什么来弄清楚其余部分是什么?

mat*_*ely 5

如果你只是想在除以二次幂时的余数,就像在你的例子中一样,那么余数实际上只是输入的低位(你移出的那些位).因此,应用位掩码运算符,您可以立即检索余数而无需进一步算术.

在microsoft x86汇编语言中,我相信你的第一个例子看起来像:

mov eax, 400    ; 400 / 4 
and eax, 3h     ; Masking with hex 3 (the lowest two bits) retrieves the remainder
Run Code Online (Sandbox Code Playgroud)