如何检查寄存器是否可以被 7 整除?
How can I check if a register is evenly divisible by 7?
我想检查寄存器ax的值是否能被7整除,余数=0,我该怎么做?
您可以使用与 this answer 中所示相同的方式,当然使用不同的常量。
7 mod 216 的 mod 元乘法逆元是 0x6db7,这(根据定义)意味着 [=11 形式的数字=] 将服从 x * 0x6db7 = n
,其中 n
将小于或等于 0xffff / 7
。所以如果你从 x * 0x6db7
中得到更大的东西,你知道它不是 7 的倍数。此外,非 7 的倍数也不能映射到低结果,因为乘以奇数 modulo a 2 的幂是双射的。
所以你可以使用(未测试)
imul ax, ax, 0x6db7
cmp ax, 0x2492
ja not_multiple_of_7
这当然是针对无符号数。
作为奖励,ax
将是原始值除以 7 iff 它是 7 的倍数。
org 100h
mov ax,14
mov cl,7
div cl
cmp ah,0
je positive
PRINTN "The number has a remainder"
jmp finish
positive:
PRINTN "The number has no remainder"
finish:
PRINTN "After comparison"
mov ah, 0
int 16h
ret
我想检查寄存器ax的值是否能被7整除,余数=0,我该怎么做?
您可以使用与 this answer 中所示相同的方式,当然使用不同的常量。
7 mod 216 的 mod 元乘法逆元是 0x6db7,这(根据定义)意味着 [=11 形式的数字=] 将服从 x * 0x6db7 = n
,其中 n
将小于或等于 0xffff / 7
。所以如果你从 x * 0x6db7
中得到更大的东西,你知道它不是 7 的倍数。此外,非 7 的倍数也不能映射到低结果,因为乘以奇数 modulo a 2 的幂是双射的。
所以你可以使用(未测试)
imul ax, ax, 0x6db7
cmp ax, 0x2492
ja not_multiple_of_7
这当然是针对无符号数。
作为奖励,ax
将是原始值除以 7 iff 它是 7 的倍数。
org 100h
mov ax,14
mov cl,7
div cl
cmp ah,0
je positive
PRINTN "The number has a remainder"
jmp finish
positive:
PRINTN "The number has no remainder"
finish:
PRINTN "After comparison"
mov ah, 0
int 16h
ret