; input/ouput: eax
; destroys: ebx
test %eax, %eax ; x == 0?
jz 1f
leal -1(%eax), %ebx ; ebx := x-1
test %eax, %ebx ; ZF := (eax & ebx == 0)
setz %al
movzx %al, %eax ; eax := ZF
1:
We can use also BSF and BSR instructions, that determines position of first and last bit=1. If number is power of 2, then just one bit is set, and thus these positions are equal. BSx sets also ZF flag if input value is zero.
; input/output: eax
; destroys: ebx, edx
bsf %eax, %ebx ; ebx := LSB's position if eax != 0, ZF = 1 if eax = 0
jz 1f
bsr %eax, %edx ; edx := MSB's position
cmp %ebx, %edx ; ZF := (ebx = edx)
setz %al
movzx %al, %eax ; eax := ZF
1:
Chyba się pośpieszyłem mówiąc, że nie piszesz nowych snippetów, czemu tego nie ma na stronie :P
OdpowiedzUsuńBartek
Ten komentarz został usunięty przez autora.
OdpowiedzUsuń