Sie sind auf Seite 1von 22

CPS 104 Computer Organization and Programming Lecture-14 : Functions and Procedures.

Feb. 11, 2004 Gershon Kedem http://kedem.duke.edu/cps104/Lectures

CPS104 Lec-14.1

GK Spring 2004

Admin.

Homework-2 is online. Due: Today!, Feb. 11, 11:59pm. Use submit to hand-in your homework.

CPS104 Lec-14.2

GK Spring 2004

Review: Memory Layout


0x7fffffff

Stack segment

Dynamic data

Data segment
Static data 0x10000000

Text segment
0x400000
CPS104 Lec-14.3

Reserved

GK Spring 2004

Review: Call-Return Linkage: Stack Frames


High Mem
Argument 6 Argument 5 Reference is relative to the stack pointer $SP

Callee Save Registers


ra, [a0-a3] [s0-s6]

Stack Frame

Local Variables SP Low Mem

Many variations on stacks possible (up/down, last pushed / next ) Block structured languages contain link to lexically enclosing frame Compilers normally keep scalar variables in registers, not memory!
GK Spring 2004

CPS104 Lec-14.4

Review: MIPS/GCC Procedure Calling Conventions Calling Procedure:

Step-1: Save caller-saved registers u Save registers $t0-$t9 if they contain live values at the call site. Step-2: Pass the arguments: u The first four arguments are passed in registers $a0-$a3 u Remaining arguments are pushed into the stack

(in reversed order arg5 is at the top of the stack).

Step-3: Execute a jal instruction.

CPS104 Lec-14.5

GK Spring 2004

Review: MIPS/GCC Procedure Calling Conventions (cont.)


Called Routine

Step-1: Establish stack frame. u Subtract the frame size from the stack pointer. subiu $sp, $sp, <frame-size> u Typically, minimum frame size is 32 bytes (8 words). Step-2: Save callee saved registers in the frame. u Register $ra is saved if routine makes a call. u Registers $a0-$a3 are saved if they are changed. u Registers $s0-$s7 are saved if they are used.

CPS104 Lec-14.6

GK Spring 2004

Review: MIPS/GCC Procedure Calling Conventions (cont.)

On return from a call


Step-1: Put returned values in registers $v0, [$v1]. (if values are returned) Step-2: Restore callee-saved registers. t Restore $sp and other saved registers. [$ra,$a0-$a3,$s0-$s7] Step-3: Pop the stack t Add the frame size to $sp. addiu $sp, $sp, <frame-size> Step-4: Return t Jump to the address in $ra. jr $ra
GK Spring 2004

CPS104 Lec-14.7

MIPS / GCC Calling Conventions


SP ra low address

fact: subiu $sp, $sp, 32 sw $ra, 20($sp) addiu $fp,$sp,28 . . . sw $a0, 0($fp) ... lw $ra, 20($sp) addiu $sp,$sp,32 jr $ra

SP ra ra

SP ra ra

First four arguments are passed in registers!


CPS104 Lec-14.8
GK Spring 2004

Example2B
# Example for CPS 104 # Program to add together list of 9 numbers. .text .align .globl main: subu sw sw sw sw stack sw move move la la la $s0, 20($sp) $v0, $0 $s1, $s0, $s2, $s3, $0 list msg list+36 # / #/ initialize exit code to 0 #\ # \ Initialization # / #/ $sp, $ra, $s3, $s2, $s1, 40 36($sp) 32($sp) 28($sp) 24($sp) # Code 2 main # MAIN procedure Entrance #\ Push the stack # \ Save return address # \ # > Entry Housekeeping # / save registers on

CPS104 Lec-14.9

GK Spring 2004

Example2B (cont.)
# again: lw add li move syscall li move syscall li la syscall addiu bne $t6, 0(s0) $s1, $s1, $t6 $v0, 4 $a0, $s2 $v0, 1 $a0, $s1 $v0, 4 $a0, nln Main code segment # Begin main loop #\ #/ Actual "work" # SPIM I/O #\ # > Print a string #/ #\ # > Print a number #/ #\ # > Print a string (eol) #/ #\ index update and #/ end of loop

$s0, $s0, 4 $s0, $s3, again

CPS104 Lec-14.10

GK Spring 2004

Example2B (cont.)
# move lw lw lw lw lw addu jr .end # $v0, $s0, $s1, $s2, $s3, $ra, $sp, $ra main Exit Code $0 20($sp) 24($sp) 28($sp) 32($sp) 36($sp) 40 #\ # \ # \ # \ Closing Housekeeping # / restore registers # / load return address # / Pop the stack #/ exit(0) ; # end of program

Data Segment .data # Start of data segment .word 35, 16, 42, 19, 55, 91, 24, 61, 53 .asciiz "The sum is " .asciiz "\n"

list: msg: nln:

CPS104 Lec-14.11

GK Spring 2004

Example: Factorial main() { printf("The factorial of 10 is %d\n", fact(10)); } int fact (int n) { if (n < 1) return(1); return (n * fact (n-1)); }

CPS104 Lec-14.12

GK Spring 2004

.text .global main main: subiu sw li jal la move jal lw addu jr .rdata LC: .asciiz "The factorial of 10 is %d\n" $sp, $sp,32 #stack frame size is 32 bytes $ra,20($sp) #save return address $a0,10 fact $a0 LC $a1,$v0 printf $ra,20($sp) $sp, $sp,32 $ra # load argument (10) in $a0 #call fact #load string address in $a0 #load fact result in $a1 # call printf # restore $ra # pop the stack # exit()

CPS104 Lec-14.13

GK Spring 2004

