Item | Detail |
|---|---|
Course learning outcomes | CLO6 - low-level software in MIPS assembly (P3); CLO3 - instruction set principles: control flow and PC-relative addressing (C2). |
Tool | MARS 4.5: Edit, Execute (Text Segment Basic column), Registers window, Tools > Instruction Counter |
Linked lectures | Practical 3 (masks: andi), Practical 4 (instruction count); Lecture 8 (PC-relative addressing); Lecture 9 (control flow). |
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:
- Use beq, bne, slt, slti and sltu to compare values and change the flow of a program.
- Translate if and if-else statements into MIPS using an inverted condition and a jump (j).
- Explain what the pseudo-branches blt, bge, bgt and ble become in real MIPS instructions.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Branch | An instruction that may jump to another place in the program | A signboard: 'if the road is closed, take the bypass' |
Label | A name for an address in the program | A shop name that you can be sent to |
Taken / not taken | The branch jumps / the program simply continues | You turn onto the bypass / you drive straight on |
slt (set on less than) | Puts 1 in a register if a < b, else 0 | A yes/no stamp on a form |
Inverted condition | Branch on the OPPOSITE of the C condition to skip the then-part | 'If NOT raining, skip the umbrella step' |
Fall through | Running into the next lines because no jump stops you | Missing your stop and riding to the next one |
Pseudo-branch | A branch the assembler builds from slt + beq/bne | A short form that the clerk expands |
3. Background
3.1 The real comparison instructions
Instruction | Meaning | Example |
|---|---|---|
beq $t0, $t1, L | if $t0 == $t1 go to L | equal readings |
bne $t0, $t1, L | if $t0 != $t1 go to L | different readings |
slt $t2, $t0, $t1 | $t2 = 1 if $t0 < $t1 (signed), else 0 | is A's bill smaller? |
slti $t2, $t0, 100 | $t2 = 1 if $t0 < 100, else 0 | is usage below 100 units? |
sltu $t2, $t0, $t1 | like slt but treats both as unsigned | addresses, sizes |
j L | always go to L | skip the else part |
MIPS has only two real conditional branches, beq and bne. 'Less than' is first computed with slt into a register, then tested with beq/bne against $zero. Branches use PC-relative addressing (Lecture 8): the target is (PC + 4) + offset x 4.
3.2 if and if-else in assembly
Pattern |
Remember: branch on the OPPOSITE of the C condition, and end the then-part with j - otherwise the program falls through into the else part. |
3.3 Pseudo-branches (real MARS Basic column)
You write | MARS assembles (Basic column) |
|---|---|
blt $t0, $t1, L | slt $at, $t0, $t1; bne $at, $zero, L |
bge $t0, $t1, L | slt $at, $t0, $t1; beq $at, $zero, L |
bgt $t0, $t1, L | slt $at, $t1, $t0; bne $at, $zero, L |
ble $t0, $t1, L | slt $at, $t1, $t0; beq $at, $zero, L |
bge $t0, 50, L | slti $at, $t0, 50; beq $at, $zero, L |
bgt $t0, 100, L | addi $at, $zero, 100; slt $at, $at, $t0; bne $at, $zero, L |
Each pseudo-branch costs 2 or 3 real instructions and uses $at (the assembler temporary). This is the load-store/RISC idea from Lecture 7: a few simple instructions instead of one complex one.
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-6 | Background 3.1-3.3 | - |
6-13 | Part A: trace the branches | - |
13-17 | Part B: predict the output | - |
17-22 | Part C: bug hunt | - |
22-45 | Part D: graded tasks 1-3 | 10 |
45-50 | Part E: post-lab questions; save; submit | - |
Part A - Warm-up: Trace Comparisons and Branches
Type the program, save it as P07_A1_trace.asm, assemble it and run it with Run > Step (F7), watching the green highlight. Fill in the table, then check against the Registers window.
P07_A1_trace.asm |
Fill in each register value and say whether each branch was taken.
Item | Value / taken? | Reason |
|---|---|---|
$t2 | ||
$t3 | ||
$t4 | ||
branch 1 (beq) | ||
branch 2 (bne) | ||
$t5, $t6, $t7 |
A1. The program has 15 instructions in the Text Segment. How many were executed? (Tools > Instruction Counter, or count the steps.)
A2. Change house B to 150 units (li $t1, 150) and predict $t5 and $t6 before running.
A3. Why does MIPS need slt? Why not a 'branch if less than' instruction?
Part B - Predict the Output
Predict first, then run. For B2, also look at the Basic column of the Text Segment.
B1. Signed or unsigned?
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. A pseudo-branch
.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. Even and odd together
P07_C1_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Pass mark 50
P07_C2_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Equal readings
P07_C3_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
Part D - Graded Tasks (10 marks)
Write each program in its own file (P07_T1.asm, P07_T2.asm, P07_T3.asm). Test each with the extra inputs given in the task.
Task 1 [Easy · 3 marks]: Even or odd
Read an integer. Print it, followed by ' is even' or ' is odd'. Use andi to get the lowest bit (Practical 3) and one beq or bne. Test with 14, 7, 0 and -7.
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]: Electricity bill with two slabs
Read the units used. The bill is Rs 10 per unit for units 1-100 and Rs 15 per unit for each unit above 100. Print the bill. Test with 80, 100, 101 and 250.
Hint: for more than 100 units: bill = 1000 + (units - 100) x 15. Use slti to test units <= 100 (that is, units < 101). |
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]: Larger of two - real instructions only
Read two integers and print the larger one. You may use ONLY slt and beq/bne for the decision (no blt, bge, bgt, ble). Then use Tools > Instruction Counter (connect it, then run) to record the number of instructions executed for the inputs (7, 3) and (3, 7). In your lab book, explain why the counts differ.
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. Write MIPS for: if (a > b) c = a - b; else c = b - a; (a, b, c in $s0, $s1, $s2) using only real instructions.
E2. Why does every pseudo-branch use $at? Why must your own programs not use $at?
E3. A beq is at address 0x00400010 and its offset field is 3. Where does it jump if taken? (Lecture 8)
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Lowest-bit test, no fall-through, correct for all four inputs | Works for positive numbers only (2) | Does not assemble |
T2 | 3 | Correct slab boundary and both formulas | One slab correct (1-2) | No attempt |
T3 | 4 | slt + beq/bne only, correct output, counts and reason | Uses a pseudo-branch, or no explanation (2-3) | No attempt |
Submission Rules
- Save files as P07_T1.asm, P07_T2.asm and P07_T3.asm in a folder named RollNo_P07.
- Record the Task 3 instruction counts and your explanation in 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 code. Copied code gets zero for both students.