Professional Documents
Culture Documents
January 2017
ENCM 369 Winter 2017 Slide Set 2 for Lecture Section 01 slide 2/47
Contents
Introduction to Procedures
Introduction to Procedures
Introduction to Procedures
jal uses a label, too. But jal also updates $ra to note where
to come back to when the called procedure has finished.
jr is most often used with $ra for procedure return, but can
be used with other GPRs for other purposes. The “r” in jr
stands for “register”, not “return”.
ENCM 369 Winter 2017 Slide Set 2 for Lecture Section 01 slide 12/47
int main(void) {
int d = 42, e = 10, f;
f = addsub(d, d - e, e + 5);
return 0;
}
int main(void) {
int d = 42, e = 10, f;
f = addsub(d, d - e, e + 5);
return 0;
}
In the example . . .
I d, d - e, and e + 5 are outgoing arguments from main;
Introduction to Procedures
Fixing a defect
How can we use the stack to help in writing correct A.L. code
for f2?
ENCM 369 Winter 2017 Slide Set 2 for Lecture Section 01 slide 28/47
Introduction to Procedures
Before alpha starts using $s0 or $s1, alpha should use the
stack to save copies of $s0 and $s1 in case alpha’s caller
(or caller’s caller, caller’s caller’s caller, etc.) is also using $s0
or $s1.
After alpha has finished using $s0 and $s1, and just before
alpha returns, alpha should read the old values of $s0
and $s1 back from the stack into $s0 and $s1.
ENCM 369 Winter 2017 Slide Set 2 for Lecture Section 01 slide 32/47
int alpha(void)
{
// Plan: use $s0
Let’s write a complete A.L.
// for b, $s1 for c.
implementation of alpha,
int b, c;
then study how it will
b = beta();
behave.
c = gamma();
return c + b * 8;
}
ENCM 369 Winter 2017 Slide Set 2 for Lecture Section 01 slide 33/47
# This might or
// a, b, c are ints # might not work!
// in $s0, $s1, $s2. add $t0, $s0, $s1 # $t0 = a + b
// f returns an int. addi $a0, $zero, 7 # $a0 = 7
c = a + b + f(7); jal f
add $s2, $t0, $v0 # c = $t0 + f rv
Introduction to Procedures
Introduction to Procedures
Moving on . . .