目录
History of Intel processors and architectures
- Complex instruction set computer (CISC)
– Many different instructions with many different formats
– But, only small subset encountered with Linux programs - Hard to match performance of Reduced Instruction Set Computers
(RISC)
– But, Intel has done just that!
– In terms of speed. Less so for low power.
C, assembly, machine code
Definitions:
- Architecture: The part of a processor design that one needs to understand or write assembly/machine code.
- Microarchitecture: Implementation of the architecture
Assembly/Machine Code View
Programmer - Visible State
- PC: Program counter
–Address of next instruction
–Called “RIP” - Register file
–Heavily used program data - Condition codes
– Store status information about most recent
arithmetic or logical operation
– Used for conditional branching - Memory
– Byte addressable array
– Code and user data
– Stack to support procedures
Turning C into Object Code
Compiling Into Assembly
gcc -Og -S Test.c
Data Types
- “Integer” data of 1, 2, 4, or 8 bytes
– Data values
– Addresses (untyped pointers) - Floating point data of 4, 8, or 10 bytes
- Code: Byte sequences encoding series of instructions
- No aggregate types such as arrays or structures
– Just contiguously allocated bytes in memory
Operations
- 在寄存器或者内存的数据上做运算操作
- 传输数据在内存和寄存器之间
- 传输控制
– 无条件跳转
– 条件分支
Object Code
Assembler
- Translates .s into .o
- Binary encoding of each instruction
- Nearly-complete image of executable code
- Missing linkages between code in different files
Linker
- Resolves references between files
- Combines with static run-time libraries
– E.g.,codeformalloc,printf - Some libraries are dynamically linked
– Linking occurs when program begins execution
Assembly Basics: Registers, operands, move
X86-64 Integer Register
Moving Data
movq Source,Dest:
Operand Types
- Immediate: Constant integer data
- Example: $0x400, $-533
- Like C constant, but prefixed with ‘$’
- Encoded with 1, 2, or 4 bytes
- Register: One of 16 integer registers
- Example: %rax, %r13
- But %rsp reserved for special use
- Others have special uses for particular instructions
- Memory: 8 consecutive bytes of memory at address given by register
- Simplest example: (%rax)
- Various other “address modes”
movq Operand Combinations
不能做内存到内存到转换通过将单一指令
Simple Memory Addressing Modes
Example
Complete Memory Addressing Modes
Most General Form
D(Rb,Ri,S)
Mem[Reg[Rb]+S*Reg[Ri]+ D]
D: Constant “displacement” 1, 2, or 4 bytes
Rb: Base register: Any of 16 integer registers
Ri: Index register: Any, except for %rsp
S: Scale: 1, 2, 4, or 8 (why these numbers?)
Special Cases
(Rb,Ri)==>Mem[Reg[Rb]+Reg[Ri]]
D(Rb,Ri) ==>Mem[Reg[Rb]+Reg[Ri]+D]
(Rb,Ri,S)==> Mem[Reg[Rb]+S*Reg[Ri]]
Arithmetic & logical operations
Address Computation Instruction
leaq Src, Dst
- Src is address mode expression
- Set Dst to address denoted by expression
Uses
- Computing addresses without a memory reference
– E.g., translation of p = &x[i]; - Computing arithmetic expressions of the form x + k*y
– k = 1, 2, 4, or 8
Some Arithmetic Operations
Two Operand Instructions:
Arithmetic Expression Example
Summary
History of Intel processors and architectures
Evolutionary design leads to many quirks and artifacts
C, assembly, machine code
- New forms of visible state: program counter, registers …
- Compiler must transform statements, expressions, procedures into low-level instruction sequences
Assembly Basics: Registers, operands, move
- The x86-64 move instructions cover wide range of data movement forms
Arithmetic
- C compiler will figure out different instruction combinations to carry out computation