Item | Detail |
|---|---|
Course learning outcomes | CLO6 - Design and analyze low-level software using MIPS assembly (P3). Supports CLO3 (operations in the instruction set). |
Tool | MARS 4.5 MIPS simulator (Settings > Values displayed in hexadecimal: ON for this practical) |
Linked lectures | Lecture 9 preview (operations in the instruction set); follows Practicals 1-2; Practical 4 continues this week. |
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 and, or, xor, nor, andi, ori and xori and predict their results bit by bit.
- Build masks to test, set, clear and toggle individual bits.
- Explain why andi, ori and xori fill the upper 16 bits of their constant with zeros.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Bit | A single binary digit, 0 or 1 | A light switch: off or on |
Bitwise | Done on each bit position separately | Checking each seat in a row one by one |
Mask | A pattern of bits used to pick, set or clear chosen bits | A stencil that lets paint through only some holes |
Set / clear / toggle | Make a bit 1 / make it 0 / flip it | Switch a fan on / off / to the other state |
Flag | A bit that records yes/no information | A tick on an attendance sheet |
Zero-extend | Fill the upper bits with 0s when widening a number | Writing 25 as 0025 on a form |
3. Background
3.1 Logical instructions
Instruction | Rule for each bit | Typical use |
|---|---|---|
and rd, rs, rt / andi rt, rs, imm | 1 only if BOTH bits are 1 | Clear bits, test a bit (mask with 1s where you look) |
or rd, rs, rt / ori rt, rs, imm | 1 if EITHER bit is 1 | Set bits |
xor rd, rs, rt / xori rt, rs, imm | 1 if the bits are DIFFERENT | Toggle bits, compare for equality |
nor rd, rs, rt | NOT (rs OR rt) | NOT: nor rd, rs, $zero |
3.2 The three mask jobs
Job | Instruction | Example on 0101 1010 (0x5A) |
|---|---|---|
Test bit 0 (odd/even) | andi rt, rs, 1 | 0x5A AND 0x01 = 0 -> even |
Set bit 0 | ori rt, rs, 1 | 0x5A OR 0x01 = 0x5B |
Clear bit 3 (value 8) | andi rt, rs, 0xFFF7 | 0x5A AND 0xFFF7 = 0x52 |
Toggle bit 0 | xori rt, rs, 1 | 0x5A XOR 0x01 = 0x5B |
Important: andi, ori and xori ZERO-EXTEND their 16-bit constant: the upper 16 bits of the mask are 0. So andi with 0xFF keeps only the lowest 8 bits, even of a negative number. |
Real-world example: Your phone stores app permissions (camera, microphone, location, contacts) as bits of one number. Allowing or removing a permission is just OR or AND with a mask. |
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-6 | Background 3.1-3.2 (guided demo, hex display ON) | - |
6-14 | Part A: trace with Step (F7) | - |
14-18 | Part B: predict, then run | - |
18-24 | Part C: bug hunt | - |
24-45 | Part D: graded tasks 1-3 | 10 |
45-50 | Part E: post-lab questions; save; submit | - |
Part A - Warm-up: Trace the Logical Instructions
Type the program, save it as P03_A1_trace.asm, assemble and Step (F7) through it. Write each result in hex (from the Registers pane), then convert the lowest 8 bits to binary.
P03_A1_trace.asm |
Fill in the table. $t0 = 0x5A = 0101 1010 and $t1 = 0x0F = 0000 1111.
Instruction | Register | Hex value | Lowest 8 bits | Decimal |
|---|---|---|---|---|
and $t2, $t0, $t1 | ||||
or $t3, $t0, $t1 | ||||
xor $t4, $t0, $t1 | ||||
nor $t5, $t0, $zero | ||||
andi $t6, $t0, 0xF0 | ||||
ori $t7, $t0, 0x01 |
A1. Why is $t5 negative in decimal?
Part B - Predict the Output
Write your prediction first, then run in MARS and compare.
B1. Odd or even?
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. Zero-extension and NOT
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
Part C - Bug Hunt
One assemble error and one logic error. Run each, read what MARS shows, and fix it.
C1. Keep the lowest 4 bits (assemble error)
P03_C1_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Clear bit 3 (logic error)
P03_C2_bug.asm |
What MARS shows |
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 (P03_T1.asm, P03_T2.asm, P03_T3.asm) with comments. Use logical instructions only for the bit work (no branches - they come in Practical 7).
Task 1 [Easy · 3 marks]: Odd or even with a mask
Read a number and print its lowest bit with a label (1 = odd, 0 = even). Use andi.
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]: App permission flags
An app stores permissions as bits: camera = 1, microphone = 2, location = 4, contacts = 8. Read the current flags, then (1) allow location, (2) remove microphone, (3) switch camera to the other state. Print the flags after each step.
Hint: Set = ori with the bit. Clear = andi with a mask that has 0 only in that bit. Toggle = xori with the bit. |
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]: XOR tricks: compare and swap
Read a and b. Print a XOR b (it is 0 only when a = b). Then swap a and b using three xor instructions and no other register, and print them.
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 does 'andi $a0, $t0, 0xFF' give 255 when $t0 = -1, but 'and $a0, $t0, $t1' with $t1 = -1 gives -1?
E2. Write one instruction each to set, clear and toggle bit 4 (value 16) of $t0.
E3. How can you write NOT $t1 (flip every bit) in MIPS, which has no NOT instruction?
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Correct lowest bit for positive, zero and negative inputs | Wrong mask or format (1-2) | Does not assemble |
T2 | 3 | Correct set, clear and toggle with output after each | One operation wrong (2) | Uses add/sub instead of logic |
T3 | 4 | XOR printed; correct three-xor swap | Swap uses a temporary (2) | No swap |
Submission Rules
- Save files as P03_T1.asm, P03_T2.asm and P03_T3.asm in a folder named RollNo_P03.
- Record code and Run I/O output 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.