Documente Academic
Documente Profesional
Documente Cultură
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
Stack segment
Dynamic data
Data segment
Static data 0x10000000
Text segment
0x400000
CPS104 Lec-14.3
Reserved
GK Spring 2004
Stack Frame
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
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
CPS104 Lec-14.5
GK Spring 2004
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
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
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
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
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"
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)
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
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
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
Tree_node ar[31] ;
.align 2 .text .globl main main: subiu sw
31 nodes, 12 bytes each. get a block from sbrk $v0 points to the root $v0=ar ;
CPS104 Lec-14.18
GK Spring 2004
# 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
move jal
# $a points to root node # go print it # do exit(0) ; # restore return address # pop the stack frame
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!
min:
L1:
CPS104 Lec-14.22
GK Spring 2004