Skip to content

REVIEW 2 Language of the Machine

:material-circle-edit-outline: 约 1250 个字 :material-clock-time-two-outline: 预计阅读时间 4 分钟

对应wdCO第四章,还没看过

Concepts

Instruction formats

image-20240406120832464

RISC-V Registers

  • RISC-V has a 32 × 64-bit register file

    • 64-bit data is called a “doubleword
      • 32-bit data is called a “word
    • 32 x 64-bit general purpose registers x0 to x31

image-20240319191621748

  • x0可以用于运算,但不会改变,永远是0

Operations

Arithmetic Operations

  • Only one operation per instruction
    • Add and subtract, three operands
  • Two sources and one destination
  • add a, b, c // a gets b + c
    • a, b, c is address where stores number
      • 用地址速度更快
      • 数据都存在寄存器里
    • All arithmetic operations have this form

image-20240319190806982

image-20240319190813394

Memory Operands

  • Memory is byte addressed

    • Each address identifies an 8-bit byte
    • 64位 = 8byte,占8个地址为一组

RISC-V does not require words to be aligned in memory

即一个地址一定指向8byte的空间,这是最小单元,不可分割

  • RISC-V is Little Endian
    • 小端模式
      • 如果一个数需要存储于多个byte,那么我们将小的位存放在小的地址指向的byte
      • Least-significant byte at least address of a word

Big Endian: most-significant byte at least address

地址编号小的是小地址,大的是大地址。也成为高低地址

数据地位就是小的位,略

注意,一个储存单位是1byte,有8bit

每个byte里面位的顺序不受大端/小段模式的影响

是byte的顺序受影响

读取一定是从小地址开始读取,区别是从低往高放,还是从高往低放

Memory Operand Example

ld和sd是内存操作指令

image-20240321190323030

三行汇编,x开头的都是寄存器。

因为double word - 8byte一个最小单元空间,所以矩阵中的第8个空间用8*8=64表示;96同理。

偏移量单位是byte。读取的时候也是以byte为单位。

Constant or immediate Operands

  • 立即数 Immediate
    • Avoids the load instruction
    • Constant zero: a register x0
    • 需要再指令后面加个 i
      • addi
      • 类似的有无符号数 addu

Signed and unsigned numbers

所有指令后面都可以加个 u,表示无符号运算

Sign Extension

因为最小储存单元为byte,如果要拓展储存空间,就会有大量空闲bit

  • 无符号数就填充0
  • 有符号数正0负1

image-20240406122440040

下表汇总了上面的内容,包括32个reg,双字下可用的的内存,五个指令

image-20240321191624196

\(2^{61}\)是指可以访问到的空间数量,是因为64位(字长64bit)寄存器多有多\(2^{64}\)个地址,然后因为CPU能访问的最小空间是双子(\(2^3\),1byte),得减去3个bit

Signed and unsigned numbers

所有指令后面都可以加个 u,表示无符号运算

image-20240406122800393

Sign Extension

因为最小储存单元为byte,如果要拓展储存空间,就会有大量空闲bit

  • 无符号数就填充0
  • 有符号数就填充1

image-20240406122440040

image-20240406122529312

Representing Instructions

注意我们学的都是RISC-V

  • All instructions in RISC-V have the same length 32-bit
  • RISC-V指令可分为以下几类
    • R 型:Reg与Reg的交互
    • I 型:Imm到Reg的交互
    • S 型:Store,内存交互

R-Format Instructions

R 型指令名称的由来是“Register-to-Register”的缩写,即“寄存器到寄存器”。

image-20240321194718909

  1. opcode: operation code
  2. rd: destination register number
  3. funct3: 3-bit function code (additional opcode)
  4. rs1: the first source register number
  5. rs2: the second source register number
  6. funct7: 7-bit function code (additional opcode)
  • opcode + funct3 + func7 = add
  • opcode \(\neq\) add, opcode > add
    • opcode $= $ add, sub, ...

R-format Example

包含add, sub等

\(opcode=51\)

image-20240321195608204

I-Format Instructions

I 型指令名称的由来是“Immediate-to-Register”的缩写,即“立即数到寄存器”。

image-20240321195807284

与R型的区别就是funcy7, rs2被合并为了immediate作为新的二号操作对象

可见立即数容量是\(2^{12}\)

  • rs1: source or base address register number
  • immediate: constant operand, or offset added to base address
    • 2s-complement, sign extended

image-20240321200030775

RISC-V S-Format Instructions

S 型指令名称的由来是“Store”的缩写,即“存储”,用于将寄存器中的数据存储到内存中。

image-20240321200935039

  • rs1: base address register number
  • rs2: source operand register number
  • immediate: offset added to base address
    • Split as [15:11] and [4:0] so that rs1 and rs2 fields always in the same place

与下图的R型对比,可见是funct7, rd被替换为了imm

image-20240321194718909

目标是内存,所以没有rd(reg)

image-20240321201046040

RISC-V fields (format)

总表

image-20240321201847606

image-20240321201452798

Logical Operations

image-20240321202347339

Shift

image-20240406125207511

  • imm: how many positions to shift

不管左移右移都是用0填充

用来进行乘法与除法

  • slli
    • shift left
    • multiplies by \(2^i\)
  • srli
    • shift right
    • devides by \(2^i\)

AND

and 可以用于 提取/选取 数据

Select some bits, clear others to 0

image-20240406125645211

OR

or则可以对一些0数据进行修改

Set some bits to 1, leave others unchanged

image-20240406125719516

XOR

用于比较两段数据的不同

image-20240406125900438

image-20240321202840448

Instructions for making decisions

  • Branch instructions
    • Branch to a labeled instruction if a condition is true
    • Otherwise, continue sequentially
  • Example
    • beq rs1, rs2, L1 if (rs1 == rs2) branch to instruction labeled L1
    • bne rs1, rs2, L1 if (rs1 != rs2) branch to instruction labeled L1

beq, bnq可以实现以下逻辑

各类逻辑的实现

if

image-20240406130759943

if-then-else

image-20240406130749056

关键点是beq x0, x0, EXIT

LOOPs

image-20240406130728401

while

image-20240406130816575

More Conditional Operations

  • blt rs1, rs2, L1 if(rs1<rs2) L1
    • 大于需要用EXIT实现

image-20240406131136504

  • bge rs1, rs2, L1 if(rs1\(\ge\) rs2) L1
    • 同上

Jump

jalr即无判断直接跳转到寄存器

对于jalr x1, 100(x6)x1是返回地址,因为循环完还得出来,这个地址就是告诉程序回到哪;100(x6)表示要跳到哪

image-20240321204319567

switch/case 逻辑就用 jump address table

EXAMPLE

image-20240326192955996

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-20240406131710071

Supporting Procedures

lecture 5