Procedure Calls
You are going to convert a simple program (on the next page, you can copy-paste) into a simple leaf procedure (leaf means it doesn’t call anything else)
Fairly simply, you do this with labels – label your program above something useful (say, multiply), read in two integers from the user, and “jal multiply”
Ok so that’s what you’ve done before basically.
In a real program, you may have numerous procedures (functions or methods) being called, sometimes many layers deep, these are called nested procedures and we’re getting there. The goal here is to think from the perspective of the method being called (that is the ‘callee’)– you want to provide a service to a caller without mucking up its data.
So what we need to do is to push the caller data onto a stack (and keep track of what is on the stack) execute our procedure, and then restore the stack.
Conceptually think of a fairly simple problem
Foo(param1, param2)
{
Int a, b;
Bar();
}
Bar(){
Int a, b;
}
So what happens to “a” and “b”? Broadly speaking this is also the problem of scope, and differently languages behave differently in terms of how nested functions can see caller variables and so on (and if it can see them, how you access them).
We are working closer to the metal than that. We have a piece of data in a specific memory location, but what if some called function wants to use the same memory location but to store something else, after all, if I’m the callee I can’t account for all possible memory states when I get called.
The convention for MIPS $s registers saved, $t registers are… well, they *can* be saved. (Ideally $s are caller saved, $t callee saved, but you probably won’t do that).
There are two ways to do this – caller and callee saving (caller saving, above, would mean Foo( , ) would save a and b, then call Bar(), then restore a, and b), callee saving would be Foo calls Bar, bar saves a and b, then restores them.
Ok some terminology,
program counter: the memory location of the instruction being executed. After executing an instruction you go to current program counter + 4 since instructions are 4 bytes long. (You never work with the PC number directly, but it is stored in the return address).
$ra : is the return address. It’s where you jump to, to return to a previous state (e.g. when you are done your procedure you jr $ra and you are back at the calling function). You need to keep track of what is in the $ra if you want to have many procedures chained together though. Technically jal to a label saves the current program counter (the location of the instruction being executed)
$sp : the stack pointer. When you push things (whatever they are) onto the stack you need to pop them back off when you are done and put them back where they belong.
How do we do this, we want to Push variables onto the stack to save them, and then pop them off when we’re done.
$sp is the stack pointer, it’s supposed to be the memory address that is the top of the stack. The custom is to push onto the stack from larger memory addresses to smaller. You do this by copying something to the next available memory address, and then adjusting the stack pointer
So to push something onto the stack you save it at memory location $sp-4, then you set $sp=$sp-4
To Pop something off the top of the stack is $sp (the top element) and then $sp=$sp+4
CS 340 Milestone One Guidelines and Rubric Overview: For this assignment, you will implement the fundamental operations of create, read, update,
Retail Transaction Programming Project Project Requirements: Develop a program to emulate a purchase transaction at a retail store. This
7COM1028 Secure Systems Programming Referral Coursework: Secure
Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip
CS 340 Final Project Guidelines and Rubric Overview The final project will encompass developing a web service using a software stack and impleme