Class Notes (1,100,000)
CA (620,000)
McGill (30,000)
COMP (700)
Lecture 11

COMP 273 Lecture Notes - Lecture 11: High-Level Programming Language, C Dynamic Memory Allocation, Current Contents

Computer Science (Sci)
Course Code
COMP 273
Piotr Przytycki

This preview shows pages 1-3. to view the full 9 pages of the document.
COMP 273 11 - functions Feb. 15, 2016
We have seen many of the 32 MIPS registers so far. Here is a complete table of them. In this
lecture, we will meet the last three of them: the stack pointer, the frame pointer (which we won’t
use, but it is good to be aware of it), and the return address. We will also discuss the rightmost
column in this table and what is meant by ”preserved on call”.
Functions (and procedures)
As you have learned in your first programming course, when a sequence of instructions is used many
times, it is better to encapsulate these instructions: give them a name and hide the inner details. In
Java, we call these encapsulated instructions methods. In C, we call them functions and procedures.
A ”procedure” is just a “function” that doesn’t return a value. In this lecture, we’ll look at the
basics of how C-like functions are implemented in MIPS. We won’t look at Java methods because
it would take us off track – I would first need to tell you how classes and their instances (objects)
are represented. If time permits, I may say something about this at the end of the course.
When we write a MIPS function, we label the first instruction with the name that we give the
function. Later we can branch to the function by using this label name. The label just indicates the
address to branch to. Instructions that define a function belong to the instruction (text) segment
of Memory, namely below 0x10000000 – see slides at end of lecture 9. We’ll see an example on p.
3 of this lecture.
To pass arguments to functions and return values from functions, we use the data segment of
Memory, in particular, a region called the stack. You learned about stacks as an abstract data type
in COMP 250. In MIPS, the stack has a specific technical meaning which I will tell you about
last updated: 16th Feb, 2016 1 lecture notes c
Michael Langer

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

COMP 273 11 - functions Feb. 15, 2016
Suppose you have a C function:
int myfunction(int j){
int k;
: // other declarations here, and many instructions
return k;
Such a function would be called by another function, for example, main. We will refer to the
function that makes the call as the parent and the function that gets called as the child. (Caller
and callee are used, respectively.) In this example, main is the parent, and myfunction is the child.
int i,j,m;
m = myfunction(i);
j = myfunction(5*i);
How might this work in MIPS? main and myfunction are just labels for an instruction, namely
the first instruction in some sequence of instructions which are in the text segment of Memory.
There are a few important questions here: How does MIPS jump from the parent to child and
then later return from child to parent? How do these functions share registers and Memory? First,
let’s consider what main needs to do or consider when it calls my function:
it will need to save in Memory any values currently in registers that myfunction might destroy
it needs to provide a “return address” so myfunction can later jump back to the location in
main from where it was called;
it needs to provide arguments, so that myfunction can access them;
it will needs to access the result which will be returned from myfunction
it needs to branch to the first instruction of myfunction;
Next consider what myfunction needs to do:
it needs to access its arguments
it needs to allocate storage for its local variables
(optional) if it computes a result, then it needs to put this result in the place that main expects
it to be
it must not destroy any data (in Memory or in registers) that its parent (e.g. main) was using
at the time of the call and that its parent will need after the call
it needs to return to the parent.
The same rules should apply to any parent and child function. Let’s deal with these issues one by
last updated: 16th Feb, 2016 2 lecture notes c
Michael Langer

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

COMP 273 11 - functions Feb. 15, 2016
Jump to and return from a function
Suppose the first line of the MIPS code for myfunction is labelled myfunction. To branch to
myfunction, you might expect main to use the MIPS instruction:
j myfunction
This is not sufficient, however, since myfunction also needs to know where it is supposed to eventu-
ally return to. Here’s how it is done. When main branches to the instruction labelled myfunction,
it writes down the address where it is supposed to return to. This is done automatically by a
different branching function jal, which stands for “jump and link.”
jal myfunction
jal stores a return address in register $31, which is also called $ra.The return address is written
automatically as part of jal instruction. That is, $ra is not part of the syntax of the jal instruction.
Rather, jal has the same syntax as the jump instruction jwe saw earlier. It is a J format instruction.
So you can jump to 226 possible word addresses. Later when we look at the data path, we will see
exactly how this works and it will make more sense!
The return address is the address of the current instruction + 4, where the current instruction
is “jal myfunction”. That is, it is the address of the instruction following the call. (Recall each
instruction is 4 bytes or 1 word).
When myfunction is finished executing, it must branch back to the caller main. The address to
which it jumps is in $ra. It uses the instruction
jr $ra
where jr stands for jump register. This is different from the jump instruction jthat we saw
earlier. jr has R-format.
So the basic idea for jumping to and returning from a function is this:
myfunction: .
jr $ra
main: .
jal myfunction
”Passing” arguments to and returning values from functions
The next issue to address is how arguments are passed to a function, and how a value computed by
the function is returned. When writing MIPS code, one uses specific registers to pass arguments
last updated: 16th Feb, 2016 3 lecture notes c
Michael Langer
You're Reading a Preview

Unlock to view full version