You are on page 1of 3



UEEA4653 Computer Architecture

Experiment: Stack Frame and Procedure Calls

DUE DATE: 17th JULY 2015(Fri), 3:30pm
Everyone should hand in his/her REPORT and email me his/her PROGRAM
before the class starts
These experiments investigate the stack frame structure that makes procedure calling possible in MIPS.

The Stack Frame Structure

In MIPS, stack is located at the bottom of the memory address space and grows downward from high
memory address to low memory address. Register $sp or $29 is reserved for pointing to the top of the
stack. Prior to making a procedure or function call, it is critical for the caller to back up or push the
values in some working registers to stack such that the callee is free to use all the 32 registers without
the worry of corrupting the working context of caller. After the callee completes and returns the control
to caller, caller can restore or pop the content of the working registers from the stack. In some scenarios,
when the caller requires passing more than four arguments to the callee, the caller can also make use of
the stack to pass the extra arguments to the callee using a well defined allocation order.
It is a common practice to declare local variables in procedures or functions. As the scope of local
variables is within a procedure or function, the life span of local variables is short as compared to that of
global variables. As such, it is more cost-effective in terms of memory usage to allocate storage for local
variables on demand in the stack and reclaim the storage after the procedure or function has ended. To
support such a need, the callee reserves or pushes the local variables on the stack prior to the execution
of the procedure or function. At the end of the procedure call, it is the responsibility of callee to pop out
these local variables before executing the return instruction.
Put all of these together, it forms a stack frame, as illustrated in the figure below. Upon each procedure
or function call, one single stack frame is allocated. It could be the case that there are multiple frames
being stacked up during the program execution.

(A) Assembly program

Run the QtSpim.exe from your computer.

Click File menu on the top left corner, then select Reinitialize and Load File, and enter the
file, swap.s, which you just created.
You will see four tabs with labels of
o FP Regs: contents of floating pointer registers
o Int Regs: contents of integer registers
o Data: the content of data memory and stack; numbers in brackets [] are the starting memory
addresses in hexadecimal format for the particular data and stack elements.
o Text: the content of instructions; i.e. your MIPS assembly program is loaded here, starting at

(B) Understanding the program

Before you run the program, the separate window called Console should be empty.
Now, press F10 to execute once, write down what you see in the Console.
To execute again, you need to reinitialize the simulator and reload the file again by clicking
Simulator menu, then Reinitialize Simulator, and clicking File menu, then Recent Files.
To understand why we are getting such output, step through each instruction by pressing F10.

(C) Questions (20 pts)



What is the starting address of the User Data Segment? (1pt)

What is the starting address of the array toso in the program? (1pt)
What is the starting address of the string str1 and str2? (1pt)
What is the starting address of the variable indx? Why its address does not start immediately
after str2? (2pt)
What is the starting address of the main? (1pt)
The instruction la at # line 1 in the program does not show up in the User Text Segment, why?
What are the instructions replacing it? (2pt)
After executing # line 2, does the content of $t1 align with your finding in Q4? (1pt)
According to the definition of the stack frame described in previous page, which lines in the
program are related to the caller-saved registers, procedure arguments, return address, calleesaved registers, and automatic local variables, respectively? Note: not every component exists.
The stack pointer is decremented by 16 upon push, why 16, not 4 or other numbers? (1pt)
What is the purpose of # line 11 to # line 15? (1pt)
Explain how many parameters are passed to the function prnt and via what registers? (1pt)
What is the content of $t1 prior to executing the instruction at # line 52? What is the content of
$t1 after executing # line 53? (2pt)
Explain in details what # line 54 to # line 57 intend to achieve? (2pt)

(D) Allocating local variables (20 pts)

Using swap.s as a starting point and conforming to the stack frame structure explained above,
provide MINIMAL MIPS assembly codes which EXACTLY match the desired behavior of C
program below. Note that the selection function does not return value, instead it stores the outcome
into the address (*list, a pointer of the array data, means the beginning address of the array data)
provided by the caller. Hence, the caller needs to allocate a local variable, temp, in the stack.
void main()
int data[10] = {55, 44, 22, 33, 77, 0, 11, 99, 88, 44};
int size = 10, i;
selection(data, 0, 0, size, 1);
printf("The sorted list in ascending order is\n");
for (i = 0; i < size; i++)
printf("%d ", list[i]);
void selection(int *list, int i, int j, int size, int flag)
int temp;
if (i < size - 1)
if (flag)
j = i + 1;
if (j < size)
if (list[i] > list[j])
temp = list[i];
list[i] = list[j];
list[j] = temp;
selection(list, i, j + 1, size, 0);
selection(list, i + 1, 0, size, 1);

The marking criteria are

1. The assembly code runs and finishes without error and like the output of C code (3 marks)
2. The assembly code is the exact translation of the C code (6 marks)
3. Correct implementation of recursive calls and stack management (8 marks)
4. The outcome is correct (3 marks)