[計算機硬件] RISC-V 匯編指令筆記


RISC-V 匯編指令筆記

本文主要作為RISC-V Instruction的一個梳理。

 

RISC-V Instruction 的格式通常為

  Operation code + Destination register + First operand register + Second oprand register

如,add x1, x2, x0 對應的 opcode = add; rd = x1; rs1 = x2; rs2 = x0.

 

對於RISC-V,有32個寄存器,分別是:

RISC-V寄存器表

寄存器 調用名字   用途 存儲者 
x0 zero 常數0  N.A.
x1 ra  返回地址   Caller
x2 sp  棧指針   Callee
x3 gp  全局指針   /
x4 tp  線程指針   /
x5-x7 t0-t2  臨時存儲   Caller
x8 s0/fp  保存用寄存器/幀指針(配合棧指針界定一個函數的棧)  Callee
x9  s1  保存用寄存器   Callee
x10-x11  a0-a1  函數參數/返回值   Caller
x12-x17  a2-a7  函數參數  Caller
x18-x27  s2-s11  保存用寄存器  Callee
x28-x31  t3-t6  臨時存儲   Caller
f0-f7  ft0-ft7  浮點臨時存儲   Caller
f8-f9  fs0-fs1  浮點保存用寄存器   Callee
f10-f11  fa0-fa1  浮點函數參數/返回值   Caller
f12-f17  fa2-fa7  浮點函數參數   Caller
f18-f27  fs2-fs11  浮點保存用寄存器   Callee
f28-f31  ft8-ft11  浮點臨時存儲  Caller

tips:   callee:是一個指針,指向擁有這個arguement對象的函數

   caller:保留着調用當前函數函數的引用。

 

而RISC-V RV32標准指令集有以下幾種框架

  • R-format for register-register arithmetic/logical operations
  • I-format for register-immediate arith/logical operations and loads
  • S-format for stores
  • B-format for branches
  • U-format for 20-bit upper immediate instructions
  • J-format for jumps
  • Others:  Used for OS & Syncronization

R即Reg相關;I即立即數相關;S存儲相關;B分支相關;U高位數相關(因為一條32位指令中無法表示高達32位的數據);J跳轉相關。

tips: about arithmetic & logical operations.

  • 邏輯右移(LSR)是將各位依次右移指定位數,然后在左側補0,算術右移(ASR)是將各位依次右移指定位數,然后在左側用原符號位補齊
  • 邏輯左移與算術左移操作相同。
  • RISC-V采用小端格式(Little-Endian),即低位字節排放在內存的低地址端,高位字節排放在內存的高地址端。

 

R-Format

通常是xxx rd, rs1, rs2。

31                                        0

Funct7 rs2 rs1 Funct3 rd opcode
7 5 5 3 5 7

Opcode = 0110011

Funct7 + Funct3 + opcode defines what operation to perform.

 

Funct7

Funct3

opcode

用途

ADD

0000000

000

0110011

加法

SUB

0100000

000

0110011

減法

SLL

0000000

001

0110011

左移

SLT

0000000

010

0110011

Set Less Than

:= rs1 < rs2 ? 1:0

SLTU

0000000

011

0110011

SLT Unsigned

XOR

0000000

100

0110011

異或

SRL

0000000

101

0110011

邏輯右移

SRA

0100000

101

0110011

算術右移

OR

0000000

110

0110011

AND

0000000

111

0110011

相關偽指令:

mv rd, rs  =  addi rd, rs, x0

nop      =  addi r0, r0, x0

not rd, rs   =       xori rd, rs, 111111111111

Note:  因為某事突然發現RISC-V好像沒有循環移位的指令(未查證),要實現循環移位估計要三條指令以上。

 

I-Format

Opcode 0010011

Immediate rs1 Funct3 rd opcode
12 5 3 5 7

 

  Shift-by-immediate rs1 Funct3 rd opcode
0x00000 5 5 3 5 7

第一部分包括ADDI, SLTI, SLTIU, XORIORIANDI(立即數有12位)

  和SLLISRLISRAI(立即數僅有5位)。

第二部分包括load instruction,格式同ADDI(12位的立即數)

  Load Opcode 0000011

  有LB(load byte) ,LH(load halfword=2 bytes), LW,LBU(load unsigned byte),LHU(load unsigned halfword)

 

S-Format

Opcode 0100011

 imm[11:5] rs2 rs1 Funct3 imm[4:0] opcode
7 5 5 3 5 7

包括SB,SW,SH

 

B-Format

Opcode 1100011

imm[12] imm[10:5] rs2 rs1 Funct3 imm[4:1] imm[11] opcode
1 6 5 5 3 4 1 7

可以表示-4096~4094的范圍

Note:The 12 immediate bits  encode  even 13-bit signed byte offsets (lowest bit of offset is always zero, so no need to store it).

包括BEQBNE,BLT(branch less than: if [rs1]<[rs2], then branch) ,BGE(branch greater than or equal: if [rs1]>=[rs2], then branch) ,BLTU,BGEU。

 

相關偽指令:

beqz x1, label  =  beq x1, x0, label

bnez x1, label  =  bne x1, x0, label

 

U-Format

imm[31:12] rd opcode
20 5 7

LUI – Load Upper Immediate  lui rd, upper im(20-bit) 

e.x.  lui x10, 0x87654   # x10 = original value → 0x87654000

AUIPC – Add Upper Immediate to PC

e.x.  auipc t0, 1  # t0 = original value → PC + 0x00001000

     auipc t0, 0  # t0 = original value → PC

 

相關偽指令:

加載立即數

li rd, imm(32-bit)     =     lui rd, imm(20-bit)

                                   addi rd, rd, imm(12-bit)

加載地址 

la rd, label    =  auipc rd, imm(20-bit)

          addi rd, rd, imm(12-bit)

 

J-Format

imm[20] imm[10:1] imm[11] imm[19:12] rd opcode
1 10 1 8 5 7

 

 

 

jal x0, label                  Discard return address

jal ra, function_name   Call Function within 218 (1 instruction = 22 bytes).

 

jr ra

 

# Call function at any 32-bit absolute address

lui x1, <high 20-bit>

jalr ra, x1, <low 12-bit>      Call Function at 32-bit absolute address

 

# Jump PC-relative with 32-bit offset

auipc x1, <high 20-bit>

jalr x0, x1, <low 12-bit>      Jump within 32-bit

 

相關偽指令:

j label     =     jal x0, label

ret          =     jr ra               =     jalr x0, ra, 0

 

Acknowledgement

  • UC Berkeley CS61C
  • http://www.kvakil.me/venus/  可以在線編譯RISC-V的工具


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM