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 |
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 |
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 |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. Two answers from one call
.data |
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 |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Two lines of dashes
P13_C2_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Square of 12
P13_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 (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 |
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 |
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 |
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.