【发布时间】:2012-12-12 16:24:52
【问题描述】:
我知道在类 C 语言中,我可以不使用 not 运算符执行布尔运算:(C)
int myBool = TRUE;
myBool = !myBool;
但我的问题是:这是如何在幕后实现的?我的猜测是使用跳转,但如果过度使用这些可能效率低下:(Intel x86 语法)
; assume eax holds boolean
test eax, eax
jnz short boolTrue
inc eax ; eax was 0, now 1
jmp short after
boolTrue: ; eax non-zero
xor eax, eax ; eax now 0
after:
如图所示,它需要 5 条指令,其中至少有一个跳转和一个逐位and (test)。必须有一种更简单的方法来做到这一点,因为我已经看到出于某种奇怪的原因执行“双重非”(if (!!fileHandle)) 的代码库。
所以(如上所述):编译器如何在 x86 上执行布尔 !s?
【问题讨论】:
-
通常你甚至不会“拥有”一个布尔值,它会成为标志的一部分。
标签: assembly compilation x86 boolean-logic