Nexus Learn

Practical 13: Procedures: jal, jr and Arguments

Lab Practical CS-704 Advanced Computer Architecture 9 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: Trace Two Calls
  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 call and return (C2).

Tool

MARS 4.5: Edit, Execute (Text Segment, Registers window: $ra, $a0-$a3, $v0-$v1, pc), Run > Step, Tools > Instruction Counter

Linked lectures

Lecture 9 (procedure call and return: jal, jr, $ra); Lecture 11 (register allocation); Practicals 11-12 (encoding jal and jr).

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:

  • Call a procedure with jal and return with jr $ra, and trace $ra and the PC in MARS.
  • Pass arguments in $a0-$a3 and return results in $v0-$v1, following the MIPS calling convention.
  • Write reusable leaf procedures and call them from several places in a program.

2. Word Bank

Word

Simple meaning

Real-world example

Procedure (function)

A named block of code that does one job and returns

A tailor: you give cloth and size, you get a shirt back

Call

Jump to the procedure and remember where to come back

Dialling a helpline and staying on the line

jal (jump and link)

Jump to a label AND put PC + 4 in $ra

Leaving your phone number before you go

jr $ra (jump register)

Jump to the address held in $ra: return

Calling back the number you were given

Return address

The address of the instruction after the jal

The page number you mark before leaving a book

Argument

A value given to the procedure ($a0-$a3)

The cloth and measurements given to the tailor

Return value

The result handed back ($v0-$v1)

The finished shirt

Leaf procedure

A procedure that calls no other procedure

A tailor who does all the work himself

Calling convention

The agreed rules for which register does what

Traffic rules: everyone drives on the left

3. Background

3.1 Call and return

jal label does two things in one instruction: $ra = PC + 4 (the address of the next instruction) and PC = label. At the end of the procedure, jr $ra jumps back to that address. Because the return address is saved in a register, the SAME procedure can be called from many places and always returns to the right one.

Pattern
li $a0, 30 # argument 1
li $a1, 60 # argument 2
jal area # $ra = address of the next line; go to area
move $s0, $v0 # comes back here: result in $v0
...
area:
mul $v0, $a0, $a1 # work
jr $ra # return

3.2 The MIPS calling convention (register use)

Registers

Number

Use

Must the procedure keep it unchanged?

$a0-$a3

4-7

first four arguments

no

$v0-$v1

2-3

return values (also syscall code in $v0)

no

$t0-$t9

8-15, 24-25

temporaries (scratch)

no - the caller must not rely on them

$s0-$s7

16-23

saved values that must live across a call

yes (Practical 14 shows how)

$ra

31

return address

yes, if the procedure calls another one (P14)

$sp

29

stack pointer

yes (P14)

Today all procedures are leaf procedures: they call no other procedure, so $ra is not overwritten and no stack is needed. Syscalls are not procedures: they do not change $ra.

4. Time Plan (50 minutes)

Time

Activity

Marks

0-5

Background 3.1-3.2

-

5-12

Part A: trace two calls 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: Trace Two Calls

A property dealer works out the area of two plots (30 ft x 60 ft and 25 ft x 50 ft) with one procedure. Type the program, save it as P13_A1_trace.asm, assemble it and use Run > Step (F7). Each time a jal or jr runs, write down where the PC goes and the values of $ra and $v0 (Registers window).

P13_A1_trace.asm
# Practical 13 - Part A: trace jal and jr (area of two plots)
.text
main:
li $a0, 30 # plot 1: length 30 ft
li $a1, 60 # width 60 ft
jal area # call 1
move $s0, $v0 # keep area 1
li $a0, 25 # plot 2: length 25 ft
li $a1, 50 # width 50 ft
jal area # call 2
move $s1, $v0 # keep area 2
add $a0, $s0, $s1 # total area
li $v0, 1
syscall
li $v0, 10
syscall

area: # area(a0 = length, a1 = width) -> v0
mul $v0, $a0, $a1
jr $ra

Fill the table while stepping.

Instruction just executed

PC goes to

$ra after it

$v0 after it

jal area (call 1, at 0x00400008)

jr $ra (end of call 1)

jal area (call 2, at 0x00400018)

jr $ra (end of call 2)

A1. Both jal instructions have the same machine code (Code column). Why is $ra different after the two calls?

A2. Run the whole program. What does it print, and how many instructions does the Instruction Counter show? Which instructions ran twice?

A3. area receives its inputs in $a0 and $a1 and gives its answer in $v0. Why not choose any registers we like?

Part B - Predict the Output

Predict first, then run.

