Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI206 - Computer Organization & Programming

Similar presentations


Presentation on theme: "CSCI206 - Computer Organization & Programming"— Presentation transcript:

1 CSCI206 - Computer Organization & Programming
Introduction to Procedures zyBook: 8.1 (for next class)

2 Procedures, the basic idea

3 $a0-$a3 jr $ra $v0-$v1

4 MIPS Procedure convention
Prepare parameters in $a0 through $a3 Return values on $v0, $v1 Call the procedure by jal (jump and link)

5 Example: compute multiplication
mult_begin: # goal: multiply $a0 by $a1 li $v0, 0 # the result will be in $v0 mult_loop: beq $a1, $zero mult_done # check if done ($a1 == 0) add $v0, $v0, $a0 # add $a0 to result one time addi $a1, $a1, -1 # decrement $a1 j mult_loop # repeat mult_done: # the result is in $v0

6 Re-write it as a MIPS procedure
mult_proc: # goal: multiply $a0 by $a1 li $v0, 0 # the result will be in $v0 mult_loop: beq $a1, $zero mult_done # check if done ($a1 == 0) add $v0, $v0, $a0 # add $a0 to result one time addi $a1, $a1, -1 # decrement $a1 j mult_loop # repeat mult_done: # the result is in $v0 Almost identical!!! The difference is how it is used. MIPS procedure call: jal mult_proc

7 multiply in asm activity
Show how you would call the function to compute 2 * 7 and store the result in $s0. Then compute 5 * 9 and store the result in $s1.

8 $a0-$a3 jr $ra $v0-$v1

9 Multiply in asm w/ procedure

10 Terminology $a0-$a3 CALLER CALLEE $v0-$v1 jr $ra

11 jal instruction details
jump and link (J-type) $ra = PC + 8 (from green sheet) PC = JumpAddr procedure: . jr $ra procedure begins after add instruction. jal procedure ← PC add … ← PC + 4 [delay slot instruction ] lw … ← PC + 8 procedure returns before lw instruction.

12 Assumptions After calling a function return to the “next line”
side-effects follow conventions

13 Question Which registers should CALLER use? for doing work
CALLEE Which registers should CALLER use? for doing work for passing arguments to CALLEE for reading the result from CALLEE

14 Question Which registers should CALLEE use? for doing work
CALLER CALLEE Which registers should CALLEE use? for doing work for reading arguments from CALLER for passing the result to CALLER

15 Problems More arguments than $a registers (4)?
Return more results than $v registers (2) Need more registers than $s (caller) or $t (callee) to do work. Callee needs to call another function. jal would overwrite the first $ra! what registers to use (callee vs caller?)

16 Dynamic storage The solution to all of these problems requires some form of dynamic storage. As the name implies, the stack memory segment is managed as a stack data structure and provides dynamic run-time storage.

17 Problems Solutions Have more arguments than $a registers (4)?
Return more results than $v registers (2) Need more registers than $s (caller) or $t (callee) to do work. Callee needs to call another function. jal would overwrite the first $ra! what registers to use (callee vs caller?) Push extra arguments onto the stack Push extra results onto the stack Use the stack to store/restore register values Push registers (including $ra) onto the stack, callee becomes caller!

18 Program Memory Map main program initializes $sp to allocate one word
points to the last used word! li $sp, 0x to allocate one word decrement $sp by 4 $sp moves DOWN to deallocate a word increment $sp by 4 $sp moves UP

19 Stack operations PUSH $x: push reg[x] onto the stack
addi $sp, $sp, -4 # allocate one word sw $x, 0($sp) # store reg $x onto the stack POP $x: pop reg[x] from the stack lw $x, 0($sp) # read the top value of stack addi $sp, $sp, 4 # deallocate word from stack

