Machine-Level Representation
Data Formats
Accessing information
- Operand Specifiers
- Data Movement Instructions
Arithmetic and Logical Operations
- Load Effective Address
The load effective address instruction leaq is actually a variant of the movq in- struction. It has the form of an instruction that reads from memory to a register, but it does not reference memory at all. Its first operand appears to be a mem- ory reference, but instead of reading from the designated location, the instruction copies the effective address to the destination. We indicate this computation in Figure 3.10 using the C address operator &S. This instruction can be used to gener- ate pointers for later memory references. In addition, it can be used to compactly describe common arithmetic operations. For example, if register %rdx contains value x, then the instruction leaq 7(%rdx,%rdx,4), %rax will set register %rax to 5x + 7. Compilers often find clever uses of leaq that have nothing to do with effective address computations. The destination operand must be a register. - Unary and Binary Operations
- Shift Operations
So, for example, when register %cl has hexadecimal value 0xFF, then instruction salb would shift by 7, while salw would shift by 15, sall would shift by 31, and salq would shift by 63.
- Special Arithmetic Operations
Control
- Condition Codes
Comparison and test instructions.
- Accessing the Condition Codes
- Jump Instructions
- Implementing Conditional Branches with Conditional Control
Disadvantage: a misprediction can incur a serious penalty, say, 15–30 clock cycles of wasted effort, causing a serious degradation of program performance. - Implementing Conditional Branches with Conditional Moves
Each of these instructions has two operands: a source register or memory location S, and a destination register R.
- Loops
jump to middle and guarded do
Procedures
- The Run-Time Stack
- GDB commands
pushq %rbp
等价于:
subq $8,%rsp #stack pointer -8
movq %rbp,(%rsp) #move %rbp onto the stack
有关栈的操作这里讲得很好。
看到下面这段代码和栈的示意图,很好懂。
_main:
push 3
push 2
call _add_a_and_b
add %esp, 8
ret
同理,pop %rbx
相当于movq (%rsp),%rbx
add %rsp,8
The pushq instruction both decrements the stack pointer by 8 and writes a register value to memory. It is therefore not totally clear what the processor should do when executing the instruction pushq %rsp, since the register being pushed is being changed by the same instruction. Two different conventions are possible: (1) push the original value of %rsp, or (2) push the decremented value of %rsp.