목차
Decision-Making Instrucions

=> imm[11:5] -> imm[12](11번째 비트가 있었던자리) / imm[10:5]
만약 imm[11:0]이면, 가장 큰 양수는 2^11-1
4의 배수를 2진수로 하면 끝자리는 무조건 ~00
주소를 표현할 일이 생기면 4의 배수일테니까 뒤 두자리는 안써도 00인걸 알듯
얘를 왜 굳이 저장해서 imm 두칸을 낭비할까.
아뭔소리야
암튼 imm[11:0]이면 표현 가능한 가장 큰 양수가 2^11-1인데,
imm[12:1]이면 표현 가능한 가장 큰 양수가 2^12-1이 됨. 표현 가능한 범위가 늘어남.
긩까 imm[0] 대신에 imm[12]를 넣자~
근데 imm[12]는 부호! 최상위수니까! 개중요한 비트인거임
그래서 imm[12]를 제일 앞에 뺀거임
s-format이나 비트가 표현하는 건 똑같게되는거임
beq는 피연산자가 두개인데, 어디로 갈지의 수가 크지 않아도 됨, 어차피 근처로 감.
Commonly represented using if statement
- Sometimes combined with goto statement
Branch to a labeled instruction if the value in a register (rs1) equal to the value in another register (rs2)
- Otherwise, continue sequentially
=> instruction pointer(counter), 즉 다음에 실행할 instruction의 주소를 바꿈
beq rs1, rs2, L1
Branch if the first value does not equal to the second value
bne rs1, rs2, L1
SB-format (S-format에서 유래된 brance instruction)

Slightly different form S-format
Instruction fields
- opcode : operation code
- func3 : 3-bit function code (additional opcode)
- rs1 : first source register number
- rs2 : second source register number
- immediate : offset added to PC(unusual encoding)
12 bit immediate, imm[12:1]
- represent addresses from -4096 to 4094
- in multiples of 2 (only even address)
위에서는 imm[12:1]이면 2^12-1라고 했는데, 여기서 -1를 하면 2의 배수가 안되니까
2^12-2까지 표현 가능. 이 수가 바로 4094
Compiling If Statements
C code
if (i==j) f = g+h;
else f = g-h;
Register usage
- f, g, h -> x19, x20, x21
- i, j -> x22, x23

Compiled RISC-V code

=> 만약 i와 j가 같다면, 보이는 instructions중 1, 4, 5째줄인 3개 실행
만약 i와 j가 같지 않다면, 2, 3째 줄인 2개 실행
register usage는 대칭적이어보이지만, 실은 대칭적이지 않음
왜그러냐, 같건 다르건 1째줄은 무조건 실행해야하는데, 3째줄은 무조건 안해도되는 branch.
Compiling Loop Statements
C code
while (save[i] == k) i += 1;
-> instruction 입장에서는 아래처럼 이해하는게 좋음
while (*(save+i) == k) i += 1;
Compiled RISC-V code :

4, 6줄 : 브랜치
More Conditional Instructions
Many other relationships between two numbers
- If the index variable is less than 0
- Full set of comparisions : <, <=, >, >=, =, !=
=> 다 필요하지 않음. not으로 커버 가능
- Dichotomy between signed and unsigned numbers
Branch if less than (blt)
blt rs1, rs2, L1
Branch if greater than or equal (bge)
bge rs1, rs2, L1
Unsigned version
- Branch if less than, unsigned (bltu)
- Branch if greater than or equal, unsigned (bgeu)
Ex) 2-43
Example.
C code
if (a > b) a += 1;
Register usage
- a -> x22
- b -> x23
Compiled RISC-V code :
