Item | Detail |
|---|---|
Course learning outcomes | CLO6 - Design and analyze low-level software using MIPS assembly (P3). Supports CLO1 (the ISA is what the programmer sees). |
Tool | MARS 4.5 MIPS simulator (Mars4_5.jar) on Java 8 or later |
Linked lectures | Lecture 1 (ISA, organization and hardware). Continued in Practical 2. |
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 MARS to edit, assemble, run and step through a MIPS program.
- Identify the 32 MIPS registers by number, name and use, and watch their values change.
- Write programs that print text and integers and read integers using system calls (syscall).
- Fix a simple assemble error and a simple logic error.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Assembly language | Human-readable form of machine instructions | Writing 'add 2 and 3' instead of the binary code |
Assembler | Program that turns assembly into machine code | A translator turning an Urdu letter into English, word by word |
Register | Very small, very fast storage inside the CPU (32 bits each in MIPS) | The coins in your hand vs money in the bank (memory) |
Directive | A command for the assembler, starts with a dot (.data, .text, .asciiz) | Headings in an exam paper that are not questions |
Label | A name for an address, ends with a colon (main:, msg:) | A house number on a street |
System call (syscall) | Asks MARS to do a service such as print or read | Calling a helpline: you say the service number, they do the job |
Run I/O | The MARS window where your program prints and reads | The screen and keyboard of the program |
3. Background
3.1 Installing and opening MARS
- Install Java (version 8 or later). Check with: java -version
- Download Mars4_5.jar from the MARS page of Missouri State University (free).
- Open it by double-clicking, or with: java -jar Mars4_5.jar
- Make a folder such as D:\CS704\P01 and save every program there with the .asm extension.
3.2 The MARS window
Part | What it shows / does |
|---|---|
Edit tab | Where you type and save your program (File > New, File > Save as ...asm). |
Execute tab | Appears after Assemble: Text Segment (instructions and machine code), Data Segment (memory). |
Registers pane (right) | All 32 registers plus pc, hi and lo. Changed values are highlighted. |
Messages / Run I/O (bottom) | Assembler messages, and the input/output of your running program. |
Toolbar | Assemble (F3), Go (F5), Step (F7), Backstep (F8), Reset (F12). |
Tip: Settings > 'Values displayed in hexadecimal' switches the Registers pane between hex and decimal. Default is hexadecimal. |
3.3 Structure of a MIPS program
Program skeleton |
3.4 The 32 MIPS registers
Number | Name | Use |
|---|---|---|
$0 | $zero | Always 0. Writing to it has no effect. |
$1 | $at | Reserved for the assembler (pseudo-instructions, Practical 2). |
$2-$3 | $v0-$v1 | Values returned; $v0 also selects the syscall service. |
$4-$7 | $a0-$a3 | Arguments; $a0 holds the value to print. |
$8-$15, $24-$25 | $t0-$t9 | Temporaries. |
$16-$23 | $s0-$s7 | Saved values (kept across procedure calls). |
$26-$27 | $k0-$k1 | Reserved for the operating system. |
$28-$31 | $gp, $sp, $fp, $ra | Global, stack and frame pointers; return address (Practical 13) |
3.5 System calls and instructions used today
Service | $v0 | Input | Result |
|---|---|---|---|
print integer | 1 | $a0 = integer | prints the number |
print string | 4 | $a0 = address of .asciiz string | prints the text |
read integer | 5 | - | $v0 = number typed by the user |
exit | 10 | - | ends the program |
Instruction | Meaning | Example |
|---|---|---|
add rd, rs, rt | rd = rs + rt | add $t2, $t0, $t1 |
sub rd, rs, rt | rd = rs - rt | sub $t3, $t0, $t1 |
addi rt, rs, imm | rt = rs + constant | addi $t4, $t2, -2 |
li rt, imm | load a constant | li $t0, 25 |
move rd, rs | copy a register | move $s0, $t4 |
la rt, label | load the address of a label | la $a0, msg |
4. Time Plan (50 minutes)
Time | Activity | Marks |
|---|---|---|
0-8 | Open MARS; walk through Background 3.1-3.5 | - |
8-16 | Part A: trace registers with Step (F7) | - |
16-21 | Part B: predict, then run | - |
21-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: Trace the Registers
Type the program below in MARS, save it as P01_A1_trace.asm and press Assemble (F3). Then press Step (F7) one instruction at a time and watch the Registers pane.
P01_A1_trace.asm |
Fill in the table after each step. Give each value in decimal and in hexadecimal (as MARS shows it).
Instruction | Register changed | Value (decimal) | Value (hex) |
|---|---|---|---|
li $t0, 25 | |||
li $t1, 17 | |||
add $t2, $t0, $t1 | |||
sub $t3, $t0, $t1 | |||
addi $t4, $t2, -2 | |||
move $s0, $t4 |
A1. Which register changes when 'li $v0, 10' runs, and what does that value tell syscall to do?
A2. How many instructions did MARS step through from the first instruction to the end?
Part B - Predict the Output
Write your prediction of the output first, then run the program in MARS and compare. Every program ends with the exit service, so MARS adds the line '-- program is finished running --' after your output.
B1. Order of operands in sub
.text |
My prediction (write BEFORE running): |
Actual output in MARS / was I right? Why? |
B2. Can we change $zero?
.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 stops MARS before the program runs. A logic error gives no message but prints the wrong answer. Run each one, read what MARS shows, then find and fix the line.
C1. Sum of two numbers (assemble error)
P01_C1_bug.asm |
Bug type (assemble error / runtime error / logic error), line number, and your fix: |
C2. Printing the year (logic error)
P01_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 (P01_T1.asm, P01_T2.asm, P01_T3.asm) with comments on every important line. Your output must match the sample run (use your own name and roll number in Task 1). Values typed by the user are shown after the prompts.
Task 1 [Easy · 3 marks]: Greeting card
Print three lines: a greeting to CS-704, your name and your roll number. Use one .asciiz string per line.
Sample run:
Record: file name of your program, and paste your output here (code goes in the lab book): |
Task 2 [Medium · 3 marks]: Sum and difference
Read two integers with prompts. Print their sum and their difference (first - second) on separate lines, with labels as in the sample.
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]: Swap without a third register
Read a into $s0 and b into $s1. Swap them using only add and sub (no third register, no move between them), then print them.
Hint: a = a + b; b = a - b; a = a - b |
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. MIPS has exactly 32 registers. How many bits does an instruction need to name one register? Why not have 1,000 registers?
E2. Why is a register that is always zero useful to the ISA designer?
E3. Why must you copy $v0 into another register after reading an integer?
Marking Rubric
Task | Marks | Full marks | Partial marks | Zero |
|---|---|---|---|---|
T1 | 3 | Correct three lines, own name/roll no., comments | Missing newline or one string wrong (1-2) | Does not assemble |
T2 | 3 | Prompts, saved inputs, correct sum and difference, exact format | Operand order or format errors (1-2) | Does not assemble / no input |
T3 | 4 | Swap with add/sub only, works with negatives | Uses a temporary register (2) | No swap |
Submission Rules
- Save files as P01_T1.asm, P01_T2.asm and P01_T3.asm in a folder named RollNo_P01 (e.g., 2025-ag-1234_P01).
- Copy the code and the Run I/O output of each task into your lab book before the next practical.
- 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.