lecture8

lecture8 - Lecture 8: Procedure Nested Procedure...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 8: Procedure + Nested Procedure Lab 1 ­3 Assigned •  •  •  •  Connect 4 Lab 1: input/output Lab2: winner detec?on Lab3: smarter program •  Various due dates: 4/29, 5/13, 5/20 Midterm •  5/3, 75 minutes •  Cover everything from number representa?on to assembly •  Will send out sample from past years •  Closed book, closed notes, no calculators REVIEW: PROCEDURE Instruc?on Support for Func?ons ... sum(a,b);... /* a,b:$s0,$s1 */ } C! int sum(int x, int y) { return x+y; } address 1000 add $a0,$s0,$zero # x = a # y = b M
 1004 add $a1,$s1,$zero #jump to sum I
 1012 jal sum 1016 ... P
 2000 sum: add $v0,$a0,$a1 S! 2004 jr $ra # new instruction Procedure calls Main Program … … … Sum(a,b) … … … … Function loc: … … … … … … … Instruc?on Support for Func?ons • Single instruc?on to jump and save return address: jump and link (jal) 1008 jal sum # $ra=1012?,go to sum • Why have a jal? Make the common case fast: func?on calls are very common. Also, you don’t have to know where the code is loaded into memory with jal. Instruc?on Support for Func?ons • Syntax for jr (jump register): jr register • Instead of providing a label to jump to, the jr instruc?on provides a register which contains an address to jump to • Only useful if we know exact address to jump • Very useful for func?on calls: – jal stores return address in register ($ra) – jr $ra jumps back to that address Rules for Procedures • Called with a jal instruc?on, returns with a jr $ra • Accepts up to 4 arguments in $a0, $a1, $a2 and $a3 • Return value is always in $v0 (and if necessary in $v1) Vola?le Register Conven?ons •  $ra: Can Change. The jal call itself will change this register. Caller needs to save on stack if nested call. •  $v0 ­$v1: Can Change. These will contain the new returned values. •  $a0 ­$a3: Can change. These are vola?le argument registers. Caller needs to save if they’ll need them a^er the call. •  $t0 ­$t9: Can change. That’s why they’re called temporary: any procedure may change them at any ?me. Caller needs to save if they’ll need them a^erwards. int sumSquare(int x, int y) { return mult(x,x)+ y; } NESTED PROCEDURES Nested Procedures int sumSquare(int x, int y) { return mult(x,x)+ y; } • Something called sumSquare, now sumSquare is calling mult. caller callee & caller $ra jal loc … … … … another callee loc: $ra … jal loc2 … … jr $ra loc2: … … … … … … … jr $ra … Problem on Nested Procedure caller callee & caller … $ra jal loc … … … … another callee loc: $ra X … jal loc2 … … jr $ra loc2: … … … … … … … jr $ra … Nested Procedures int sumSquare(int x, int y) { return mult(x,x)+ y; } •  Something called sumSquare, now sumSquare is calling mult. •  So there’s a value in $ra that sumSquare wants to jump back to, but this will be overwri6en by the call to mult. •  Need to save sumSquare return address before call to mult. Nested Procedures •  In general, may need to save some other info in addi?on to $ra. •  When a C program is run, there are 3 important memory areas allocated: – Sta?c: Variables declared once per program, cease to exist only a^er execu?on completes. E.g., C globals – Heap: Variables declared dynamically – Stack: Space to be used by procedure during execu?on; this is where we can save register values C Memory Alloca?on Address!∞! Heap! $sp ! stack! pointer! ! Stack! Space for saved 
 procedure information! Explicitly created space, 
 e.g., malloc(); C pointers! Variables declared! Static! once per program! Code! 0! Program! Using the Stack •  So we have a register $sp which always points to the last used space in the stack. •  To use stack, we decrement this pointer by the amount of space we need and then fill it with info. •  So, how do we compile this? int sumSquare(int x, int y) { return mult(x,x)+ y; } Using the Stack • Hand ­compile int sumSquare(int x, int y) { return mult(x,x)+ y; } sumSquare: addi $sp,$sp,-8 “push”! sw $ra, 4($sp) sw $a1, 0($sp) # space on stack # save ret addr # save y add $a1,$a0,$zero # mult(x,x) jal mult # call mult lw $a1, 0($sp) add $v0,$v0,$a1 lw $ra, 4($sp) addi $sp,$sp,8 “pop”! jr $ra mult: ... # # # # restore y mult()+y get ret addr restore stack Nested Procedure caller … $ra callee & caller jal loc … … … … loc: sub $sp, 4 sw $ra, ($sp) … jal loc2 $ra … … lw $ra, ($sp) add $sp, 4 jr $ra another callee loc2: … … … … … … … jr $ra … Basic Structure of a Func?on Prologue! entry_label: addi $sp,$sp, -framesize sw $ra, framesize-4($sp) #save $ra .. save other regs if need be Body ... Epilogue! ra" (call other functions…)! memory" ..restore other regs if need be lw $ra, framesize-4($sp) addi $sp,$sp, framesize jr $ra #restore $ra EXERCISE  ­ ­ FIBONACCI NUMBERS F(n) = F(n – 1) + F(n – 2) F(0) = F(1) = 1 ...
View Full Document

This note was uploaded on 12/27/2011 for the course CMPSC 64 taught by Professor Zheng during the Fall '09 term at UCSB.

Ask a homework question - tutors are online