.text fact: subiu sw sw lw bgtz li j L2: lw sub move jal lw mul L1: lw addiu jr $ra,20($sp) $sp,$sp,32 $ra $v1,28($sp) $v0,$v1,1 $a0,$v0 fact $v1,28($sp) $v0,$v0,$v1 # # # # # # load n compute n-1 load argument (n-1) into $a0 call fact load n fact(n-1)*n $sp,$sp,32 $ra,20($sp) $a0,28($sp) $v0,28($sp) $v0, L2 $v0, 1 L1 # stack frame is 32 bytes #save return address # # # # # save argument(n)on stack load n from stack if n>0 go to $L2 return(1)

# return (result in $v0) # restore $ra # pop the stack #return

CPS104 Lec-14.14

GK Spring 2004

Example: Factorial
Stack
Old $ra Main

Old $ra Old $a0=10 Old $ra Old $a0=9 Old $ra Old $a0=8 Old $ra Old $a0=7 Old $ra Old $a0=6 Old $ra Old $a0=5
CPS104 Lec-14.15

fact(10)

fact(9)

Stack grows

fact(8)

fact(7)

fact(6)

fact(5)
GK Spring 2004

Example: Binary Tree


class Tree_node { public: int me; Tree_node *left; Tree_node *right; } ; // node number // left sub-tree pointer // right sub-tree pointer

main() { Tree_node *ar = new Tree_node[31]; // The tree has 31 nodes. Tree_node *p = ar; // p = <top of the array>. int k; for (k = 0; k < 31; k++){ // initialize all nodes. ar[k].me = k; if( (2*k+2) < 31 ) { // if it is an interior node, ar[k].left = &ar[2*k+1]; // set left and right pointers ar[k].right = &ar[2*k+2]; } else{ // if it is a leaf node ar[k].left = NULL; // set the left and right pointers ar[k].right = NULL; // to NULL. } } print_tree(p); // print the tree nodes in preorder. } CPS104 Lec-14.16
GK Spring 2004

Example: Binary Tree

void print_tree(Tree_node * p) { // a recursive procedure to print a binary tree in preorder. if( p == NULL) return; cout << p << endl; // if the tree is null, return // print the node address // print the left subtree // print the right subtree

print_tree(p->left); print_tree(p->right); }

CPS104 Lec-14.17

GK Spring 2004

Example: Binary Tree


4 bytes
class Tree_node { public: int me; Tree_node *left; Tree_node *right; } ;

ar[i].me ar[i].left ar[i].right

&(ar[i].me) = ? &(ar[i].left = ? &(ar[i].right = ?

Tree_node ar[31] ;
.align 2 .text .globl main main: subiu sw

$sp, $sp, 32 $ra, 20($sp)

# stack frame 32 bytes # save return address ar = new Tree_node[31] ;

# allocate the tree node array: li $a0 372 li $v0 9 syscall # # #

31 nodes, 12 bytes each. get a block from sbrk $v0 points to the root $v0=ar ;

CPS104 Lec-14.18

GK Spring 2004

Example: Binary Tree


# for (k=0; k<31 ; k++) { move $t0 $0 LP: mul $t1, $t0, 12 addu $t2, $v0, $t1 sw $t0, 0($t2) mul $t3, $t0, 2 addi $t3, $t3, 2 mul $t4, $t3, 12 addu $t5, $v0, $t4 bge $t3, 32, L1 sw $t5, 8($t2) subiu $t5, $t5, 12 sw $t5, 4($t2) j LD L1: sw $0, 4($t2) sw $0, 8($t2) LD: addi $t0, $t0, 1 blt $t0, 31 LP # }

# k = 0; # $t1 = k*12 # $t2 = &ar[k] # ar[k].me = k ; # # $t3 = 2*k+2 # # $t5 = &ar[2*k+2]; # if( (2*k+2) < 31 ) then{ # ar[k].right = &ar[2*k+2] ; # $t5 = &ar[2*k+1] ; # ar[k].left = &ar[2*k+1]} # go to LD # else { ar[k].left = NULL; # ar[k].right = NULL;} # k++ # if k < 31 go to LP end of for() loop

CPS104 Lec-14.19

GK Spring 2004

Example: Binary Tree

move jal

$a0, $v0 print_tree

# $a points to root node # go print it # do exit(0) ; # restore return address # pop the stack frame

li $v0 ,0 lw $ra, 20($sp) addiu $sp, $sp, 32 j $ra

CPS104 Lec-14.20

GK Spring 2004

Example (cont.)
.text print_tree: subiu sw sw beqz li syscall li la syscall lw lw jal lw lw jal pr_done: lw addu j .data mystr: .asciiz $sp, $ra, $a0, $a0, $v0, $sp, 32 20($sp) 12($sp) pr_done 1 # # # # # # stack frame 32 bytes save return address save node_ptr if null, return prepare to print int print it out

$v0, 4 $a0, mystr $a0, 12($sp) $a0, 4($a0) print_tree $a0, 12($sp) $a0, 8($a0) print_tree $ra, 20($sp) $sp, $sp, 32 $ra "\n"

#print end_of_line # restore node_ptr # load left ptr # restore node_ptr # load right ptr # return code # restore return address # remove frame

CPS104 Lec-14.21

GK Spring 2004

Simple functions
If a function does not call other functions one can simplify!

int min(int a, int b) { if(a <= b) return(a) ; return(b); }

min:

L1:

bgt move jr move jr

$a0, $a1, L1 $v0, $a0 $ra $v0, $a1 $ra

CPS104 Lec-14.22

GK Spring 2004

Das könnte Ihnen auch gefallen