首页 文章

x86 asm中NOT指令的简单示例

提问于
浏览
3

有人能解释一下x86汇编程序中的NOT指令到底是什么吗?

在我所知道的编程语言中,NOT用于检查特定状态是否为真(例如: if (!Isset($var))) .

但是在Assembler中,运算符似乎做了其他事情,而我并不完全理解操作数的用途 .

有人可以用一个简单的例子解释这个操作吗?

1 回答

  • 4

    x86 NOT是按位操作;它只是分别反转每个位,如 xor reg, -1 但不影响FLAGS .

    NOT implements C's ~ operator, and is totally different from ! (logical not).

    以下是C编译器如何实现这些运算符(gcc8.1和clang6.0用于x86-64 System V调用约定,on the Godbolt compiler explorer) . 两个编译器生成相同的代码,正确地为现代Intel / AMD CPU选择最有效的实现 .

    int bitnot(int a) { return ~a; }
    
        mov     eax, edi
        not     eax
        ret
    
    int logical_not(int a) { return !a; }
    
        xor     eax, eax
        test    edi, edi
        sete    al
        ret
    
    int booleanize(int a) { return !!a; }
    
        xor     eax, eax
        test    edi, edi
        setne   al
        ret
    

相关问题