Skip to content

Lecture 5

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

240326

!!! bug “我真的服了” 这个老师会传统点名

接上周没讲完的 jump 跳转

Jump

Basic Blocks

跳转让优化变得很困难,因为跳转是未知的、不确定的。

为了方面预测并优化,将指令集合分为多个basic block,每个block里面不会进行jump,还有一些其它的性质:

  • A basic block is a sequence of instructions with
    • No embedded branches (except at end)
    • No branch targets (except at beginning)

image-20240326193313016

2.8 Supporting Procedures

Procedure/function

  • Procedure/function be used to structure programs
    • A stored subroutine that performs a specific task based on the parameters with which it is provided
    • Six step
  1. Place Parameters in a place where the procedure can access them
  2. Transfer control to the procedure(jump to)
  3. Acquire the storage resources needed for the procedure
  4. Perform the desired task
  5. Place the result value in a place where the calling program can access it
  6. Return control to the point of origin

Procedure Call Instructions

  • Instruction for procedures: jal ( jump-and-link )
    • Address of following instruction put in x1
    • Jumps to target address
Caller jal x1, ProcedureAddress
  • Procedure return:jalr (jump and link register)
    • Like jal, but jumps to 0 + address in x1
    • Use x0 as rd (x0 cannot be changed)
    • Can also be used for computed jumps
Callee jalr x0, 0(x1)

Using More Registers

  • Registers for procedure calling
    • a0 ~ a7(x10-x17): eight argument registers to pass parameters & return values
    • ra/x1:one return address register to return to origin point
  • Stack
    • ideal data structure for spilling registers
    • Stack pointer sp
  • Stack grow from higher address to lower address
    • 约定起始位置在高位,往低位扩张
    • Push: sp= sp - 8 addi sp,sp,-8 sd …,8(sp)
    • Pop: sp = sp + 8 ld …,8(sp) addi sp,sp,
    • 8 是字长

image-20240326194338299

Example

这里没听没看

image-20240326194504182

下面虚线部分是运算部分,可以忽略

image-20240326194921829

记成tx是方便,就一个临时记号

image-20240326195414142

Nested Procedures

下面都没看

嵌套程序

image-20240326195859660

image-20240326195918007

image-20240326195923996

一个stack不够用,用多个stack

image-20240326200256336

Memory Layout

image-20240326200418824

Local Data on the Stack

image-20240326200813995

RISC-V assembly language

这张表经常要查

image-20240326200851781

image-20240326200950507

2.9 Communicating with People Character Data

  • Byte-encoded character sets
    • ASCII ( American Standard Code for Information Interchange )
  • Unicode: 32-bit character set

Byte/Halfword/Word Operations

image-20240326201124621

String Copy Example

image-20240326201228004

image-20240326201403306

2.10 Addressing for 32-Bit Immediate and Addresses

image-20240326201727932

image-20240326201750655

Addressing

Branch Addressing

image-20240326201851474

Jump Addressing

image-20240326201926943

Show branch offset in machine language

image-20240326201949330

Instructions Addressing and their Offset

image-20240326202005729