본문 바로가기
Computer Architecture/ISA

[RISC-V] 컴퓨터구조 - Addressing Mode & Instruction Format

by FastBench 2024. 3. 12.
반응형

사전 참고
1. [RISC-V] 컴퓨터구조 - Instructions 개요  https://microelectronics.tistory.com/38
2. [RISC-V] 컴퓨터구조 - Instruction to Machine Code https://microelectronics.tistory.com/39
3. [RISC-V] 컴퓨터구조 - Procedure call 과 스택포인터 https://microelectronics.tistory.com/40


2.9 Communicating with People

ASCII 코드를 통해 1바이트 숫자로 문자를 표현할 수 있게 되었다.
따라서 당연히 바이트를 추출하는 명령어도 존재해야 한다.

당연하게  byte load 시 오른쪽 기준으로 load 된다.
 

 

2.10 RISC-V Addressing for Wide Immediates and Addresses

RISC-V 의 레지스터들은 32비트 사이즈로 고정이 되지만, 32비트 또는 그 이상의 상수나 주소를 표현하는 일이 생길 수 있다.
 

Wide Immediate Operands

대부분의 상수들은 주로 12비트나 그 아래의 값을 가지기 때문에, 기존 I-type 명령어의 12비트 필드도 충분하지만 그 이상의 비트를 필요로 할 때도 있다.

U-format 명령어가 처음 등장하였다

 

Addressing in Branches

RISC-V 분기 명령어는 12비트 immediates 를 사용하는 명령어 format 을 사용하는데. 이 형식은 -4096~4094 까지의 범위만 사용 가능하다.
12비트 임에도 -2048~2047 이 아닌 이유는, [11:0] 이 아닌 [12:1] 을 받도록 약속하여 짝수만 표현가능한 13비트 수를 받게 하였기 때문이다. 
(왜? 16비트 시스템이던 32비트 시스템이던 64비트 시스템이던 주소의 0비트는 0일수밖에 없다)
 

jal 의 imm 필드가 20bit 길이라면, 이것은 곧 프로그램의 사이즈가 2의 20승 보다 클 수 없다는 의미가 된다.
 
그런데 현실적으로 분기는 루프와 if문에서 발견되고, 가까운 거리로 점프한다. (허용 범위는 immediate 가 12비트냐 20비트냐에 따라 다를것이다) 
따라서 현재 주소를 나타내는 PC에 20비트의 오프셋을 더하는,
PC-relative addressing 을 한다면 거의 모든 분기 케이스를 커버할 수 있게 된다.
 
대다수의 컴퓨터와 같이 RISC-V 도 conditional branch 그리고 unconditional jumps 모두에 PC-relative addressing 을 사용한다.
 
하지만 만약 procedure 호출과 같은 경우 극단적인 사이즈의 점프가 필요할 수 있다.
이럴 경우에는 lui 명령어를 사용해 상위 20비트를 임시 레지스터에 쓰고, jalr 의 immediate(offset) 통해 하위 12비트를 더해 jump 할 수 있다.
 

RISC-V Addressing Mode

명령어 동작에 따라 4가지의 모드가 존재한다.

 

  1. Immediate addressing, where the operand is a constant within the instruction itself.
  2. Register addressing, where the operand is a register.
  3. Base or displacement addressing, where the operand is at the memory location whose address is the sum of a register and a constant in the instruction.
  4. PC-relative addressing, where the branch address is the sum of the PC and a constant in the instruction.

RISC-V Instruction Encoding and Format

분기문의 경우 S-format 과 포맷이 동일하고
jal 의 경우 20 비트 imm 을 가진 U-format 과 포맷이 동일하지만
짝수비트만 표현가능한 imm범위를 사용하는 대신 범위를 1비트 늘렸으므로 ( [11:0] -> [12:1] )
각각 SB-format, UJ-format 으로 명명한다.


Reference

반응형

댓글