![Page 1: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/1.jpg)
CPS3340 COMPUTER
ARCHITECTURE Fall Semester, 2013
CPS3340 COMPUTER
ARCHITECTURE Fall Semester, 2013
10/17/2013
Lecture 12: Procedures
Instructor: Ashraf Yaseen
DEPARTMENT OF MATH & COMPUTER SCIENCECENTRAL STATE UNIVERSITY, WILBERFORCE, OH
1
![Page 2: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/2.jpg)
Review
Last Class Conditional Instructions
Beq, bne, j slt, slti, sltu, sltui
Branch Addressing
This Class Procedure Call
Leaf procedure Non-leaf procedure
Next Class Characters Starting a Program Linking
2
![Page 3: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/3.jpg)
Procedure
Procedure (function) A stored subroutine that performs a specific
task based on the parameters with which it is provided
Important when writing a large program Allow a programmer to focus on a specific
task
3
![Page 4: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/4.jpg)
Procedure Calling
Steps required1. Place parameters in registers2. Transfer control to procedure3. Acquire storage for procedure4. Perform procedure’s operations5. Place result in register for caller6. Return to place of call
4
![Page 5: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/5.jpg)
Caller and Callee
Caller The program that instigates a procedure
and provides the necessary parameter values
Callee A procedure that executes a series of
stored instructions based on parameters provided by the caller and then returns control to the caller
5
![Page 6: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/6.jpg)
Register Usage
$a0 – $a3: arguments (reg’s 4 – 7) $v0, $v1: result values (reg’s 2 and 3) $t0 – $t9: temporaries
Can be overwritten by callee $s0 – $s7: saved
Must be saved/restored by callee $gp: global pointer for static data (reg 28) $sp: stack pointer (reg 29) $fp: frame pointer (reg 30) $ra: return address (reg 31)
6
![Page 7: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/7.jpg)
Program Counter (PC)
Program Counter A register in CPU Containing the address of the instruction in
the program being executed
7
![Page 8: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/8.jpg)
Stack
Stack A last-in-first-out queue Stack pointer
$sp Point to the address of the most recent element
in the stack Push
Add element onto the stack Pop
Remove element from the stack
8
![Page 9: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/9.jpg)
Procedure Call Instructions
Procedure call: jump and linkjal ProcedureLabel Address of following instruction put in $ra Jumps to target address
Procedure return: jump registerjr $ra Copies $ra to program counter Can also be used for computed jumps
e.g., for case/switch statements
9
![Page 10: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/10.jpg)
Leaf Procedure and non-Leaf Procedure
Leaf Procedure Procedures that do not call other
procedures Non-leaf Procedure
Procedures that call other procedures
10
![Page 11: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/11.jpg)
Leaf Procedure Example
C code:int leaf_example (int g, int h, int i, int j){
int f; f = (g + h) - (i + j); return f;}
Arguments g, …, j in $a0, …, $a3 f in $s0 (hence, need to save $s0 on
stack) Result in $v0
11
![Page 12: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/12.jpg)
Leaf Procedure Example
MIPS code: (leaf example)addi $sp, $sp, -12
sw $t1, 8($sp)
sw $t0, 4($sp) sw $s0, 0($sp)
add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1
add $v0, $s0, $zero
lw $s0, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
addi $sp, $sp, 12
jr $ra
Save $s0, $t1, $t0 on stack
Procedure body
Restore $s0, $t1, $t0 from the stack
Result
Return
12
![Page 13: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/13.jpg)
Status of Stack13
![Page 14: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/14.jpg)
Temporary Registers
MIPS Assumption$t0 – $t9: temporary registers that are not
preserved by the callee on a procedure call
$s0 – $s7: saved registersMust be preserved by callee on a procedure callIf used, the callees saves and restores them
14
![Page 15: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/15.jpg)
Simplified Leaf Procedure Example
MIPS code: (leaf example)addi $sp, $sp, -4
sw $s0, 0($sp)
add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1
add $v0, $s0, $zero
lw $s0, 0($sp)
addi $sp, $sp, 4
jr $ra
Save $s0 on stack
Procedure body
Restore $s0 from the stack
Result
Return
15
![Page 16: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/16.jpg)
Non-Leaf Procedures
Procedures that call other procedures For nested call, caller needs to save on
the stack: Its return address Any arguments and temporaries needed
after the call Restore from the stack after the call
16
![Page 17: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/17.jpg)
Non-Leaf Procedure Example C code:
int fact (int n){ if (n < 1)
return 1; else
return n * fact(n - 1);}
Argument n in $a0 Result in $v0
17
![Page 18: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/18.jpg)
Non-Leaf Procedure Example
MIPS code:
fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and returnL1: addi $a0, $a0, -1 # else decrement n = n - 1 jal fact # recursive call lw $a0, 0($sp) # restore original n lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return
18
![Page 19: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/19.jpg)
What is preserved and what is not?
Data and registers preserved and not preserved across a procedure call
19
![Page 20: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/20.jpg)
Global Pointer
Two kinds of C/C++ variables automatic
Local to a procedure Discarded when the procedure exits
static Global to a procedure Still exist after procedure exits Can be revisited
Global Pointer $gp Point to static area
20
![Page 21: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/21.jpg)
Procedure Frame
Revisiting Stack Stack not only stores the saved registers but also local variables that do not fit in
registers local arrays or structures
Procedure Frame (activation record) Segment of the stack containing a procedure’s
saved registers and local variables Frame pointer
Point to the location of the saved registers and local variables for a given procedure
21
![Page 22: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/22.jpg)
Local Data on the Stack
Local data allocated by callee e.g., C automatic variables
Procedure frame (activation record) Used by some compilers to manage stack storage
22
![Page 23: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/23.jpg)
MIPS Memory Layout
32-bit address space 0x80000000 ~ 0xFFFFFFFF
Not available for user program For OS and ROM
0x00000000~0x003FFFFF Reserved
0x00400000~0x0FFFFFFF Text: Machine language of the user program
0x10000000~0x7FFFFFFF Data
Static: Static variables, Constants Dynamic: Malloc() in C, New in java Stack
23
![Page 24: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/24.jpg)
Memory Layout24
![Page 25: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/25.jpg)
Register Summary
Register 1: $at reserved for the assembler
Register 26-27: $k0-$k1 reserved for the OS
25
![Page 26: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/26.jpg)
Summary
Procedure Call Steps of Procedure Call Caller and Callee Registers used Stack jal and jr leaf and no-leaf procedure Allocating space for new data on the heap
26
![Page 27: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/27.jpg)
Summary
Procedure Call Registers used Stack jal and jr leaf and no-leaf procedure Allocating space for new data on the heap
27
![Page 28: CPS3340 Computer Architecture Fall Semester, 2013](https://reader036.vdocuments.us/reader036/viewer/2022070404/56813af1550346895da3689b/html5/thumbnails/28.jpg)
What I want you to do
Work on Assignment 3 Review Chapter 2
28