RISC-V¶
约 966 个字 8 张图片 预计阅读时间 3 分钟
为了这个语言专门开了 cs61c 这个坑。
Basic Introduction¶
What is ISA¶
- Instruction Set Architecture, an abstract model that generally defines how software controls the CPU in a computer or a family of computers.
- In general, an ISA defines the supported instructions, data types, registers; the hardware support for managing main memory, fundamental features (such as the memory consistency, addressing modes, virtual memory), and the input/output model of implementations of the ISA.
- The level between software & hardware.
CISC & RISC¶
- CISC: Complex Instruction Set Computing- difficult to learn
- less work for the compiler
- complicated hardware runs more slowly
 
- RISC(Reduced): Simpler/smaller instruction set
What is RISC-V?¶
- Open-source Instruction Set specification
- 
Appropriate for all levels of computing - 32 / 64 / 128-bits
- microcontrollers / supercomputers
 

- Little Endian: 小端序,一个多位数,低位存小地址,高位存大地址(和正常阅读顺序相反)
How to Use Green Card?¶
- 本节课使用 32 位机,greencard 中的以 w为后缀的是为了 >32 位机准备,使得操作统一在 32 位。
- FMT是指操作系统把汇编语言转化为机器语言的方式,具体可见 greencard 中的右下角。
Registers¶
- The more registers you have, the slower it is to access any of them.
- number: 32, x0~x31.


- Registers have no type.
- Register zero(x0) always has the value 0 and cannot be changed.
Basic Instructions¶
Arithmetic Instructions¶
- op dst, src1, src2:- dst = src1 <op> src2.
- opi dst, src, imm:- addi t1, s1, 5:- a = b + 5.
- 没有 subi:因为完全可以通过加负数来实现(但是sub是有的!!!)
 
- mul dst, src1, src2; mulh dst, src1, src2:- src1*src2,第 32 位用- mul计算,高 32 位用- mulh计算.
- div dst, src1, src2; rem dst, src1, src2.
Compare Instructions¶
- slt dst, reg1, reg2:- dst = (reg1 < reg2)
- slti dst, reg1, imm.
Data Transfer¶
memop reg, off(bAddr)
- access memory at bAddr+off.
- Load Word(lw): Takes data at address bAddr+offfrom the memory and place it into reg.
- Store Word(sw): store the reg to the address.
- word: 32-bits, 4 bytes.(通用于 RISC-V)

- 有关小端序和 sign extention的例子

- Half-Word Instructions:

Control Flow Instructions¶
- beq reg1, reg2, label: If- reg1 == reg2goto label, otherwise go to the next instruction.
- bne reg1, reg2, label: not equal.
- j label: jump.
- blt reg1, reg2, label:- reg1 < reg2.
- bge reg1, reg2, label:- reg1 >= reg2.
Program Counter¶
A special register that keeps track of the memory address of the next instruction to be executed in a program.
Loops¶
Environmental Call¶
- ecallis a way for an application to interact with the operating system.
- The value in register a0is given to the OS which performs special functions.(不同操作系统所用的寄存器可能不相同)- Print values
- Exit the program
- Allocating more memory for the program
- For more information
 
Function in RISC-V¶
- a0-a7: eight argument registers to pass parameters.(more arguments go to the stack)
- a0-a1: two argument registers also used to return values.
- sp: "stack pointer", holds the current memory address of the "bottom" of the stack.
Jumping and Returning¶
- 
jal ra sum(jump and link): 设置ra为该指令的下一个地址(link),然后去执行sum函数,执行结束以后再执行jr ra,返回ra继续执行。
- 
在实际的 risc-v 中,其实没有 j label这个语句,它是通过jal x0 label来实现的,因为对x0的写入是无效的。( jal x0 ra是错误的,因为第二个参数必须是一个label)
- 
jr ra可用jalr x0, ra, 0来实现,其中 0 是程序跳到ra的后续偏移量。
Local Storage for Variables¶
- 
主要靠移动栈指针 sp来实现。
- 
核心问题出在函数嵌套上面—— rais overwritten?
- 
s0-s11作为 saved registers,当 callee 被调用时,要先把 caller 用的s全部存到栈里面。- s0: frame pointer(参考 x86 笔记—— stack frame)
 
- 
对于 ra寄存器,在 caller 调用 callee 时,会提前把它进行存储。其他要存储的寄存器: t0-t6(tempporary registers), a0-a7(return address and arguments)

(注意!栈的保存是从高地址到低地址)
- It's callee's duty to store the registers it wants to use to the stack before overrites it.
Instruction Formats¶
原则:尽量 32 位中相同的位置在不同的 Formats 中能有相同的作用。
R-Format¶
有 dest 三寄存器运算(rd: register destination)。
I-Format¶
有 dest 二寄存器一立即数运算;如读取数据操作。
S-Format¶
无 dest 二寄存器一立即数运算;如保存数据操作。
SB-Format¶
用于 branch 操作。
- Extensions to RISC-V base ISA support 16-bit compressed instructions.
- 16-bit = half-word.
- To enable this, RISC-V scales the branch offset to be half-words even there are no 16-bit instructions.
- PC-Relative Addressing: 以 PC 为中心定位代码地址(所以一个完整的代码无论存在哪里相对偏移量都是不变的)

U-Format¶
Dealing With Large Immediates.
- luiwrites the upper 20 bits of the destination with the immediate value.
- Together with addito set low 12 bits.
- The key to PC-Relative Addressing: auipc x10 0.
UJ-Format¶
- For branches, we assumed that we won't branch too far, but for general jumps(jal), we may jump anywhere in code memory.
- notice: jalris I-Format.(rd = PC+4; PC = rs1 + offset)