생성일: 2021년 10월 3일 오후 8:25
Registers
32 MIPS registers are partitioned as follows
- Register 0 : $zero always stores the constant 0
- Regs 2-3 : $v0, $v1 return values of a procedure
- Regs 4-7 : a0−a3 input arguments to a procedure
- Regs 8-15 : t0−t7 temporaries
- Regs 16-23: s0−s7 variables
- Regs 24-25: t8−t9 more temporaries
- Reg 28 : $gp global pointer
- Reg 29 : $sp stack pointer
- Reg 30 : $fp frame pointer
- Reg 31 : $ra return address
Procedures
Each procedure (function, subroutime) maintains a scratchpad of register values
- When another procedure is called (the callee(수신자)), the new procedure takes over the scratchpad
- Values in the register file may have to be saved so we can safely return them to the caller
- Parameters (arguments) are placed where the callee can see them
- Control is transferred to the callee
- Acquire storage resources for the callee
- Execute the procedure
- Place result value where the caller can access it
- Return control to caller
Jump-and-Link (jal)
- A special register (storage not part of the register file) maintains the address of the instruction currently being executed – this is the program counter (PC)
- The procedure (function) call is executed by invoking the jump-and-link (jal) instruction – the current PC (actually, PC+4) is saved in the register $ra and we jump to the procedure’s address (the PC is accordingly set to this address)
💡 jal addr
- To return from the procedure, we can simply execute “jr $31” (or jal addr stroes PC+4 in register $31, and then jumps to location addr)
Supporting procedure calls
- Problem
- What if procB calls another procedure?
- Content of $ra will be overwritten
- What if we have more than 4 parameters?
- Solution
- Use stack in main memory for storing
- Stack은 동적으로 증가하고 축소되는 메모리 영역
Stack
A procedure’s values are therefore backed up in memory on a stack