B1. The caller trusts $t0

.text
main:
li $t0, 100 # the caller keeps 100 in $t0
li $a0, 7
jal double
add $a0, $t0, $v0 # caller expects 100 + 14
li $v0, 1
syscall
li $v0, 10
syscall

double: # double(a0) -> v0
add $t0, $a0, $a0 # uses $t0 as scratch
move $v0, $t0
jr $ra

My prediction (write BEFORE running):

Actual output in MARS / was I right? Why?

B2. Two answers from one call

.data
dot: .asciiz "."
.text
main:
li $a0, 47 # balls bowled
jal overs
move $t0, $v1 # balls left over
move $a0, $v0 # complete overs
li $v0, 1
syscall
li $v0, 4
la $a0, dot
syscall
li $v0, 1
move $a0, $t0
syscall
li $v0, 10
syscall

overs: # overs(a0 = balls) -> v0 = overs, v1 = balls
li $t1, 6
div $a0, $t1
mflo $v0 # quotient
mfhi $v1 # remainder
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. Bus fare

P13_C1_bug.asm
# Bus fare: Rs 20 per stop. Print the fare for 6 stops.
.text
main:
li $a0, 6
jal fare
move $a0, $v0
li $v0, 1
syscall
li $v0, 10
syscall

fare: # fare(a0 = stops) -> v0
mul $a0, $a0, 20
jr $ra

What MARS shows
Run I/O: 0

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

C2. Two lines of dashes

P13_C2_bug.asm
# Print a line of dashes two times
.data
line: .asciiz "----------\n"
.text
main:
jal dashes
jal dashes

dashes: # prints one line
li $v0, 4
la $a0, line
syscall
jr $ra

What MARS shows
----------
----------
----------
----------
----------
----------
----------
... (never stops - press Stop)

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

C3. Square of 12

P13_C3_bug.asm
# Square of a number: 12 x 12
.text
main:
li $a0, 12
jal Square
move $a0, $v0
li $v0, 1
syscall
li $v0, 10
syscall

square: # square(a0) -> v0
mul $v0, $a0, $a0
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 (P13_T1.asm, P13_T2.asm, P13_T3.asm). Every procedure must take its inputs in $a registers, return results in $v registers and end with jr $ra.

Task 1 [Easy · 3 marks]: A reusable print procedure (shopping list)

Write a procedure print_rs that prints 'Rs ', the amount in $a0, and a new line. main calls it four times: milk Rs 220, bread Rs 150, eggs Rs 390 and the total Rs 760. Careful: syscall 4 needs the string address in $a0 - keep the amount somewhere else first.

After the run, write down the instruction count (Tools > Instruction Counter).

Sample run:

MARS Run I/O
Rs 220
Rs 150
Rs 390
Rs 760
-- 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]: Rickshaw fare: a procedure with a decision

A rickshaw app charges Rs 150 for the first 2 km and Rs 40 for every further km. Write fare(a0 = km) → v0 = rupees. main reads the distance, calls fare and prints 'Fare = Rs ...'. Test with 1, 2, 3, 7 and 12 km.

Sample run:

MARS Run I/O
Distance (km): 7
Fare = Rs 350
-- 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]: One procedure, two answers, two arrays

Write stats(a0 = address of an array, a1 = count) that returns the total in $v0 and the highest mark in $v1. Call it for section A (5 students) and section B (4 students), keep the four answers in $s0-$s3, then print both lines with a second procedure show(a0 = label, a1 = total, a2 = highest).

After the run: what is in $ra, and why?

Sample run:

MARS Run I/O
Section A: total 382, highest 91
Section B: total 292, highest 88
-- 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. Why can a procedure not simply end with 'j main' or a jump to a fixed label?

E2. With the procedure, Task 1 executes 62 instructions; a version without a procedure (the print code copied four times) executes 46 but is 38 lines long instead of 21. Explain the difference.

E3. Which registers may a procedure change freely, and which must it give back unchanged?

Marking Rubric

Task

Marks

Full marks

Partial marks

Zero

T1

3

One procedure, $a0 saved, 4 correct lines

Works but inline / no jr (1-2)

Does not assemble

T2

3

All five distances correct

Boundary 2-3 km wrong (2)

No attempt

T3

4

Both arrays, $v0/$v1, show, $ra explained

One array or no $ra (2)

No attempt

Submission Rules

  • Save files as P13_T1.asm, P13_T2.asm and P13_T3.asm in a folder named RollNo_P13.
  • Copy the Part A table, the Task 1 instruction count and the Task 3 $ra value 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