It’s time to rehearse some logic theory. Don’t panic, because we will look at only what we need: NOT, OR, XOR, and AND.
In this chapter, 0 means false, and 1 means true.
NOT
A | 0 | 1 |
NOT A | 1 | 0 |
Convert every 0 into 1 and every 1 into 0.
OR
A | 0 | 1 | 0 | 1 |
B | 0 | 0 | 1 | 1 |
A OR B | 0 | 1 | 1 | 1 |
If there is a 1 in A or B or in both, the outcome is a 1.
XOR
A | 0 | 1 | 0 | 1 |
B | 0 | 0 | 1 | 1 |
A XOR B | 0 | 1 | 1 | 0 |
Exclusive OR: If there is a 1 in A or B, the outcome is a 1. If A and B are both 1 or 0, the outcome is 0.
Hence, xor rax, rax is the same is mov rax,0. But xor executes faster than mov.
You can also use xor to modify the sign of a floating-point number.
Use the tool at www.binaryconvert.com/result_float.html to verify this.
Note that if you want to change the sign of an integer, subtract it from zero or use the neg instruction .
AND
A | 0 | 1 | 0 | 1 |
B | 0 | 0 | 1 | 1 |
A AND B | 0 | 0 | 0 | 1 |
If there is a 1 in A and in B, the outcome is a 1; otherwise, it’s 0.
The AND instruction can be used as a mask to select and investigate bits.
Here we conclude that bit 6 is set and bit 3 is not set. I’ll talk more about that later.
The AND instruction can also be used to round down numbers, and it is especially useful to round down addresses on a 16-byte boundary. We will use this later to align stacks.
Here we rounded down the lowest byte of the address. If the address already ends in a zero byte, the and instruction would not change anything. Verify that the rounded address is divisible by 16. Use an online utility to do the conversion (e.g., www.binaryconvert.com/convert_unsigned_int.html ).
Summary
Logical operators
How to use logical operators as assembly instructions