I was wondering how cmp
is used outside of saving a byte on doing a zero-check on a register, for example:
cmp $0, %eax
vs.
test %eax, %eax
I was doing some trial-and-error and it seems like this is its main usage (unless I'm missing something -- if any further usage can use the and
instruction).
Are there other uses of the test
instruction?
Somewhat related: In x86 what's difference between "test eax,eax" and "cmp eax,0".
Here are some samples I did:
mov $1, %eax
mov $2, %ebx
test %eax, %ebx
jg _start # <-- doesn't jump
jl _start # <-- doesn't jump
mov $1, %eax
mov $1, %ebx
test %eax, %ebx
je _start # <-- doesn't jump
mov $0, %eax
mov $1, %ebx
test %eax, %ebx
je _start # <-- why is this the only one that works? 1 & 0 == 0 ?