Skip to content

Lecture 6 | The Processor-Part1

:material-circle-edit-outline: 约 633 个字 :fontawesome-solid-code: 15 行代码 :material-clock-time-two-outline: 预计阅读时间 2 分钟

Chap2后半部分+Chap4前部分

chap2

2.12 Translating and starting a program

image-20240404154717937

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

image-20240404155026929

gp指向data segment的起始位置

将 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)

image-20240404155638431

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
    1. Allocate registers to program variables
      • 分配寄存器给变量
    2. Produce code for the body of the procedures
      • 根据C代码塞指令
    3. Preserve registers across the procedures invocation
      • ?

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

image-20240404161022301

swap is a leaf procedure, nothing to preserve

image-20240404161119303

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

image-20240404161329259

The Outer Loop

image-20240404161441766

li 是伪代码,load immediately,没有这个指令

The Inner Loop

image-20240404161521993

Preserving Registers

image-20240404161604148

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

image-20240404161910396

*2.16 Real Stuff: MIPS Instructions

精简指令集架构

MIPS: commercial predecessor to RISC-V

2.17 Real Stuff: The Intel x86 ISA

复杂指令集架构

Fallacies

  1. Powerful instruction → higher performance
    • Compilers are good at making fast code from simple instructions
  2. Use assembly code for high performance
    • modern compilers are better at dealing with modern processors
    • More lines of code Þ more errors and less productivity
  3. Backward compatibility → instruction set doesn’t change

Pitfalls

  1. Sequential words are not at sequential addresses
    1. Increment by 4, not by 1!
  2. Keeping a pointer to an automatic variable after procedure returns
    1. Pointer becomes invalid when stack popped
    2. e.g., passing pointer back via an argument

Chap4 The Processor-Part1

也是重点

单周期和流水线处理器

如何实现读取指令与运行

Instruction Execution Overview

  1. For every instruction, the first two step are identical
    1. Fetch the instruction from the memory
    2. Decode and read the registers
  2. Next steps depend on the instruction class
    1. Memory-reference
    2. Arithmetic-logical
    3. branches

image-20240404165003332

An overview of Implementation

image-20240404164845301