๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐“ก๐“ธ๐“ธ๐“ถ5: ๐’ฆ๐‘œ๐“‡๐‘’๐’ถ ๐’ฐ๐“ƒ๐’พ๐“‹/Computer Architecture(COSE222)

[์ปดํ“จํ„ฐ๊ตฌ์กฐ] CH4. RISC-V & RISC-V Instruction #3

Branch

  • ๊ณ„์‚ฐ๊ธฐ์™€ ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ์˜ ์žฅ์ ์€ ์–ด๋–ค ์˜์‚ฌ๊ฒฐ์ •์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ!
    • ์ปดํ“จํ„ฐ๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • high-level language๋กœ 'if/else, case, while, for loops' ๋ฌธ๋“ค์ด ๋ฐ”๋กœ ์กฐ๊ฑด๋ถ€ ์ฝ”๋“œ(Conditionally execute code) ์ž„!
  • ์›๋ž˜ PC(Program Counter)๋Š” instruction์„ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•œ ๋ช…๋ น์ด ๋๋‚˜๋ฉด 4์”ฉ ์ฆ๊ฐ€ํ•œ๋‹ค (๊ฐ ๋ช…๋ น ํฌ๊ธฐ๊ฐ€ 4 byte์ž„ ใ…‡ใ…‡)
  • Branch Instruction์€ ์ฝ”๋“œ๋ฅผ ์ค‘๊ฐ„์— ๋›ฐ์–ด ๋„˜๊ฑฐ๋‚˜, ์ด์ „ ์ฝ”๋“œ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ๋” PC๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น!
  • Branch Instruction์—๋Š” ๋‘ ์ข…๋ฅ˜๊ฐ€ ์žˆ์Œ
    1. Conditional branch : ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ์—๋งŒ ์‹คํ–‰.
      • beq (branch if equal) : a == b
      • bne (branch if not equal) : a != b
      • blt (branch if less than) : a < b
      • bge (branch if greater than or equal) : a >= b
      • bltu (branch if less than unsigned) : a < b (a, b๋Š” unsigned)
      • bgeu (branch if greater than or equal for unsigned) : a >= b (a, b๋Š” unsigned)
    2. Unconditional branch : ์กฐ๊ฑด ์—†์ด ํ•ญ์ƒ branch ๋จ.
      • jal (jump and link)
      • jalr (jump and link register)
    3. Pseudo Instructions

a > b ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด์„œ๋Š” blt์—์„œ a, b ์œ„์น˜๋งŒ ๋ฐ”๊ฟ”์„œ ๋„ฃ์–ด์ฃผ๊ณ , a <= b ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ bge๋ฅผ ๋’ค์ง‘์–ด์„œ ์‚ฌ์šฉํ•œ๋‹ค!

Branch Instructions

1. beq, bne, blt, bge, bltu, bgeu

  • B-Type instruction

beq rs1, rs2, label

beq s0, s1, skip  # go to "skip" if (s0 == s1)
.
.
.
skip : add t0, t1, t2

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด s0์™€ s1 ์ฆ‰ i์™€ j๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ ์•„๋ž˜๋ฅผ ๊ทธ๋Œ€๋กœ ์‹คํ–‰ํ•˜๊ณ  (add)
๋งŒ์•ฝ i์™€ j ๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ skip์œผ๋กœ ๋ฐ”๋กœ ๋„˜๊ฒจ์„œ add ์ƒ๋žตํ•˜๊ณ  skip ๋ถ€ํ„ฐ ์‹คํ–‰ํ•œ๋‹ค.

์ฐธ๊ณ ๋กœ ๊ฐ™์€์ง€ ์•„๋‹Œ์ง€๋Š” sub instruction์„ ํ™œ์šฉํ•œ๋‹ค. s0์™€ s1์„ ๋นผ์„œ ์ด 32 bit ๊ฒฐ๊ณผ๊ฐ’์„ or ์—ฐ์‚ฐ์„ ํ–ˆ์„ ๋•Œ ๋ชจ๋‘ 0์ด ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

Branch Destination Address

