3 ms·
but you can actually implement ! in just three lines of assembly I was actually expecting the "classic" x86 idiom for !, which also happens to be 3 instruction
by userbinator 9y ago
but you can actually implement ! in just three lines of assembly
I was actually expecting the "classic" x86 idiom for !, which also happens to be 3 instructions, but is slightly shorter and likely faster than the sequence presented:
neg eax
sbb eax, eax
inc eax
As a bonus, it also works in 16-bit mode (using the 16-bit registers) on everything back to the 8086, since SETcc is 386+, and given that I've seen it in early DOS compiler output, this neg/sbb/inc idiom has likely been known for a very long time. Figuring out how it works is left as an exercise for the reader ;-)
- cwzwarich 9y agoThe code there is probably faster, because the mov eax, 0 will be handled during register renaming and not actually issued. It will also eliminate the partial dependency of SETE on the other bits of EAX.