Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedures 2 Intructions Supporting Procedures Making Use of a Stack.

Similar presentations

Presentation on theme: "Procedures 2 Intructions Supporting Procedures Making Use of a Stack."— Presentation transcript:

1 Procedures 2 Intructions Supporting Procedures Making Use of a Stack

2 Outline Requirements for procedures Basic MIPS concepts Procedures calling procedures Using the stack

3 Last time... Motivation for Procedures Example of procedures Introduced the idea of a stack –push an item onto a stack –pop the “top” of the stack This “data structure” supports the implementation of the procedure idea.

4 Requirement for Procedures Consider this abstract code: instruction 1#some instruction instruction 2 call procA#go to procedure instruction 3#return here!....#other stuff procA:instructions for procA After procA has completed, control returns to the instruction after the call.

5 MIPS Implementation... jal procA – j ump a nd l ink jumps to the address specified by procA stores the address of the following instruction in register $31. –this is called the return address. jr $31 –used at the end of the procedure »to return back to the appropriate address

6 Example add2 - adds the numbers in registers $4 and $5 and puts the result in $6 addi $4,$0,10 #$4 = 10 li $5,20#$5 = 20 jal add2#call add2 add $4,$0,$6 li $5,25 jal add2 sw $6, X($20)#stores result add2 : add $6,$4,$5#procedure body jr $31#return

7 Procedures Calling Procedures Suppose we want a procedure ‘sprod’ that returns x1*y1 + x2*y2. Abstractly we can think of this as: sprod(x1,y1,x2,y2){ a = prod(x1,y1);#x1*y1 b = prod(x2,y2);#x2*y2 return add2(a,b);#a + b } Procedure ‘sprod’ calls ‘prod’ and ‘add2’.

8 Problem When Proc call Procs jal procAddress –causes $31 to contain the next address So each time that a procedure is called (eg, when ‘prod’ is called inside ‘sprod’) –$31 gets overwritten (‘clobbered’) »so that the return address for the outer procedure is no longer available! Stacks can get us out of this problem.

9 How stacks are used A stack is used to store the sequence of return addresses when there are nested calls The calling (outer) procedure –pushes address in $31 onto stack –calls the procedure ($31 contains return address) –pops stack to restore $31 (after procedure returns) The called procedure... before returning –if it has called another procedure, it overwrites $31 with a pop from the stack –jr $31

10 Main calls ProcA calls ProcB...main program......some instructions... jal procA [1]#[1] goes into $31 procA:...some instructions... push $31 jal procB [2]#[2] goes into $31...some instructions $31 gets pop() jr $31#jumps to [1]

11 ProcB calls ProcC procB:...some instructions... push $31 jal procC [3]#[3] goes into $31...some instructions $31 gets pop() jr $31#jumps to [2] procC:...some instructions... jr $31#jumps to [3]

12 Stack Pointer Register $29 is called the ‘stack pointer’ It contains an address in memory of the top of a stack In MIPS stacks grow ‘downwards’ When pushing or popping items onto or from the stack, we must adjust $29 Remember byte addressing!

13 Summary jal –jump and link »saves return address in $31 jr $31 –jumps to return address in register $31 Stack pointer used to store nested return addresses ($29)

14 Next Time Example MIPS code for nested procedures

Download ppt "Procedures 2 Intructions Supporting Procedures Making Use of a Stack."

Similar presentations

Ads by Google