C++ Program Analysis: Understanding Stack Frames and Argument Passing, Exams of Programming Languages

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

Pre 2010

Uploaded on 02/13/2009

koofers-user-ntw
koofers-user-ntw 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 330 Practice Exam 2 - April 2000
Show all work necessary to justify your answers!
1. What will the following program, in C++ syntax, output if parameters are passed
with:
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
pf3
pf4
pf5

Partial preview of the text

Download C++ Program Analysis: Understanding Stack Frames and Argument Passing and more Exams Programming Languages in PDF only on Docsity!

CMSC 330 Practice Exam 2 - April 2000

Show all work necessary to justify your answers!

  1. What will the following program, in C++ syntax, output if parameters are passed with:

 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

  1. This question requires you to write pseudo-co de for several MIPS assembly language instructions that must b e executed to run the following program. Your pseudo-co de should b e as detailed as the assembly instructions, but we are not concerned ab out syntax. For example, the instruction \sw $fp, 0($sp)" can b e written as \store the frame p ointer at 0 past the stack p ointer."

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

  1. (a) If parameters are passed by value, and using static scoping rules, what do es the environment (name/value pairs, including functions) lo ok like just b efore function g returns, for the following program, which uses C++ syntax?

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'; }

Top-level

(global)

f

main: int * char** -> int

f, g: void -> void

x: int

x: int

y: int 40

16

8