목차
Review
Supporting procedures
Before performing the desired task
- Caller puts parameters in some appropriate place
- Caller executes jal instruction
- Callee acquires the storage resources in stack to save some "saved registers" and to allocated some local variables
- Callee uses the parameters
After performing the desired task
- Callee restores some "saved registers" and releases the acquired storage resources
- Callee puts result value in some appropriate place
- Callee executes jalr instruction
- Caller takes the result value
Memory layout

32-bit Constants : Wide Immediate Operands
Most constants are small
- 12-bit immediate is sufficient with I-format
For the occasional 32-bit constant
- Load 20-bit upper immediate first
- Add 12-bit lower immediate later
Example : 0x003D0500

U-format

Supports 20-bit immediate
Instruction fields
- opcode : operation code
- rd : destination register number
- immediate : bits 12 through 31
lui x19, 0x003D0 (Load upper immediate)

- Loads 20-bit constant to bits [31:12] of register rd
- Extends bit 31 to bits[63:32]
- Clears bits [11:0] of rd to 0

Long Jumps to 32-bit Absolute Address
PC-relative addressing is sufficient for most branches
- Destination of branches is likely to be close to the branch
Procedure calls may require jumping > 2^18 words away
=> 18이 괜히 18이 아님
- No guarantee that the callee is close to the caller
- Needs very long jumps to any 32-bit address
Two-instruction sequence, lui and jalr
- lui instruction writes bits 12 through 31 of branch address to a temporary register
- jalr instruction adds the lower 12 bits of the address to the temporary register and jumps to the sum
- Example 32-bit address: 0x003D0500
lui x19, 0x003D0
jalr x0, 0x500(x19)
Addressing in Branches
Most branch targets are near branch
- Forward or backward => PC-relative addressing
- Target address = PC + immediate[12:1] * 2
Conditional branch
- Opcode, two registers, 12-bit immediate[12:1] (SB-format)
- Represent addresses from -4096 to 4094 (even address only)
- Branch within +-2^10 words (+-4 KiB)
bne x10, x11, 2000
Unconditional branch
- Opcode, one register, 20-bit immediate[20:1] (UJ-format)
- Branch within +-2^18 words (+-1 MiB)
jal x0, 2000
Branching Far Away
If branch target is too far to encode, assembler rewrites the code
Example
beq x10, x0, L1
=>
bne x10, x0, L2
jal x0, L1
L2 : ...