Supporting Procedures in Computer Hardware
Six Steps in Execution of a Procedure
- Main routine(caller) 가 parameters(=arguments) 를 procedure(callee)가 접근할 수 있는 곳에 위치시킨다.
- caller : 함수를 부르는 루틴
- callee: 불러지는 함수
- $a0 - $a3이 register에서 arguments를 위한 곳 (four argument registers)
- Caller가 callee로 control을 넘긴다. (callee가 실행됨)
- Callee가 실행되면 실행하기 위해 필요한 스토리지 공간, 즉 메모리를 할당받는다.(보통 stack이라는 공간)
- Callee가 desired task를 수행한다.
- Callee는 task끝난 후 결과를 Caller가 접근할 수 있는 곳에 위치시킨다.
- $v0 - $v1이 result values를 위한 곳이다( two value registers for result values)
- Callee는 Caller에게 컨트롤을 넘긴다. (Caller 실행)
- 이때는 $ra가 실행 - 리턴해야하는 주소값이 들어가있음.
- $ra가 pc(program counter)로 복사되어 6번 단계 실행 수에는 $ra에 담겨있는 명령어부터 수행하게 된다.
Register Usage

28~31 : special register
Procedure Call Instructions
- jal procedureLabel
- procedure call: jump and link 명령어
- 실행시 procedureLabel에 있는 명령어로 이동
- 동시에 현재 그 명령어(procedurelabel) 다음에 있는 명령어를 $ra에 넣는다.
- jr $ra
- procedure return: jump register 명령어
- $ra 레지스터의 명령어를 Program Counter(PC)에 복사
- case/switch statements같은 계산된 jump 명령어에서도 사용되기도 한다.
Leaf Procedure Example
int leaf_example (int g, h, i, j)
{ int f;
f = (g+h) - (i+j);
return f;
}
- arguments g, ..., j는 $a0, ..., $a3 에 있음
- f 는 $s0에 있음 즉, stack의 $s0에 저장해야한다
- result는 $v0에 들어있음