跳转至

Chapter 2 | Instruction

约 866 个字 50 行代码 48 张图片 预计阅读时间 5 分钟

概述

  • Introduction
  • Operations of the computer hardware (计算机硬件的操作)
  • Operands of the computer hardware(计算机硬件的操作数)
  • Signed and unsigned numbers (有符号和无符号数)
  • Representing instructions in the computer(计算机中指令的表示)
  • Logical operations(逻辑操作)
  • Instructions for making decision(决策指令)
  • Supporting procedures in computer hardware(计算机对过程的支持)
  • Instruction addressing (指令的寻址)

image-20240329185607510

image-20240401143701122

image-20240408221320258

image-20240408221326449

信息量 Op > Func 3 > Func 6/7

image-20240408221334596

image-20240408221341825

image-20240408221349600

Mention

重点理解

  • Branch指令进行循环等操作
  • Procdure尤其是栈的写法
    • 递归,斐波那契
  • 区分jal jalr

jal 跳转是基于PC的跳转 UJ type

jalr 相当于jal的寄存器版本 跳转范围更加广 I type

PC + 4 都要存回去

  • 立即数的种类
  • 转换code的时候注意rs1,rs2的位置

Introduction

存储程序概念: 指令和多种类型的数据不加区分地存储在存储器中并因此易于更改,因为产生了存储程序计算机

Operation

  • 一条指令一个操作

image-20240318135127371

Operands

RISC - V 32 × 64-bit register file

word: 32 bits

double word : 64 bits

image-20240330224444337

image-20240330224636101

  • Arithmetic instructions use register operands 必须在寄存器
  • 以64bits的想法理解后续的所有操作

image-20240321112632612

image-20240321114211969

Register

image-20240321102943316

C code: f = (g + h) - (i + j); f, …, j in x19, x20, …, x23 Compiled

RISC-V code:
add x5, x20, x21 add x6, x22, x23 sub x19, x5, x6

Memory Operands

byte addressed

image-20240321103426071

little endian

  • 大端小端针对的是一个word, 在内存中有影响

  • Least-significant byte at least address of a word

  • c.f. Big Endian: most-significant byte at least address

0x 12345678 word index : 3210

32bits,4bytes

==> 78 is the lsb, 这个byte位于3

image-20240618225145806

word 对齐 Alignment

RISV - V不强制要求word aligned

struct {
    int a;
    char b;
    char c[2];
    char d[3]
    float e;
}

image-20240321104220823

example

image-20240321111203000

  • 能访问寄存器的只有 load store
  • 这里展示的是字节寻址(8 bits)

Constant or immediate operands

避免常数操作时load store 浪费时间

Offer versions of the instruction 
        addi   x22, x22, 4  // x22= x22+ 4 

Constant zero: a register x0

Representing Instructions

  • Mapping registers into numbers map registers x0 to x31 onto registers 0 to 31

  • RISC-V instructions

  • Encoded as 32-bit instruction words 每条指令一个word
  • Small number of formats encoding operation code (opcode), register numbers, …Regularity

  • sd A,B
  • ld

R-Format instructions

image-20240321113030366

  • opcode: operation code
  • rd: destination register number
  • funct3: 3-bit function code (additional opcode) 例如区分加减法
  • rs1: the first source register number
  • rs2: the second source register number
  • funct7: 7-bit function code (additional opcode)

example

I-Format Instructions

image-20240321114100493

  • load addi
Exampleld x9, 64(x22)
22 (x22) is placed rs1;
64 is placed immediate
9 (x9) is placed rd

S-Format Instructions

image-20240321115017140

Examplesd x9, 64(x22)
    22 (x22) is placed rs1;
    64 is placed immediate
    9 (x9) is placed rs2

image-20240329185255630

Logical

image-20240329185938814

image-20240329201142182

  • slli、ori...使用的仍然是I type Instruction,只是imm只用了低6位

Instructions for making decisions

Branch 指令

image-20240329201636854

image-20240329201822551

image-20240329202259687

  • 通过slli x10,x22,3将 i 左移3位得到地址偏移量

image-20240329202356914

SLT

image-20240329202739370

image-20240329202728563

  • 注意这里的大于表示方法, bge>= beq== bne!= A>B B>=A

有符号数比较

  • blt : less than
  • bge : greater equal

无符号数 bltu bgeu

Switch/Case

jalr :jump and load register

JumpTable中存储的是目标程序的地址,这个Table的起始地址假设在X6

image-20240329205936056

image-20240329210014029

  • 边界控制
  • 计算Table中的地址
  • 取出Table中的值(一个跳转的地址)
  • 跳转

Basic Blocks

image-20240507201107993

Procedure/function

jal : jump and link

jal x1,100 跳到 PC + 100 的位置

jarl x1,100(x5) 几乎相同,只是跳转的基址是reg中的值

基本流程
    // Caller !push to stack!
    Place Parameters in a place where the procedure can access them

    Transfer control to the procedurejump to  

    Acquire the storage resources needed for the procedure

    Perform the desired task

    Place the result value in a place where the calling program can access it 
    // Callee jalr x0,0(x1) 
    // !pull from stack and recover!
    // !注意函数return的值要给某个另外的寄存器,而不是temporary寄存器!
    Return control to the point of origin 

Stack

image-20240507201515737 - 当参数多于临时寄存器的个数 - 恢复原来的参数 针对Saved value - 注意方向,可以看到下面是低位,所以push要sp - 8*bits

202404082213406

custom-image-17

image-20240330223530829

image-20240406211909518

Communicating with People

image-20240507204926938

    strcpy:     addi   sp, sp, -8       # adjust stack for 1 doubleword
                sd      x19, 0(sp)              # save x19
                add    x19, x0, x0          # i  =  0 
     L1:        
                add   x5, x19, x11              # x5 = address of y[ i ]
                lbu   x6, 0(x5)             # x6  =  y [ i ]
                add   x7, x19, x10          # x7 = address of x[ i ] 
                sb    x6, 0(x7)             # x[ i ]  =  y[ i ]
                beq   x6, x0,  L2                # if y[i] == 0 then exit
                addi    x19, x19, 1                # i  =  i  +  1
                jal     x0, L1             # next iteration of loop
     L2:    ld  x19, 0(sp)         # restore saved old s3
            addi    sp, sp, 8      # pop 1 double word from stack
            jalr    x0  0(x1)  # return

注意这里计算address的时候是char类型的

32-Bit Immediate and Addresses

lui

image-20240507205325658

寻址

  • PC相对寻址方式

下面两种imm的最低为恒为1

Branches

image-20240507205643403

Jump

image-20240507205743985

这里的jump指的是jal

image-20240507210602456

Synchronization in RISC-V | 同步

不想学了

hobbitqia的Notes

A C Sort Example To Put it All Together

主要要理解一下loop

题目

大小端对齐

image-20240618223842575

这里一个byte存储两个16进制数

answer
  1. 0x11

  2. 0x88

汇编

斐波那契数

image-20240619112610242

尾递归的优化、

image-20240619112555245

image-20240619112536945