问题
For example:
int x = 35;
if( x%2==1) { //do something }
I just want to check the modulus value without assigning the result to x
.
Assume that value is in eax
, so I can use DIV
instruction, then put back the original value to eax
etc.. but that seems inefficient. Can you suggest a better way?
回答1:
To branch according to the modulo 2 of a value in al
/ax
/eax
/rax
:
test al,1
jnz is_odd
is_even:
; do something for even numbers.
is_odd:
; do something for odd numbers.
However, if all you want is the modulo value, you don't need any branches.
test al,1
setnz bl ; modulo 2 of al/ax/eax/rax is now in bl.
回答2:
If the low bit is on, it is not divisible by two:
test x, 1
jne somewhere_when_odd
来源:https://stackoverflow.com/questions/12764460/what-is-the-easiest-way-to-check-an-integers-remainder-for-modulus-2-in-nasm-as