B-type instructions๋Š” 12-bit immediate (imm[12:1]) ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  • Branch Instructions์€ immediate field๋ฅผ offset์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. (base ์—์„œ ๋”ํ•˜๊ณ  ๋นผ๋Š”..?)
  • Offset์€ ํ˜„์žฌ์˜ PC์— ๊ด€๋ จ ๋˜์–ด์žˆ์Œ. ํ˜„์žฌ PC ๊ธฐ์ค€์œผ๋กœ ๋”ํ•˜๊ฑฐ๋‚˜ ๋บŒ

Branch destination calculation

  • branch instruction์—์„œ ๋ถ€ํ„ฐ -2^12 ~ +2^12 - 2 bytes ์‚ฌ์ด์˜ ๋ฒ”์œ„๋กœ ๋ถ„๊ธฐ์˜ ๋ฒ”์œ„๋ฅผ ํ•œ์ •ํ•œ๋‹ค.
    (jump ํ•˜๊ณ  ์‹ถ์–ด๋„ ๋„ˆ๋ฌด ๋ฉ€๋ฆฌ๋Š” ๋ชปํ•œ๋‹ค๋Š” ๋œป)
  • ๊ทธ ๊ฒฐ๊ณผ, destination = PC + sign - extended {imm[12:1], 1'b0}

ํ˜„์žฌ PC(32 bit)์— offset (immediate imm[12:1]) ์„ ๋”ํ•œ ๊ฐ’์ด Branch Destination address์ด๋‹ค.
์ด ๋•Œ offset๊ณผ pc์˜ ๋น„ํŠธ๊ฐ€ ์•ˆ๋งž์œผ๋ฏ€๋กœ offset์˜ MSB๋กœ sign-extend ํ•ด์„œ 32 bit์˜ ์•ž๋ถ€๋ถ„์„ ์ฑ„์›Œ์ค€๋‹ค.

๋ณด๋ฉด if ๋ฌธ ์•ˆ์— i==j๊ฐ€ ์žˆ์ง€๋งŒ ์‹ค์ œ๋กœ assembly test์—์„œ๋Š” ๋ฐ˜๋Œ€์ธ i != j๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

2. slt, sltu, slti, sltiu

  • ์œ„ ๋ช…๋ น์–ด๋“ค์€ ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€ ํŒ๋ณ„ํ•ด์„œ rd์— ์ฐธ์ด๋ฉด 1์„, ๊ฑฐ์ง“์ด๋ฉด 0์„ ์ €์žฅํ•˜๋Š” ๋ช…๋ น์–ด๋‹ค.
  • slt, slti ๋Š” sign numbers
    sltu, sltiu ๋Š” unsigned numbers์—์„œ ์“ด๋‹ค.
  • slti์™€ sltiu์—์„œ immediates๋Š” sign-extended๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
slt rd, rs1, rs2 # set on less than (R-type)
sltu rd, rs2, rs2 # set on less than unsigned (R-type)
slti rd, rs1, imm12 # set on less than immediate (I-type)
sltiu rd, rs1, imm12 # set on less than unsigned immediate (I-type)
slt t0, s0, s1 # if (s0 < s1) then t0 = 1;

sltiu t0, s0, 25 # if (s0 < 25) then t0 = 1;

3. jal (jump and link)

  • J-type instruction
  • Unconditional branch
  • jal ์€ ๋‹ค์‹œ ๋Œ์•„์˜ฌ ์ฃผ์†Œ๋ฅผ ๊ธฐ์–ตํ•˜๊ณ  ๋‹ค๋ฅธ ์–ด๋”˜๊ฐ€์˜ ์ฃผ์†Œ๋กœ jump ํ•˜๋Š” ๋ช…๋ น์–ด
    (ret์„ ๋งŒ๋‚˜๋ฉด ๋Œ์•„์˜ค๋ ค๊ณ  ํ•œ ์ฃผ์†Œ๋กœ ๋Œ์•„์˜จ๋‹ค)
jal rd, target // PC <- target
               // rd <- PC + 4

์œ„์ฒ˜๋Ÿผ PC๋ฅผ target์œผ๋กœ ์ด๋™ํ•  ๊ฑด๋ฐ, rd ์ž๋ฆฌ์— ์›๋ž˜ ์ฝ๋˜ PC+4 ๋ฅผ ์ €์žฅํ•ด๋‘๊ณ  (์›๋ž˜ ์ง„ํ–‰์ค‘์ธ ๋ช…๋ น์˜ ๋‹ค์Œ ๋ช…๋ น ์ฆ‰ jal ๋‹ค์Œ ๋ช…๋ น)
target ์ฝ์œผ๋Ÿฌ ๊ฐ.

j LLL // == jal x0, LLL
jal foo // == jal ra, foo

j instruction์€ pseudo instruction์ด๋‹ค! jal x0 , LLL ์ด๋‹ˆ๊นŒ LLL๋กœ jump ํ•  ๊ฑฐ๊ธด ํ•œ๋ฐ, rd ๊ฐ€ x0 ๋กœ ์ง€์ •์ด ๋˜์—ˆ์œผ๋ฏ€๋กœ PC+4๋ฅผ ๋Œ€์ž…ํ•˜์ง€ ์•Š๊ณ  ์•„๋ฌด๊ฒƒ๋„ ์•ˆํ•˜๊ณ  ๊ทธ๋ƒฅ jumpํ•˜๊ณ  ๋๋‚ธ๋‹ค. x0 ์˜ ๊ฐ’์€ 0์œผ๋กœ ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ!

jal foo๋Š” ra๊ฐ€ ์ƒ๋žต๋œ ๊ฒƒ์œผ๋กœ, foo๋กœ ์ ํ”„ํ–ˆ๋‹ค๊ฐ€ ์›๋ž˜ ์ฝ๋˜ ๋ช…๋ น jal ๋‹ค์Œ ๋ช…๋ น์˜ ์ฃผ์†Ÿ๊ฐ’์„ ra์— ์ž„์‹œ๋กœ ์ €์žฅํ•ด๋’€๋‹ค๊ฐ€ foo๋ฅผ ์ฝ์œผ๋Ÿฌ ๊ฐ„๋‹ค.

Procedure (function)

  1. ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์€ procedure๋‚˜ function์„ ์ด์šฉํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๊ฒŒ ๋œ๋‹ค.
    • ํ”„๋กœ๊ทธ๋žจ์„ modular, ์ฆ‰ ๋ชจ๋“ˆํ™” ํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด!
    • ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด!
    • procedure๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ํ•œ ๋ฒˆ์— ํ•œ ๋ถ€๋ถ„์—๋งŒ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค!
  2. Parameters (=arguments)๋Š” procedure๊ณผ ๋‚˜๋จธ์ง€ ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์ด์˜ interface ์—ญํ• ์„ ํ•ด์ค€๋‹ค.
  3. procedure๋Š” ๋ถ€๋ฅธ๋‹ค.
    • caller : calling procedure (ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ถ€๋ถ„)
    • callee : called procedure (ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜)

jal - ret pair

jal๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด pc๋ฅผ ์ด๋™์‹œ์ผฐ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋‹ค์‹œ ๋Œ์•„์˜ค๋ƒ! ๋ฐ”๋กœ ret์ด๋ผ๋Š” ๋ช…๋ น์–ด๋‹ค.
ret ์€ ์ด์ „์— ์ €์žฅํ•œ ra์˜ ์ฃผ์†Œ๋กœ ๋‹ค์‹œ ์ ํ”„ํ•œ๋‹ค.

์ฆ‰, jal ์ด ra์—๋‹ค๊ฐ€ PC+4์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅ์„ ํ•œ ๋‹ค์Œ simple๋กœ jump๋ฅผ ํ•˜๋ฉด simple์„ ์ฝ๊ธฐ ์‹œ์ž‘ํ• ๊ฑฐ๊ณ , simple์„ ์ฝ๋‹ค๊ฐ€ ret์„ ๋งŒ๋‚˜๋ฉด ์•„๊นŒ ์ €์žฅํ•œ ra์˜ ์ฃผ์†Œ๋กœ ๋‹ค์‹œ jump๋ฅผ ํ•ด์„œ jal์˜ ๋‹ค์Œ instruction์„ ๋‹ค์‹œ ์ฐจ๋ก€๋Œ€๋กœ ์ฝ์–ด๋‚˜๊ฐ„๋‹ค.

4. jalr (jump and link register)

  • I-Type instruction
  • Indirect jump
jalr rd, imm12(rs1) // PC <- rs1 + imm12
                     // rd <- PC + 4

jalr x1, 4(t0)

jair์€ ์ง์ ‘ ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ rs1 + imm12 ๊ฐ€ ์ฃผ์†Œ์ธ ๊ณณ์œผ๋กœ ์ง์ ‘ ์ฐพ์•„๊ฐ„๋‹ค.
rd๋Š” jal ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” ๋ช…๋ น ๋‹ค์Œ ์ค„, ์ฆ‰ PC+4 ๋ฅผ ์ €์žฅํ•ด์ค€๋‹ค.

Jumping Far Away

  • ๋งŒ์•ฝ jal ์˜ destination์ด ๋ฐ”๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” 20-bit๋ณด๋‹ค ๋” ๋„ˆ๋ฌด ๋ฉ€๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?
  • call pseudo instruction์„ ์ด์šฉํ•˜์ž! (= auipc + jalr )

๋ณด๋ฉด foo๊ฐ€ jal๋กœ ๋ถ€ํ„ฐ ๋„ˆ๋ฌด ๋ฉ€๋‹ค! ์ด๋Ÿด ๊ฒฝ์šฐ์—๋Š” auipc๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

์žŠ์–ด๋ฒ„๋ ธ์„ ๊ฒƒ ๊ฐ™์œผ๋‹ˆ auipc๊ฐ€ ๋ญ๋ƒ๋ฉด

auipc๋Š” ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” instruction์˜ ์ฃผ์†Œ(=PC)๋กœ ๋ถ€ํ„ฐ ์ƒ์ˆ˜๋งŒํผ ๋”ํ•œ ์ˆซ์ž๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ]

