Instructions Flashcards

1
Q

mov

A

移动数据,寄存器到寄存器,寄存器到内存,内存到寄存器,立即数到寄存器,立即数到内存。

不改变标志寄存器。

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

lea

A

数据的地址加载到寄存器。不是数据本身。

不改变标志寄存器。

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ADD reg/mem, reg/mem/imm

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Tricks and Traps for ADD: 1

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Tricks and Traps for ADD: 2

A

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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Tricks and Traps for ADD: 3

A

Using “Add reg, 1” if need “INC reg” which affects the carry flag.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Tricks and Traps for ADD: 4

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Sign Extension for ADD

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

SUB reg/mem, reg/mem/imm

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Tricks for SUB: 1

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Tricks for SUB: 2

A

A quick way to Zero is “SUB reg, reg”. “XOR reg, reg” is more common for this purpose.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Tricks for SUB: 3

A

Use “SUB x, 1” to achieve a “DEC x” which affects the carry flag.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Tricks for SUB: 4

A

You can use ADD and SUB to perform a swap, similar to XOR swap, but XCHG instruction is faster and easier to read.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

INC and Carry Flag

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

DEC

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly