Nexus Learn

Practical 7: Decisions: Branches and Comparisons

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 Comparisons and Branches
  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 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
C: if (n % 2 == 0) print("even"); else print("odd");

MIPS: andi $t1, $t0, 1 # test the condition
bne $t1, $zero, else # INVERTED: branch away if NOT even
...then part...
j endif # jump over the else part
else: ...else part...
endif:

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
# Practical 07 - Part A: trace comparisons and branches
.text
main:
li $t0, 120 # units used by house A
li $t1, 95 # units used by house B
slt $t2, $t0, $t1 # is A < B ?
slt $t3, $t1, $t0 # is B < A ?
slti $t4, $t1, 100 # is B < 100 ?
li $t5, 0
li $t6, 0
li $t7, 0
beq $t2, $zero, skip1 # branch 1
li $t5, 1 # runs only if A < B
skip1:
bne $t4, $zero, small # branch 2
li $t6, 1 # runs only if B >= 100
small:
li $t7, 1
li $v0, 10
syscall

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
main:
li $t0, -1
li $t1, 1
slt $a0, $t0, $t1 # signed compare
li $v0, 1
syscall
li $v0, 11
li $a0, 32 # space
syscall
sltu $a0, $t0, $t1 # unsigned compare
li $v0, 1
syscall
li $v0, 10
syscall

My prediction (write BEFORE running):

Actual output in MARS / was I right? Why?

B2. A pseudo-branch

.data
pass: .asciiz "PASS"
fail: .asciiz "FAIL"
.text
main:
li $t0, 50 # marks
bge $t0, 50, ok
la $a0, fail
j show
ok:
la $a0, pass
show:
li $v0, 4
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. Even and odd together

P07_C1_bug.asm
# Should print only "Even" or only "Odd"
.data
ev: .asciiz "Even\n"
od: .asciiz "Odd\n"
.text
main:
li $t0, 14
andi $t1, $t0, 1 # lowest bit: 0 = even
bne $t1, $zero, odd
li $v0, 4
la $a0, ev
syscall
odd:
li $v0, 4
la $a0, od
syscall
li $v0, 10
syscall

What MARS shows
Even
Odd

-- program is finished running --

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

C2. Pass mark 50

P07_C2_bug.asm
# Pass if marks >= 50. Should print PASS for 50
.data
pass: .asciiz "PASS"
fail: .asciiz "FAIL"
.text
main:
li $t0, 50 # marks
ble $t0, 50, low # marks too low?
la $a0, pass
j show
low:
la $a0, fail
show:
li $v0, 4
syscall
li $v0, 10
syscall

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

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

C3. Equal readings

P07_C3_bug.asm
# Should print 1 when the two readings are equal
.text
main:
li $t0, 7
li $t1, 7
li $a0, 0
bne $t0, $t1, Done
li $a0, 1
done:
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 (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
Enter an integer: 14
14 is even
-- 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]: 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
Units used: 250
Bill = Rs 3250
-- 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]: 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
First number: 7
Second number: 3
Larger = 7
-- program is finished running --

Instructions executed (MARS): (7, 3) = 26 (3, 7) = 27

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.

← All CS-704 material