Procedures 2 Intructions Supporting Procedures Making Use of a Stack.

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

Procedures 2 Intructions Supporting Procedures Making Use of a Stack

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

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.

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.

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

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’.

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.

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

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]

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]

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!

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)

Next Time Example MIPS code for nested procedures

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

Similar presentations