Lecture 6 | The Processor-Part1
Chap2后半部分+Chap4前部分
chap2
2.12 Translating and starting a program
Producing an Object Module
Assembler (or compiler) translates program into machine instructions
-
Provides information for building a complete program from the pieces
- Header: described contents of object module
- Text segment: translated instructions
- 就是程序本身,包含程序的所有指令
- Static data segment: data allocated for the life of the program
- Relocation info: for contents that depend on absolute location of loaded program
- Symbol table: global definitions and external refs
- Debug info: for associating with source code
gp指向data segment的起始位置
Link
将 Object modules(including library routine) → executable program
- 3 steps of Link
- Place code and data modules symbolically in memory
- 是什么东西就放到什么位置
- Determine the addresses of data and instruction labels
- 重定位
- Patch both the internal and external references (Address of invoke)
- Place code and data modules symbolically in memory
Loading a Program
- Load from image file on disk into memory
- Read header to determine segment sizes
- Create virtual address space
- Copy text and initialized data into memory
- Or set page table entries so they can be faulted in
- Set up arguments on stack
- Initialize registers (including sp, fp, gp)
- Jump to startup routine
Dynamic Linking
对于很大的程序,不用一开始都链接好,要用时再链接
Only link/load library procedure when it is called
Lazy Linkage
2.13 A C Sort Example To Put it All Together
这个程序用于复习本章的内容
- Three general steps for translating C procedures
- Allocate registers to program variables
- 分配寄存器给变量
- Produce code for the body of the procedures
- 根据C代码塞指令
- Preserve registers across the procedures invocation
- ?
- Allocate registers to program variables
Procedure swap
void swap ( long long v[ ] , size_t k )
{ //sway k and k+1
long long temp ;
temp = v[ k ] ;
v[ k ] = v[ k + 1 ] ;
v[ k + 1 ] = temp ;
}
Allocate registers to program variables
swap is a leaf procedure, nothing to preserve
Procedure Sort
冒泡排序
这就是考试题目
void sort (long long v[ ] , size_t n )
{
size_t i , j ;
for ( i = 0 ; i < n ; i + = 1 ) {
for ( j = i - 1 ; j >= 0 && v[j] > v[j+1] ; j -= 1 )
swap ( v , j ) ;
}
}
Register allocation for sort
The Outer Loop
li 是伪代码,load immediately,没有这个指令
The Inner Loop
Preserving Registers
2.14 Arrays vs. Pointers
指针比array更高效
- Array indexing involves
- Multiplying index by element size
- Adding to array base address
- Pointers correspond directly to memory addresses
指针可以直接操作物理地址,而数组需要计算出地址偏移量
Compiler can achieve same effect as manual use of pointers
编译器会优化数组的操作
Example: Clearing an Array
*2.16 Real Stuff: MIPS Instructions
精简指令集架构
MIPS: commercial predecessor to RISC-V
2.17 Real Stuff: The Intel x86 ISA
复杂指令集架构
Fallacies
- Powerful instruction → higher performance
- Compilers are good at making fast code from simple instructions
- Use assembly code for high performance
- modern compilers are better at dealing with modern processors
- More lines of code Þ more errors and less productivity
- Backward compatibility → instruction set doesn’t change
Pitfalls
- Sequential words are not at sequential addresses
- Increment by 4, not by 1!
- Keeping a pointer to an automatic variable after procedure returns
- Pointer becomes invalid when stack popped
- e.g., passing pointer back via an argument
Chap4 The Processor-Part1
也是重点
单周期和流水线处理器
如何实现读取指令与运行
Instruction Execution Overview
- For every instruction, the first two step are identical
- Fetch the instruction from the memory
- Decode and read the registers
- Next steps depend on the instruction class
- Memory-reference
- Arithmetic-logical
- branches