SKKU SW/Computer Architecture

[컴퓨터구조] RISC-V Instruction Formats(1)

효딩 2023. 4. 14. 17:43

Instructions and data are represented in binary.

(machine code)

 

Instructions and data are stored in memory (set of bytes)

 

Operating system will load the program binary from the disk and put it to the memory

-> CPU ready to execute the program

 

RISC-V의 모든 instruction은 32-bit, 즉 1 word의 길이로 나타내며,

little endian으로 나타낸다. 한 마디로, 우리는 뒤에서부터 읽어 해석해야 한다.

 

<RISC-V R-format Instructions>

- Instruction fields

opcode: operation code

(0~6비트까지 총 7bits. representing type of instruction. shift인지 arithmetic인지 이런거 구분해주는 역할을 한다.)

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)

funct3와 funct7를 합친 10-bit가 operation의 세부적인 기능을 결정한다.

 

RISC-V assembly instruction will be represented in 32 bits in binary value in memory

-> CPU will read this 32 bits and execute

 

오른쪽에서부터 쭉 읽어서 이를 4비트씩 끊고, 이렇게 표현된 binary를

hexadecimal format으로 읽을 수도 있다!

 

<RISC-V I-format Instructions>

Immediate arithmetic and load instructions

rs1: source or base address register number

R-format의 opcode~rs1까지는 동일한 형식이지만, I-format은 rs2가 불필요하므로 rs2 + funct7 영역을 합친

12bit로 상수를 표현한다.(immediate)

immediate: constant operand, or offset added to base address

using 2's complement

-> will be sign extended to 32-bit

 

<Sign extension>

representing a number using more bits

preserve the numeric value

 

sign bit를 왼쪽으로 쭉 replicate한다.

ex)

0000 0010 -> 0000 0000 0000 0010

1111 1110 -> 1111 1111 1111 1110

 

lb: sign-extend loaded byte

1bu: zero-extend loaded byte

 

<Shift Operations>

slli, srli, srai

but not using I-format

shamt (0~31): shift amount in number of bit positions

 

sll, srl, sra

rd <- value in rs1 shifted by shift amount in rs2