์ฆ‰, auipc x1, offset[31:12] + offset[11] ์„ ํ•˜๋ฉด x1 ์ž๋ฆฌ์— offset 11๋ถ€ํ„ฐ 31๊นŒ์ง€๋ฅผ ์ €์žฅ์„ ํ•˜๊ณ 
jal x1, offset[11:0](x1)์„ ํ†ตํ•ด x1 ์ฆ‰ offset์˜ 11~31 ๋ฒ”์œ„ + offset[11:0] ์ฆ‰ offset์˜ 0~11 ๋ฒ”์œ„๋ฅผ ๋”ํ•ด ๊ทธ ์œ„์น˜๋กœ jump๋ฅผ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์ •๋ฆฌํ•˜์ž๋ฉด ๊ธฐ๋‚˜๊ธด, ๊ฐ€๊ณ ์ž ํ•˜๋Š” ํ•จ์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ์ชผ๊ฐœ์„œ ๋”ํ•˜๊ณ  ๊ทธ ์œ„์น˜๋กœ jump๋ฅผ ํ•œ๋‹ค๋Š” ๊ฒƒ

5. Jump Pseudo-Instructions

๊ทธ๋ ‡๋‹ค๋ฉด auipc์—์„œ ์™œ ๊ตณ์ด offset์„ 31:12 ์™€ 11๋กœ ์ชผ๊ฐค๊นŒ??
์ฐธ๊ณ ๋กœ offset[11] ์€ sign-extention ์ด๋‹ค. offset[11] ์ด 0์ผ ๋•Œ์™€ 1์ผ ๋•Œ๋ฅผ ๋ณด์ž

1) offset[11] == 0 : {offset[31:12], offset[11:0]}
2) offset[11] == 1 : {{offset[31:12], 0x000} + {0xffff_f, offset[11:0] = {offset[31:!2] , offset[11:0]}