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

2023. 4. 15. 02:17· SKKU SW/Computer Architecture

<Memory addressing>

memory "address" is also a 32-bit (non-negative) integer.

 

memory -> register: Load

(need address & destination register)

register -> memory: Store

(need address & source register)

 

<Memory Operand>

C code 1:

g = h + A[8];

g in x1, h in x2, base address (starting address) of A in x3

A is an 'int' type array

(array: list of values)

 

Compiled RISC-V code:

Index "8" requires an offset of 32

lw x10, 32(x3)     # lw = load word

add x1, x2, x10

 

첫 번째 줄 x10: destination register

32: integer value

x3: register number

32(x3): represent address part

 

C code 2:

A[12] = h + A[8];

h in x2, base address of A in x3

 

Compiled RISC-V code:

lw x10, 32(x3)   # load word

add x11, x2, x10

sw x11, 48(x3)  # store word

 

세 번째 줄 x11: source register

48: offset value

x3: base register

 

lw와 sw는 방향이 다르다는거 기억하기!

lw는 32(x3)을 x10에 load (오른쪽에서 왼쪽으로)

sw는 x11을 48(x3)에 store (왼쪽에서 오른쪽으로)

 

C code 3:

for ( i = 0; i < 10; i++ ) {
     sum = sum + A[i];

}

sum in x1, i in x2, base address of A in x3

 

Compiled RISC-V code (only the loop body):

addi x10, x10, 4     (everytime we go th the for loop, actual memory location increase by 4)

lw x11, 0(x10)

add x1, x1, x11

 

<Memory>

lw, lh, lb

load word / halfword / byte

 

sw, sh, sb

store word / halfword / byte

 

<RISC-V S-format Instructions>

Store instructions use a different binary format:

rs1: base address register number

rs2: source operand register number

immediate: offset added to base address

 

Most constants are small

For occasional 32-bit constant,

lui rd, constant

Load Upper Immediate

copies a 20-bit constant to [31:12] of rd (upper 20 bits of data)

sets the lower 12 bits of rd to 0

 

<Sign Extension>

RISC-V instructions sign-extend the immediate value

lh and lb also sign-extends the loaded halfword or byte

lhu and lbu zero-extends the loaded halfword or byte

-> regardless of loaded value, always fills remaining field with 0

There are no shu or sbu instructions.

 

<Conditional Operations>

Branch to a labeled instruction if a condition is true

Otherwise, continue to the next instruction.

(Depending on condition, if condition is satisfied, it actually jump to another location)

 

If we want to write program structure like conditional statements, while / for loops,

we need to let CPU where we want to move after executing the such instruction - change order of execution

 

beq rs1, rs2, L1

if (*rs1 == *rs2), jump to the instruction labeled L1

bne rs1, rs2, L1

if (*rs1 != *rs2), jump to the instruction labeled L1

 

ex)

beq x10, x12, L1

: If x10 and x12 have some value, then next instruction we're going to execute is indicated by L1 label

There is no destination register

 

blt rs1, rs2, L1

if (*rs1 < *rs2), jump to the instruction labeled L1

(less than)

bge rs1, rs2, L1

if (*rs1 >= *rs2), jump to the instruction labeled L1

(greater or equal to)

 

signed comparison: blt, bge

unsigned comparison: bltu, bgeu

 

<Jump Instructions>

jal x1, L1 : jump-and-link

jump to L1

Address of the following instruction (PC + 4) -> x1

여기서 PC + 4 는 address of the following instruction을 의미한다.

 

similar to 'function'

- branch instruction과 jump instruction의 차이 -

In branch instruction, if condition does not match, we can just go to next instruction

but in jump instruction, we always jump to the target location

쉽게 생각하면 brach는 if ~ else문, jump는 함수를 생각하면 되겠다.

if ~ else문은 조건에 충족할 경우 이를 수행하지만, 함수는 어떤 조건이 있지 않다.

 

다른 location으로 jumping 하면, we have to know where to come back.

: that's why we are remembering location to come back jumping to another locaiton

-> why we are performing additional link operation that stores following instruction's address

 

jalr x1, offset(x2) : jump register

jumping to the address stored in the register

jump to offset + address in x2

Address of the following instruction (PC + 4) -> x1

 

j L1 : jump

pseudo-instruction

jal x0, L1

 

PC: value that tracks where we are executing the instruction from the memory

 

 

'SKKU SW > Computer Architecture' 카테고리의 다른 글

[컴퓨터구조] 5. Memory Hierarchy 메모리 계층구조  (0) 2023.05.28
[컴퓨터구조] Procedure Calling  (0) 2023.04.17
[컴퓨터구조] RISC-V Instruction Formats(1)  (0) 2023.04.14
[컴퓨터구조] Logical operations  (0) 2023.04.14
[컴퓨터구조] Immediate Operands, Load Immediate Value  (0) 2023.04.14
'SKKU SW/Computer Architecture' 카테고리의 다른 글
  • [컴퓨터구조] 5. Memory Hierarchy 메모리 계층구조
  • [컴퓨터구조] Procedure Calling
  • [컴퓨터구조] RISC-V Instruction Formats(1)
  • [컴퓨터구조] Logical operations
효딩
효딩
개ㄱ발은 기세다. 줄여서 객기.
효딩
hyoding
효딩
전체
오늘
어제
  • 분류 전체보기 (245)
    • SKKU SW (30)
      • Computer Architecture (14)
      • Database (4)
      • Computer Network (3)
      • Operating System (7)
      • Mobile App Programming (2)
    • SuperCoding (68)
    • CS (8)
    • Web Programming (19)
    • Cloud (13)
    • Languages (45)
      • Python (8)
      • Java (37)
    • Supporters (8)
      • MoteMote (6)
      • NHN Cloud (2)
    • Certification (27)
      • Network Advisor (14)
      • ADsP (10)
      • Engineer Information Proces.. (3)
    • Finance (9)
      • 경제금융용어 (3)
    • Woori FISA (14)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 네트워크관리사2급 필기
  • 인프라개발
  • 네트워크관리사 준비물
  • 봐
  • AWS
  • 글로벌소프트웨어캠퍼스
  • Kotlin
  • 클라우드 서비스
  • 서버배포
  • K-디지털트레이닝
  • 네트워크관리사 후기
  • 우리FISA
  • 우리에프아이에스
  • 클라우드
  • apppaas
  • 네트워크관리사2급
  • 코틀린문법
  • 인프라
  • 서버개발
  • 네트워크관리사
  • 우리fis아카데미
  • 클라우드서비스개발
  • 코틀린
  • 서버생성
  • 네트워크관리사2급 공부방법
  • 네트워크관리사 합격
  • nhn cloud
  • 네트워크관리사 커트라인
  • 앱개발
  • rds local 접속

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
효딩
[컴퓨터구조] RISC-V Instruction Formats(2)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.