TRUE OR FALSE
first wut is boolean? boolean is just all yap about true or false tbh, We will use 0 as shorthand for “false” and 1 as shorthand for “true”.
AND
AND is true if both of the inputs are true.
- 0 AND 0 = 0
- 0 AND 1 = 0
- 1 AND 0 = 0
- 1 AND 1 = 1
OR
OR is true if either input is true.
- 0 OR 0 = 0
- 0 OR 1 = 1
- 1 OR 0 = 1
- 1 OR 1 = 1
XOR
Exclusive-OR is true if only one input is true.
- 0 XOR 0 = 0
- 0 XOR 1 = 1
- 1 XOR 0 = 1
- 1 XOR 1 = 0
NOT
NOT flips the value to the opposite value
- NOT 1 = 0
- NOT 0 = 1
Boolean logic in C
Logical operators
In C “logical” boolean operations, which operate over a statement and are interpreted as true if the result is non-zero or false if the result is zero.
| |
Bitwise operators
In C “bitwise” boolean operations, the boolean operation is computed individually on each bit in the same index of the two operands. So if you’re doing a bitwise AND of two variables, it is computed as:
| |
| |
| |
| |
We can see the new instructions AND, NOT, OR, and XOR!
AND - Bitwise AND
C binary operator & not && that’s logical AND. Destination operand can be r/mX or register, source operand can be r/mX or register or immediate. No source and destination as r/mXs.
| |
Btw we calculated this by vertically comparing them, left to right, 1 to 1 is 1, 1 to 0 is 0, etc. because AND is only if both inputs are true.
AND al, 0x42
| |
OR - Bitwise OR
C binary operator | not || that’s logical OR. Destination operand can be r/mX or register, source operand can be r/mX or register or immediate, no source and destination as r/mXs. Calculation of this is if either is true.
OR al, bl
| |
OR al, 0x42
| |
XOR - Bitwise Exclusive OR
C binary operator ^. Destination operand can be r/mX or register, source operand can be r/mX or register or immediate, no source and destination as r/mXs. XOR is commonly used to zero a register, by XORing it with itself because it’s faster than a MOV. So frequently compiler will generate a XOR of a register with itself in order to zero that register.
| |
All 0s because it’s only true if one input is true.
NOT - One’s Complement Negation
C unary operator ~ not ! that’s logical NOT. Single source/destination operand can be r/mX. NOT only flips the bits. That’s all!
For Loops
C code:
| |
Disassembled:
| |
INC/DEC (increment/decrement) (add/sub)
Single source/destination operand can be r/mX. Increase or decrease the value by 1. When optimized, compiler will tend to favor not using inc/dec that’s why we’re seeing add and sub in Binary Ninja because it’s directed by the Intel optimization guide. So their presence may be indicative of hand-written or un-optimized code. This modifies OF, SF, ZF, AF, PF, and CF flags.
| |
xor rax, rax
| |
inc rax
| |
Another example:
| |
mov rax, 0
| |
dec rax
| |