Nexus Learn

Practical 14: The Runtime Stack and Non-Leaf Procedures

Lab Practical CS-704 Advanced Computer Architecture 10 min read Updated 8 Nov 2026
On this page
  1. 1. Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A - Warm-up: Push and Pop
  6. Part B - Predict the Output
  7. Part C - Bug Hunt
  8. Part D - Graded Tasks (10 marks)
  9. Part E - Post-Lab Questions
  10. Marking Rubric
  11. Submission Rules

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)
proc:
addi $sp, $sp, -8 # frame of 2 words
sw $ra, 4($sp) # save return address
sw $s0, 0($sp) # save caller's $s0
... # body: may use $s0 and call other procedures
lw $s0, 0($sp) # restore
lw $ra, 4($sp)
addi $sp, $sp, 8 # free the frame: $sp as on entry
jr $ra

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
# Practical 14 - Part A: push two scores, then pop them
.text
main:
li $t0, 45 # score of batsman 1
li $t1, 78 # score of batsman 2
addi $sp, $sp, -8 # step 1: make room for 2 words
sw $t0, 4($sp) # step 2: push 45
sw $t1, 0($sp) # step 3: push 78
lw $t2, 0($sp) # step 4: read the top word
lw $t3, 4($sp) # step 5: read the next word
addi $sp, $sp, 8 # step 6: free the room
add $a0, $t2, $t3 # partnership
li $v0, 1
syscall
li $v0, 10
syscall

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
main:
li $t0, 1
addi $sp, $sp, -4
sw $t0, 0($sp) # push 1
li $t0, 2
addi $sp, $sp, -4
sw $t0, 0($sp) # push 2
li $t0, 3
addi $sp, $sp, -4
sw $t0, 0($sp) # push 3
li $t1, 3 # pop and print three times
again:
lw $a0, 0($sp)
addi $sp, $sp, 4
li $v0, 1
syscall
addi $t1, $t1, -1
bne $t1, $zero, again
li $v0, 10
syscall

My prediction (write BEFORE running):

Actual output in MARS / was I right? Why?

B2. A procedure that calls a procedure

.text
main:
jal outer
li $v0, 10
syscall

outer: # non-leaf: prints [ , calls inner, prints ]
addi $sp, $sp, -4
sw $ra, 0($sp) # save return address
li $v0, 11
li $a0, '['
syscall
jal inner
li $v0, 11
li $a0, ']'
syscall
lw $ra, 0($sp) # restore return address
addi $sp, $sp, 4
jr $ra

inner: # leaf: prints *
li $v0, 11
li $a0, '*'
syscall
jr $ra

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
# Print "Pakistan" and a new line, using two procedures
.data
name: .asciiz "Pakistan"
nl: .asciiz "\n"
.text
main:
jal report
li $v0, 10
syscall

report: # non-leaf: calls newline
li $v0, 4
la $a0, name
syscall
jal newline
jr $ra

newline: # leaf
li $v0, 4
la $a0, nl
syscall
jr $ra

What MARS shows
Pakistan
... (nothing more is printed, but the program never ends - press Stop)

Bug type (assemble error / runtime error / logic error), line number, and your fix:

C2. Bills with tax

P14_C2_bug.asm
# Two electricity bills; the total is kept in $s0
.data
msg: .asciiz "Total with tax = Rs "
.text
main:
li $s0, 0 # running total
li $a0, 2000 # bill 1
jal withtax
add $s0, $s0, $v0
li $a0, 3000 # bill 2
jal withtax
add $s0, $s0, $v0
li $v0, 4
la $a0, msg
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 10
syscall

withtax: # withtax(a0 = bill) -> v0 = bill + 10% tax
li $s0, 10
div $a0, $s0 # bill / 10
mflo $v0
add $v0, $v0, $a0
jr $ra

What MARS shows
Total with tax = Rs 3310

Bug type (assemble error / runtime error / logic error), line number, and your fix:

C3. An 8-byte frame

P14_C3_bug.asm
# Save $ra and $s0 in a stack frame of 8 bytes
.text
main:
li $s0, 5
jal work
move $a0, $s0
li $v0, 1
syscall
li $v0, 10
syscall

work:
addi $sp, $sp, -8
sw $ra, 2($sp) # save $ra
sw $s0, 0($sp) # save $s0
li $s0, 99
lw $s0, 0($sp)
lw $ra, 2($sp)
addi $sp, $sp, 8
jr $ra

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
Roll number: 11
Roll number: 22
Roll number: 33
Reverse: 33 22 11
-- program is finished running --

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
Score: 45
Score: 78
Score: 62
Highest = 78
-- program is finished running --

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
Grades: A C C F B
Year still 2026
-- program is finished running --

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.

← All CS-704 material