20 Illustrating a nonleaf procedure
int main(){ printf(“result: %d”, nonleaf(1,1,2,3) ); } int nonleaf(int a, int b, int c, int d){ return sum( sum(a,b), sum(c,d)); int sum(int a, int b){ return a+b; nonleaf needs a stack: allocate stack space store $ra [return from nonleaf] call sum 3 times, storing partial results restore $ra deallocate stack

21 nonleaf assembly main: li $a0, 1 li $a1, 1 li $a2, 2 li $a3, 3
jal nonleaf … <other code> … nonleaf: push $ra # save my $ra push $a3 # save a3 push $a2 # save a2 jal sum # a0/a1 are summed pop $a0 # restore a2 to a0 pop $a1 # restore a3 to a1 push $v0 # store result of sum(a0, a1) jal sum # a0/a1 are used to sum a2/a3 pop $a0 # restore sum(a0, a1) into a0 move $a1, $v0 # move second result to a1 jal sum # compute final sum into $v0 pop $ra # restore $ra jr $ra # return nonleaf assembly int main(){ printf(“result: %d”, nonleaf(1,1,2,3) ); } int nonleaf(int a, int b, int c, int d){ return sum( sum(a,b), sum(b,c)); int sum(int a, int b){ return a+b;

22 nonleaf uses 4 words of stack space...
main: li $a0, 1 li $a1, 1 li $a2, 2 li $a3, 3 jal nonleaf … <other code> … nonleaf: addi $sp, $sp, -16 # allocate space sw $ra, 12($sp) sw $a2, 8($sp) # store a2 sw $a3, 4($sp) # store a3 jal sum # sum (a0,a1) sw $v0, 0($sp) # store sum(a0,a1)! lw $a0, 8($sp) # restore a2 to a0 lw $a1, 4($sp) # restore a3 to a1 jal sum # sum (b, c) lw $a0, 0($sp) # restore sum(a0,a1) move $a1, $v0 # move 2nd to a1 jal sum # final sum into $v0 lw $ra, 12($sp) # restore $ra addi $sp, $sp, 16 # deallocate space jr $ra # return main: li $a0, 1 li $a1, 1 li $a2, 2 li $a3, 3 jal nonleaf … <other code> … nonleaf: push $ra # save my $ra push $a3 # save a3 push $a2 # save a2 jal sum # a0/a1 are summed pop $a0 # restore a2 to a0 pop $a1 # restore a3 to a1 push sum # store result of a0/a1 jal sum # a0/a1 are used to sum a2/a3 pop $a0 # restore first result move $a1, $v0 # move second result to a1 jal sum # compute final sum into $v0 pop $ar # restore $ra jr $ra # return nonleaf uses 4 words of stack space...

23 nonleaf stack memory map
main: li $a0, 1 li $a1, 1 li $a2, 2 li $a3, 3 jal nonleaf … <other code> … nonleaf: addi $sp, $sp, -16 # allocate space sw $ra, 12($sp) sw $a2, 8($sp) # store a2 sw $a3, 4($sp) # store a3 jal sum # a0/a1 are summed sw $v0, 0($sp) # store $v0 to stack! lw $a0, 8($sp) # restore a2 to a0 lw $a1, 4($sp) # restore a3 to a1 jal sum # a0/a1 are used to sum a2/a3 lw $a0, 0($sp) # restore first result move $a1, $v0 # move second result to a1 jal sum # compute final sum into $v0 lw $ra, 12($sp) # restore $ra addi $sp, $sp, 16 # deallocate space jr $ra # return nonleaf stack memory map $fp = $sp+16 BOTTOM of stack $sp+12 nonleaf’s $ra $sp+8 a2 (c) $sp+4 a3 (d) $sp+0 sum(a,b)

24 General Stack Allocation
callee caller return to caller stack frame or activation record: everything stored on the stack for a particular function. $sp points to the end of the record, $fp points to the start of the record (initial $sp before the function changes it)! Access local variables relative to $sp or $fp, it’s up to you!

25 Procedure Call Outline
prepare arguments for procedure jal to procedure allocate all stack space for local variables and to store any preserved registers that will be changed. do some work. restore stack and preserved registers for caller. place result where the caller expects. jr $ra. process result

26 Square in asm using mult

27 power with mult


Download ppt "CSCI206 - Computer Organization & Programming"

Similar presentations


Ads by Google