Instructions Flashcards
mov
移动数据,寄存器到寄存器,寄存器到内存,内存到寄存器,立即数到寄存器,立即数到内存。
不改变标志寄存器。
lea
数据的地址加载到寄存器。不是数据本身。
不改变标志寄存器。
ADD reg/mem, reg/mem/imm
The add instruction adds the source to the destination and stores in the destination. It is used for both signed and unsigned arithmetic, depending on how the flags are used.
The overflow flag indicates a signed carry. The Carry flag indicates unsigned carry.
Flags: Overflow, Carry, Sign, Zero, Auxiliary Carry, Parity LOCK is supported.
Tricks and Traps for ADD: 1
Using the original registers, EAX, CX, BL etc. generates more efficient machine code than using the new ones, R8B, R12W etc. New registers, and 64 bit instructions add a REX prefix in machine code.
Tricks and Traps for ADD: 2
Use “Add reg,0” to update the flags according to reg without changing it, similar to “AND reg, reg” and “OR reg, reg”. These are slightly different to “CMP reg, reg” since CMP sets the flags as “SUB reg, reg”.
Tricks and Traps for ADD: 3
Using “Add reg, 1” if need “INC reg” which affects the carry flag.
Tricks and Traps for ADD: 4
ADD is faster than the multiplication instructions, so to do double a reg, you can use for example “ADD ax, ax”. It is more common to use shifts for this purpose.
Sign Extension for ADD
ADD sign extends an immediate operand when the operands are not of the same size. This only matters when the destination is 64 bits, but there is no “ADD reg64, imm64”!
When the destination is 64 bits, the immediate is always read as 32 bits. It will be sign extended, so if the 31st bit is a 1, you’ll get a negative result!
ADD RCX, 2147483648; add rcx, ffffffff80000000h
If you need to add a 64 bit immdiate to a reg or mem, you have to use an immediate MOV instruction:
MOV rax, 2147483648
ADD rcx, rax
SUB reg/mem, reg/mem/imm
SUB substracts the second operand from the first, and stores the result in the first.
The Overflow flag indicates a signed borrow. The Carry flag indicates unsigned borrow.
Flags: Overflow, Carry, Sign, Zero, Auxiliary Carry, Parity LOCK is supported.
Tricks for SUB: 1
64bit SUB is similar to ADD in the way it sign extends a 32 bit immediate. If you need to subtract a 64 bit immediate, you have to use a MOV first!
Tricks for SUB: 2
A quick way to Zero is “SUB reg, reg”. “XOR reg, reg” is more common for this purpose.
Tricks for SUB: 3
Use “SUB x, 1” to achieve a “DEC x” which affects the carry flag.
Tricks for SUB: 4
You can use ADD and SUB to perform a swap, similar to XOR swap, but XCHG instruction is faster and easier to read.
INC and Carry Flag
The INC and DEC instructions does not change the carry flag. I believe this has to do with the “LOOP” instruction. So if you need to check the carry flag after an INC, you should use “ADD x, 1” instead.
Most of the time, “INC reg” is the same speed as “ADD reg, 1”, but for 32 and 64 bit regs, “INC” is smaller (3 bytes vs 4), and in rare occasions where you need a loop’s body fit into some specific number of bytes, INC might be the best choice.
DEC
The DEC, short for Decrement, instruction substructs 1 from the destination.
DEC reg/mem
The DEC instruction does not change the carry flag! same as INC. Use “SUB x, 1” to affect the carry.
Flags: Overflow, Sign, Zero, Auxiliary and Parity.