Probably the "simplest" or simplest, "not caring for details" answer, how to determine:
; here ebx is some value, flags are set to anything test ebx,ebx ; CF=0, ZF=0/1 according to ebx jz whereToJumpWhenZero ; "non-zero ebx" will go here ; Or you can use the inverted "jnz" jump to take ; a branch when value was not zero instead of "jz".
Here is a detailed answer from Peter Cordes to " testl eax vs eax?" question about flags set, etc. There is also a link to another similar answer, but a discussion about why this is the best way in terms of performance. :)
How to set another register (I will choose eax ) to 1 when ebx is zero, and to 0 when ebx not zero (non-destructive way for ebx itself):
xor eax,eax ; eax = 0 (upper 24 bits needed to complete "al" later) test ebx,ebx ; test ebx, if it is zero (ZF=0/1) setz al ; al = 1/0 when ZF=1/0 (eax = 1/0 too)
Or how to convert ebx itself to 1/0 when ebx is zero / non-zero:
neg ebx ; ZF=1/0 for zero/non-zero, CF=not(ZF) sbb ebx,ebx ; ebx = 0/-1 for CF=0/1 inc ebx ; 1 when ebx was 0 at start, 0 otherwise
Or how to convert ebx itself to 1/0 when ebx is zero / non-zero, another option (faster on "P6" kernels to "Haswell"):
test ebx,ebx ; ZF=1/0 for zero/non-zero ebx setz bl ; bl = 1/0 by ZF (SETcc can target only 8b r/m) movzx ebx,bl ; ebx = bl extended to 32 bits by zeroes
etc. etc. It depends on what happens before your testing, as well as what you really want as a test result, there are many possible ways (optimal for different situations and optimal for another target CPU).
I will add some more extremely common situations ... Intermediate counter counter from N to zero, per cycle N times:
mov ebx,5 ; loop 5 times exampleLoop: ; ... doing something, preserving ebx dec ebx jnz exampleLoop ; loop 5 times till ebx is zero
How to process 5 elements of word (16b) array (access to them in array [0], array [1], ... order):
mov ebx,-5 lea esi,[array+5*2] exampleLoop: mov ax,[esi+ebx*2] ; load value from array[i] ; process it ... and preserve esi and ebx inc ebx jnz exampleLoop ; loop 5 times till ebx is zero
Another example, I somehow have so many:
How to set the target register ( eax in the example) to ~ 0 (-1) / 0 when ebx is zero / non-zero, and you already have the value 1 in some register ( ecx in the example):
; ecx = 1, ebx = some value cmp ebx,ecx ; cmp ebx,1 => CF=1/0 for ebx zero/non-zero sbb eax,eax ; eax = -1 (~0) / 0 for CF=1/0 ; ebx/ecx intact
-1 may look as practical as 1 (for indexing purposes, at least), but -1 works just like a full bitmask for further and/xor/or operations, so sometimes it's more convenient.