Item | Detail |
|---|---|
Course learning outcomes | CLO6 - low-level software in MIPS assembly (P3); CLO3 - instruction set principles: control flow (C2). |
Tool | MARS 4.5: Edit, Execute (Run > Step), Registers window, Tools > Instruction Counter |
Linked lectures | Practical 7 (beq, bne, slt, if-else); Lecture 9 (control flow); Lecture 6 (common case). |
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:
- Translate AND (&&) and OR (||) conditions into a chain of branches, using short-circuit evaluation.
- Build an else-if chain (grade calculator) and a nested decision (largest of three) in MIPS.
- Compute a condition without branches using slt, and, or and xori.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Compound condition | Two or more conditions joined by AND or OR | Bus concession: student AND under 25 |
Short-circuit | Stop testing as soon as the answer is known | If the shop is closed, you don't ask the price |
Else-if chain | Test bands one after another; the first true one wins | Electricity slabs or grade bands |
Nested decision | An if inside another if | Is the shop open? If yes, is the item in stock? |
Range check | Test that a value lies between a low and a high limit | Age must be 18-60 for a CNIC-based loan form |
Branchless | Computing a yes/no answer with logic instead of jumps | A checklist where every box is ticked anyway |
Boundary value | A value exactly on a limit (50, 65, 80) | Exactly 50 marks - pass or fail? |
3. Background
3.1 AND: branch to the false part as soon as one test fails
AND pattern |
3.2 OR: branch to the true part as soon as one test passes
OR pattern |
3.3 Else-if chain
Test the bands from the highest down, and end every part with j. The first condition that is true wins, so the ORDER of the tests matters.
Marks | Grade |
|---|---|
80-100 | A |
65-79 | B |
50-64 | C |
0-49 | F |
below 0 or above 100 | Invalid marks |
3.4 Without branches
slt gives 1 or 0, so conditions can be combined with and / or. 30 <= t < 40 is (t < 40) AND NOT (t < 30): slti, slti, xori ... 1, and. No branch means no jump - useful later when we study pipelines (Unit 3), where branches are expensive.
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-6 | Background 3.1-3.4 | - |
6-12 | Part A: trace an AND condition | - |
12-16 | Part B: predict the output | - |
16-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 an AND Condition
Type the program, save it as P08_A1_trace.asm, and Step (F7) through it. Then change the age to 15 and to 70 and run it again each time, with Tools > Instruction Counter connected.
P08_A1_trace.asm |
Complete the table for the three ages.
Age | Branch 1 taken? | Branch 2 taken? | $t5 | Instructions executed |
|---|---|---|---|---|
25 | ||||
15 | ||||
70 |
A1. Why does age 15 execute the fewest instructions?
A2. For age 25, $t1 = 0 and $t2 = 1. Explain both values.
A3. Why does the program use slti ..., 61 for 'age <= 60'?
Part B - Predict the Output
Predict first, then run.
B1. OR with short-circuit
.data |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. A condition without branches
.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. Grade bands in the wrong order
P08_C1_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Range check
P08_C2_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Minor or not
P08_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 (P08_T1.asm, P08_T2.asm, P08_T3.asm). Test every boundary value given.
Task 1 [Easy · 3 marks]: Valid marks (AND)
Read marks and print 'Valid marks' if 0 <= marks <= 100, otherwise 'Invalid marks'. Test with 75, 0, 100, 101 and -1.
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]: Grade calculator (else-if chain)
Extend Task 1. For valid marks print 'Grade: ' and the grade from the table in 3.3; for invalid marks print 'Invalid marks'. Test the boundary values 80, 79, 65, 50, 49, 100 and 120.
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]: Highest score of three batsmen (nested decisions)
Read the runs of three batsmen and print the highest score and which batsman made it. If two scores are equal and highest, report the batsman who batted first. Use a nested decision: compare batsman 1 with 2, then the winner with 3.
Test: 45, 78, 30 → batsman 2; 90, 12, 90 → batsman 1; 10, 40, 40 → batsman 2; 5, 5, 8 → batsman 3. |
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. Explain short-circuit evaluation for AND and for OR. Which branch target do you use in each case?
E2. In an else-if chain, why must the grade bands be tested from the highest down when you use bge?
E3. Write a branchless MIPS sequence that sets $t9 = 1 if $t0 < 0 OR $t0 > 100, else 0.
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Both limits, correct for all five inputs | One limit missing (1-2) | Does not assemble |
T2 | 3 | Validity + bands in the right order, all boundaries right | Bands right but a boundary wrong (2) | No attempt |
T3 | 4 | Correct for all four tests including ties | Correct score but wrong batsman on ties (3) | No attempt |
Submission Rules
- Save files as P08_T1.asm, P08_T2.asm and P08_T3.asm in a folder named RollNo_P08.
- Record the Part A table and the Task 2 boundary results 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.