



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A practice exam question related to understanding stack frames and argument passing in c++. It includes the syntax of a c++ program and requires the reader to write pseudo-code for mips assembly instructions to run the program. Additionally, the document discusses the concept of static and dynamic scoping for non-local variables.
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




CMSC 330 Practice Exam 2 - April 2000
Show all work necessary to justify your answers!
call-by-value call-by-value-result call-by-reference call-by-name
int i, j;
int main(int argc, char* argv[]) { i = 1; j = 2; f(i, j); cout << i << " " << j << '\n'; }
void f(int j, int k) { j = j - 9; k = k + 8; i = i - j; }
value: 9 2 value-result: -8 10 reference: 0 10 name: 0 10
program Example ; var A1,B1: integer;
procedure C(var N,Tot: integer);
procedure Show(Value:integer); begin writeln(Value); writeln(Tot); writeln(B1) end;
begin if (N >= 1) then begin Tot := Tot * N; N := N - 1; C(N,Tot) end else Show(Tot) end;
begin A1 := 4; B1 := 1; C(A1,B1) end.
You should assume the register conventions discussed in class, and parameter passing is done as was shown in class for Pascal (i.e. default is pass-by-value, unless explicitly sp eci ed pass-by-reference in a pro cedure header with var). The template for a single stack frame (activation record instance) is shown b elow.
(b) Provide the pseudo-co de that is necessary to access the values of the arguments to writeln in the three writeln statement in pro cedure Show: writeln(Value); writeln(Tot); writeln(B1)
Value is lo cal and passed by value:
la $t0, -56($fp) # only local variable in Show lw $t0, ($t0)
Tot is non-lo cal (from C) and pass-by-reference to C: lw $t0, -8($fp) # static link to ARI of C lw $t0, -60($t0) # Tot lw $t0, ($t0) # follow pointer to B
B1 is non-lo cal (from Example), so need to follow 2 static links
lw $t0, -8($fp) # static link to ARI for C lw $t0, -8($t0) # static link to ARI for Example lw $t0, -60($t0) # B
(c) What do es the program print? 24 24 24
int x, y;
int main(int argc, char* argv[]) { x = 2; y = 10; f(); cout << x << " " << y << '\n'; }
void g() { x = y / 5; }
void f() { int x; x = y + 6; y = 4 * y; g(); cout << x << '\n'; }
main: int * char** -> int
f, g: void -> void
x: int
x: int
y: int 40
16
8