Item | Detail |
|---|---|
Course learning outcomes | CLO6 - Design and analyze low-level software using MIPS assembly (P3). Supports CLO2 (instruction count and execution time). |
Tool | MARS 4.5 MIPS simulator; Tools > Instruction Counter |
Linked lectures | Lecture 1 (ISA level), Lecture 2 (clock rate); prepares for Lecture 4 (CPU performance equation). Follows Practical 1. |
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:
- Read the Basic and Code columns of the Text Segment and explain what pseudo-instructions become.
- Print characters and newlines with syscall 11 and combine output services.
- Find and fix assemble errors, runtime errors and logic errors from real MARS messages.
- Measure the number of instructions executed and convert it into execution time.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Pseudo-instruction | A convenient instruction the assembler changes into one or more real ones | 'Book a cab' in an app becomes several real steps |
Basic instruction | A real instruction of the MIPS ISA that the hardware executes | The actual steps the driver takes |
Machine code | The 32-bit binary (shown in hex) form of an instruction | The barcode on a product |
Immediate | A constant written inside the instruction (16 bits in MIPS) | A price printed on the packet itself |
Runtime error | An error that stops the program while it is running | A car that starts but breaks down on the road |
Logic error | The program runs but gives a wrong answer, with no message | A calculator that adds when you pressed minus |
Instruction count (IC) | Number of instructions executed by a program run | Number of steps walked from home to class |
3. Background
3.1 Pseudo-instructions and the Basic column
After you assemble, the Execute tab shows the Text Segment with columns Address, Code, Basic and Source. Source is what you typed. Basic is the real MIPS instruction(s) the assembler produced. Code is its 32-bit machine code in hexadecimal. A pseudo-instruction such as li, move or la is not in the hardware; the assembler replaces it with one or more basic instructions and may use the register $at ($1) to do so.
You write | MARS produces (Basic column) | Why |
|---|---|---|
li $t0, 25 | addiu $t0, $zero, 25 | small constant fits in 16 bits |
li $t1, 100000 | lui $at, 0x1 ; ori $t1, $at, 0x86a0 | 100000 needs more than 16 bits |
move $s0, $t0 | addu $s0, $zero, $t0 | add with $zero copies a register |
la $a0, msg | lui $at, 0x1001 ; ori $a0, $at, 0x0 | a 32-bit address is built in two halves |
3.2 More system calls
Service | $v0 | Input | Result |
|---|---|---|---|
print character | 11 | $a0 = character code (ASCII) | prints one character, e.g., 10 = newline, 65 = 'A' |
print integer / string / read integer / exit | 1 / 4 / 5 / 10 | as in Practical 1 |
3.3 Counting instructions
Open Tools > Instruction Counter and click 'Connect to MIPS' BEFORE you press Go. When the program ends, the tool shows how many instructions were executed. Execution time = instruction count x CPI x clock cycle time. In this practical assume CPI = 1 and a 2 GHz clock (clock cycle = 1 / 2 GHz = 0.5 ns).
Link to Lectures 1 and 2: MARS works at the ISA level: the instruction count is real, but the time depends on the CPI and clock rate of a real chip (its organization and hardware). |
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-6 | Background 3.1-3.3 (guided demo) | - |
6-14 | Part A: pseudo-instructions in the Text Segment | - |
14-19 | Part B: predict, then run | - |
19-27 | Part C: bug hunt | - |
27-45 | Part D: graded tasks 1-3 | 10 |
45-50 | Part E: post-lab questions; save files; submit | - |
Part A - Warm-up: What Do Pseudo-instructions Become?
Type the program below, save it as P02_A1_pseudo.asm and press Assemble (F3). Look at the Text Segment in the Execute tab. Then connect the Instruction Counter and press Go.
P02_A1_pseudo.asm |
Fill in the Basic instruction(s) and machine code MARS shows for each line of source code.
Source line | Basic instruction(s) | Machine code |
|---|---|---|
li $t0, 25 | ||
li $t1, 100000 | ||
move $s0, $t0 | ||
la $a0, msg | ||
li $v0, 4 |
A1. The program has 8 lines of code in .text. How many basic instructions are in the Text Segment, and how many did the Instruction Counter report?
A2. Check that 'lui $at, 0x1' followed by 'ori $t1, $at, 0x86a0' really puts 100000 in $t1.
A3. If a 2 GHz processor runs this program with CPI = 1, how long does it take?
Part B - Predict the Output
Write your prediction first, then run in MARS and compare.
B1. Strings, integers and a newline
.data |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. Same register, two services
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
Part C - Bug Hunt
Each program has one bug: an assemble error (MARS will not run it), a runtime error (it stops while running) or a logic error (no message, wrong answer). Run each one, read the MARS message, then find the line and fix it.
C1. Printing a message (assemble error)
P02_C1_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Sum of two numbers (logic error)
P02_C2_bug.asm |
What MARS shows |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C3. Printing 85 (runtime error)
P02_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 (P02_T1.asm, P02_T2.asm, P02_T3.asm) with comments. Output must match the sample run. Values typed by the user are shown after the prompts.
Task 1 [Easy · 3 marks]: Rickshaw app fare
A rickshaw app charges Rs 40 base fare plus Rs 25 for each kilometre. Read the distance in km and print the fare. Use mul rd, rs, rt for the multiplication (rd = rs x rt; details in Practical 20).
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]: Expression with add only
Read x and y and print z = 3x + 2y - 7 using only add, sub and addi (no mul). Hint: 3x = x + x + x.
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]: Count and compare
Make a copy of Task 2 that uses mul for 3x and 2y. Run BOTH versions with x = 5 and y = 8 using the Instruction Counter. In your lab book write: (a) the instruction count of each version, (b) the execution time of each on a 2 GHz processor with CPI = 1, (c) which version has fewer instructions, and whether that means it is always faster.
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. What is a pseudo-instruction? Give two examples from today and what MARS turned them into.
E2. Why did 'li $a0, msg' fail, and why does la need two basic instructions?
E3. Which level of design does MARS simulate - ISA, organization or hardware? What does this mean for the times you calculated?
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Correct fare for all km values incl. 0 | Formula partly wrong (1-2) | Does not assemble |
T2 | 3 | Correct z using add/sub/addi only, exact format | Correct z but format errors (2) | Uses mul or wrong result |
T3 | 4 | mul version works; both counts, times and a sensible conclusion | Counts or times missing (2-3) | No comparison |
Submission Rules
- Save files as P02_T1.asm, P02_T2.asm and P02_T3.asm in a folder named RollNo_P02.
- Write the counts, times and conclusion of Task 3 in the lab book (Measurements box).
- 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.