Nexus Learn

Practical 8: Nested Decisions and Compound Conditions

Lab Practical CS-704 Advanced Computer Architecture 8 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 an AND Condition
  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 - 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
C: if (age >= 18 && age <= 60) eligible = 1;

MIPS: blt $t0, 18, done # first test false -> stop (short-circuit)
bgt $t0, 60, done # second test false -> stop
li $t5, 1 # both true
done:

3.2 OR: branch to the true part as soon as one test passes

OR pattern
C: if (marks < 0 || marks > 100) invalid();

MIPS: bltz $t0, invalid # first test true -> go (short-circuit)
bgt $t0, 100, invalid # second test true -> go
...valid part...
j show
invalid: ...

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
# Practical 08 - Part A: trace an AND condition (18 <= age <= 60)
.text
main:
li $t0, 25 # age
li $t5, 0 # eligible flag
slti $t1, $t0, 18 # age < 18 ?
bne $t1, $zero, done # branch 1: too young
slti $t2, $t0, 61 # age <= 60 ?
beq $t2, $zero, done # branch 2: too old
li $t5, 1 # 18 <= age <= 60
done:
li $v0, 10
syscall

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
bad: .asciiz "Invalid"
good: .asciiz "Valid"
.text
main:
li $t0, 150 # marks
bltz $t0, invalid # marks < 0 ?
bgt $t0, 100, invalid # marks > 100 ?
la $a0, good
j show
invalid:
la $a0, bad
show:
li $v0, 4
syscall
li $v0, 10
syscall

My prediction (write BEFORE running):

Actual output in MARS / was I right? Why?

B2. A condition without branches

.text
main:
li $t0, 35 # temperature
slti $t1, $t0, 40 # t < 40
slti $t2, $t0, 30 # t < 30
xori $t2, $t2, 1 # t >= 30
and $a0, $t1, $t2 # 30 <= t < 40 ?
li $v0, 1
syscall
li $v0, 10
syscall

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
# Should print grade A for 85 (A >= 80, B >= 65, C >= 50, else F)
.data
ga: .asciiz "A"
gb: .asciiz "B"
gc: .asciiz "C"
gf: .asciiz "F"
.text
main:
li $t0, 85 # marks
bge $t0, 50, isC
bge $t0, 65, isB
bge $t0, 80, isA
la $a0, gf
j show
isA:
la $a0, ga
j show
isB:
la $a0, gb
j show
isC:
la $a0, gc
show:
li $v0, 4
syscall
li $v0, 10
syscall

What MARS shows
C
-- program is finished running --

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

C2. Range check

P08_C2_bug.asm
# Should print "In range" only if 18 <= age <= 60
.data
yes: .asciiz "In range"
no: .asciiz "Out of range"
.text
main:
li $t0, 70 # age
bge $t0, 18, inside
bgt $t0, 60, outside
inside:
la $a0, yes
j show
outside:
la $a0, no
show:
li $v0, 4
syscall
li $v0, 10
syscall

What MARS shows
In range
-- program is finished running --

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

C3. Minor or not

P08_C3_bug.asm
# Should print 1 if age is below 18 (a minor)
.text
main:
li $t0, 15 # age
li $a0, 0
blt $t0, 18
j show
minor:
li $a0, 1
show:
li $v0, 1
syscall
li $v0, 10
syscall

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
Enter marks: 75
Valid marks
-- 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]: 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
Enter marks (0-100): 72
Grade: B
-- 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]: 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
Runs of batsman 1: 45
Runs of batsman 2: 78
Runs of batsman 3: 30
Highest score: 78 by batsman 2
-- 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. 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.

← All CS-704 material