I’m trying to write a (very) short assembly routine which tests for equality of two dwords and returns a boolean value (1 = true, 0 = false). So far I’ve come up with three methods, one of which uses LAHF which apparently isn’t supported on some x86_64 processors, so that one is out unfortunately of the question.
Version one is:
mov eax, [esp + 8]
cmp b, [esp + 4]
mov eax, 1
jnz jpt
mov eax, 0
jpt: ret
and version two is:
mov eax, [ebp + 8]
cmp b, [ebp + 4]
pushf ; Get lowest word of the flags register
pop ax
and eax, 0x0040 ; Extract the zero flag
shr eax, 6 ; eax is now true(1) if arg1 == arg2
ret
Version one has an extra branch instruction, but version two has an extra push and an extra pop instruction. Which one would you expect to be fastest and why? Is this dependent of if the branch would be taken/predicted or not?
Both are version are bad. A random branch takes ages to execute, because it can’t be predicted and lahf is just a no no because of a partial register write. But of course, writing a test for equality in assembler is complete nonsense anyway, because the function overhead will be a multiple of the equivalent instructions inline, so here I go: