CS360 Midterm Exam: March 2, 2004. Answer to Question 5
Before answering the question, let's label memory:
0xfff42c: The current top of the stack. This contains nothing
right now, but when a() is called, it will contain
pc+4: 0x1068.
0xfff430: Parameter #1 for a().
0xfff434: Parameter #2 for a().
0xfff438: Although we know a() takes three parameters, this
plus the memory value below it certainly appear to be an old frame
pointer and program counter. This is the old frame pointer for
main() when it called b().
0xfff43c: This is the old program counter (plus 4) for
main() when it called b().
0xfff440: Parameter #1 for b().
0xfff444: Parameter #2 for b().
0xfff448: Local variable i for main().
0xfff44c: This is the old frame pointer that will be restored
when main() returns.
0xfff450: This is the old program counter that will be restored
when main() returns.
0xfff454: This is argc for main().
0xfff458: This is argv for main().
0xfff45c: This is envp for main() (you don't really
need to care about it, but that's what it is).
0xfff460: This is argv[0].
0xfff464: This is argv[1].
0xfff468: This is argv[2] (again, you don't really need
to care about it, but that's what it is).
Now, onto the answers.
Part a. When b() returns, it will set the program
counter to 0x108c. Therefore, the 'jsr b' statement is
at: 0x1088.
Part b.Argc is at memory location 0xfff454. Its
value is: 2.
Part c. See above: 0xfff448.
Part d. See above: 0xfff430.
Part e. 0xfff475 is the first character of argv[1].
From memory location 0xfff444, we know that atoi(argv[1]) is 4.
Therefore argv[1][0] is most likely the character '4' (i.e.
the ascii value for '4'. Note, it could be whitespace too (since
atoi() ignores leading whitespace. Therefore, I'll accept the
answers: '4', or "either '4' or a whitespace character."
int's: no.
Part f. 0x1068 will be stored in location 0xfff42c.
0xfff434 will be stored in location 0xfff428. The new frame pointer
wil be: 0xfff424.
Part g. The value of r1 will be the contents of
[fp+20]. This will be the contents of 0xfff438: 0xfff448.
Part h. The programming error is that a() has been
called with only two parameters instead of three. Although I didn't
ask the question, the result will be that i in main()
will be set to 9.