Item | Detail |
|---|---|
Course learning outcomes | CLO6 - design and analyze low-level software in MIPS assembly (P3); CLO3 - instruction set principles: procedure support and the stack (C2). |
Tool | MARS 4.5: Execute, Registers window ($sp, $ra, $s0-$s7), Data Segment window set to 'current $sp', Run > Step |
Linked lectures | Practical 13 (jal, jr, arguments); Lecture 9 (call and return); Lecture 8 (displacement addressing, word alignment). |
Assessment level | Psychomotor P3 (Guided Response): you perform each step by following the guided procedure, then on your own. |
1. Objectives
By the end of this practical you will be able to:
- Push and pop words on the runtime stack with $sp and read the stack in the MARS Data Segment.
- Save $ra and $s registers in a stack frame so that a procedure can call another procedure.
- Write non-leaf procedures that follow the MIPS calling convention and leave $sp unchanged.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Stack | Memory used last-in, first-out (LIFO) | A pile of plates: the last one put on is the first taken off |
$sp (stack pointer) | Register holding the address of the top of the stack | Your finger on the top plate |
Push | Make room ($sp - 4) and store a word | Put a plate on the pile |
Pop | Load the top word and free its room ($sp + 4) | Take the top plate off |
Stack frame | The block of stack a procedure uses for its saved registers | A locker given to each visitor |
Non-leaf procedure | A procedure that calls another procedure | A manager who passes part of a job to a clerk |
Callee-saved | Registers a procedure must give back unchanged ($s0-$s7, $ra, $sp) | A borrowed book must be returned as it was |
Caller-saved | Registers a procedure may change ($t, $a, $v) | A shared scratch pad anyone may use |
Alignment | A word address must be a multiple of 4 | Parking only inside the painted bays |
3. Background
3.1 The stack in MARS
MARS starts every program with $sp = 0x7FFFEFFC, near the top of user memory. The stack grows DOWN, towards smaller addresses. To push one word: addi $sp, $sp, -4 then sw $t0, 0($sp). To pop it: lw $t0, 0($sp) then addi $sp, $sp, 4. To see the stack, set the Data Segment window's drop-down to 'current $sp'.
3.2 Why a non-leaf procedure needs the stack
A procedure that calls another one executes a jal, which OVERWRITES $ra. Its own return address is lost unless it is saved first. The same holds for $s registers: the caller expects them unchanged, so a procedure that uses one must save it and restore it.
Frame pattern (prologue and epilogue) |
Must be the same after the call (callee saves) | May change (caller saves if it needs them) |
|---|---|
$s0-$s7, $sp, $fp, $gp, and $ra in a non-leaf procedure | $t0-$t9, $a0-$a3, $v0-$v1 |
Offsets in a frame are multiples of 4 (word alignment, Lecture 8).
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-5 | Background 3.1-3.2 | - |
5-12 | Part A: push and pop with Step | - |
12-16 | Part B: predict the output | - |
16-22 | Part C: bug hunt | - |
22-46 | Part D: graded tasks 1-3 | 10 |
46-50 | Part E: post-lab questions; save; submit | - |
Part A - Warm-up: Push and Pop
Two batsmen scored 45 and 78; the program keeps both scores on the stack and then adds them (the partnership). Type the program, save it as P14_A1_trace.asm, set the Data Segment drop-down to 'current $sp' and Step (F7) through it. After each step write down $sp and the two stack words.
P14_A1_trace.asm |
Fill the table while stepping (words in decimal).
After | $sp | word at 0x7FFFEFF8 | word at 0x7FFFEFF4 | $t2 / $t3 |
|---|---|---|---|---|
start (after li $t1, 78) | ||||
step 1: addi $sp, $sp, -8 | ||||
step 2: sw $t0, 4($sp) | ||||
step 3: sw $t1, 0($sp) | ||||
step 4: lw $t2, 0($sp) | ||||
step 5: lw $t3, 4($sp) | ||||
step 6: addi $sp, $sp, 8 |
A1. Why does $sp go DOWN by 8 before anything is stored?
A2. After step 6 the Data Segment still shows 45 and 78. Are they still 'on the stack'?
A3. What does the program print, and which value did each lw get?
Part B - Predict the Output
Predict first, then run. For both, also predict the final value of $sp.
B1. Last in, first out
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. A procedure that calls a procedure
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
Part C - Bug Hunt
Each program has ONE bug. Say whether it is an assemble error, a runtime error or a logic error, give the line number, and fix it.
C1. Pakistan
P14_C1_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Bills with tax
P14_C2_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. An 8-byte frame
P14_C3_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
Part D - Graded Tasks (10 marks)
Type, run and save each program (P14_T1.asm, P14_T2.asm, P14_T3.asm). At the end of every program $sp must be back at 0x7FFFEFFC - check it in the Registers window.
Task 1 [Easy · 3 marks]: Roll numbers in reverse order
Read three roll numbers with a loop, pushing each one on the stack. Then pop and print them with a second loop: they come out in reverse order. What is the lowest value of $sp during the run?
Sample run:
MARS Run I/O |
Record: file name of your program, and paste your output here (code goes in the lab book): |
Task 2 [Medium · 3 marks]: Highest of three scores: max3 calls max2
Write a leaf procedure max2(a0, a1) → v0 and a non-leaf procedure max3(a0, a1, a2) → v0 that calls max2 twice. max3 must save $ra and the third score on the stack (max2 is free to change $a2). main reads three scores and prints 'Highest = ...'.
Step to the second 'jal max2' and write down the two words in max3's frame.
Sample run:
MARS Run I/O |
Record: file name of your program, and paste your output here (code goes in the lab book): |
Task 3 [Challenge · 4 marks]: Grade report with a full stack frame
main keeps the year 2026 in $s0 and calls report(a0 = address of marks, a1 = count). report keeps its pointer, counter and the current letter in $s0, $s1 and $s2, so it must save $ra, $s0, $s1 and $s2 in a 16-byte frame. For each mark it calls the leaf procedure grade(a0 = mark) → v0 = 'A' (80+), 'B' (65+), 'C' (50+) or 'F', and prints the letter. After report returns, main prints the year.
Draw report's frame with each offset and the value MARS shows while grade is running.
Sample run:
MARS Run I/O |
Record: file name of your program, and paste your output here (code goes in the lab book): |
Part E - Post-Lab Questions
E1. In Task 3, why must report save $ra but grade need not?
E2. Delete the sw and lw of $s0 in report (Task 3) and run again. MARS prints 'Year still 268501012'. What is this number?
E3. Why are return addresses saved on a stack and not in one fixed memory word?
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Reverse order; push/pop correct; $sp restored | Output right, $sp not restored (2) | Does not assemble |
T2 | 3 | All tests; $ra and a2 saved; frame values | Works for some inputs (2) | No attempt |
T3 | 4 | Letters, 16-byte frame drawn, year kept, $sp | Letters right, frame wrong (2) | No attempt |
Submission Rules
- Save files as P14_T1.asm, P14_T2.asm and P14_T3.asm in a folder named RollNo_P14.
- Copy the Part A table, the Task 2 frame words and the Task 3 frame drawing into the lab book.
- Submit the zipped folder on the LMS by the end of the session; late submission loses 1 mark per day.
- Each student writes his/her own program. Copied work gets zero for both students.