Introduction to Computer Organization (CCIT4026)
Lab 03: Calculator (Addition, Subtraction, Multiplication, Division, Logical Shift)
This document gives answers to the lab tasks and detailed explanations for each part of the solution.
1. Understanding the Problem
1.1 Formula
The program must compute:
Result = (quotient : remainder) = ((B + C) × A − (D ≪ 4)) / E
Where:
- (D ≪ 4) means D left-shifted by 4 bits (same as D × 16).
- quotient = integer division: var3 / E
- remainder = modulo: var3 % E
1.2 Constraint on Inputs
The five integers must satisfy:
5 ≤ E < D < C < B < A ≤ 20
So: - E is the smallest (at least 5). - A is the largest (at most 20). - Order: E < D < C < B < A.
The lab asks the user to enter values in that order: E, then D, then C, then B, then A.
1.3 Variables to Compute and Store (in data memory)
| Variable | Formula | Meaning |
|---|---|---|
| var1 | B + C | First addition |
| var2 | var1 × A | Multiplication (use lower 32 bits only) |
| var3 | var2 − (D ≪ 4) | Subtract (D left-shifted by 4) from var2 |
| quotient | var3 / E | Integer quotient of var3 ÷ E |
| remainder | var3 % E | Remainder of var3 ÷ E |
2. Manual Calculation (Sample)
Use values that satisfy 5 ≤ E < D < C < B < A ≤ 20. Example: E=5, D=6, C=7, B=8, A=9.
| Step | Calculation | Result |
|---|---|---|
| var1 | B + C = 8 + 7 | 15 |
| var2 | var1 × A = 15 × 9 | 135 |
| D ≪ 4 | 6 × 16 | 96 |
| var3 | var2 − (D ≪ 4) = 135 − 96 | 39 |
| quotient | var3 / E = 39 / 5 | 7 |
| remainder | var3 % E = 39 % 5 | 4 |
Note: In the lab PDF sample it says “var3 = var2 − (A ≪ 4)”. That is a typo; the formula uses (D ≪ 4), not (A ≪ 4). With D=6, (D ≪ 4) = 96, and 135 − 96 = 39.
3. MIPS Solution — Complete Assembly Program
The following program follows the lab’s pseudo-code steps and uses only instructions from the course (MARS syscalls 1, 4, 5, 10; add, sub, mul, div, mflo, mfhi, sll, lw, sw, li, la, jr, etc.).
# ICO Lab 03 - Calculator
# Formula: Result = (quotient : remainder) = ((B+C)*A - (D<<4)) / E
# Constraint: 5 <= E < D < C < B < A <= 20
# Input order: E, D, C, B, A
.data
promptE: .asciiz "Please enter the value E: "
promptD: .asciiz "Please enter the value D: "
promptC: .asciiz "Please enter the value C: "
promptB: .asciiz "Please enter the value B: "
promptA: .asciiz "Please enter the value A: "
msg_var1: .asciiz "var1 = (B + C) = "
msg_var2: .asciiz "var2 = var1 * A = "
msg_var3: .asciiz "var3 = var2 - (D << 4) = "
msg_quot: .asciiz "quotient = var3 / E = "
msg_rem: .asciiz "remainder = var3 % E = "
newline: .asciiz "\n"
author: .asciiz "[Your Name, Class, Student ID]\n"
E: .word 0
D: .word 0
C: .word 0
B: .word 0
A: .word 0
var1: .word 0
var2: .word 0
var3: .word 0
quotient: .word 0
remainder: .word 0
.text
.globl main
main:
# ========== Step 1 ==========
# Step 1.1 # Display a prompt, "Please enter the value E: "
li $v0, 4
la $a0, promptE
syscall
# Step 1.2 # Setup to wait for the user to enter a non-zero positive integer.
li $v0, 5
syscall
# Step 1.3 # Store the entered value to the variable E defined in the data segment.
sw $v0, E
# ========== Step 2 ==========
# Step 2.1 # Display a prompt, "Please enter the value D: "
li $v0, 4
la $a0, promptD
syscall
# Step 2.2 # Setup to wait for the user to enter a non-zero positive integer
li $v0, 5
syscall
# Step 2.3 # Store the entered value to the variable D defined in the data segment.
sw $v0, D
# ========== Step 3 ==========
# Step 3.1 # Display a prompt, "Please enter the value C: "
li $v0, 4
la $a0, promptC
syscall
# Step 3.2 # Setup to wait for the user to enter a non-zero positive integer
li $v0, 5
syscall
# Step 3.3 # Store the entered value to the variable C defined in the data segment.
sw $v0, C
# ========== Step 4 ==========
# Step 4.1 # Display a prompt, "Please enter the value B: "
li $v0, 4
la $a0, promptB
syscall
# Step 4.2 # Setup to wait for the user to enter a non-zero positive integer
li $v0, 5
syscall
# Step 4.3 # Store the entered value to the variable B defined in the data segment.
sw $v0, B
# ========== Step 5 ==========
# Step 5.1 # Display a prompt, "Please enter the value A: "
li $v0, 4
la $a0, promptA
syscall
# Step 5.2 # Setup to wait for the user to enter a non-zero positive integer
li $v0, 5
syscall
# Step 5.3 # Store the entered value to the variable A defined in the data segment.
sw $v0, A
# ========== Step 6 ==========
# Step 6.1 # Load the value stored in variable B and C to two registers.
lw $t0, B
lw $t1, C
# Step 6.2 # Add the values in the two registers.
add $t2, $t0, $t1
# Step 6.3 # Store the addition result to the variable var1.
sw $t2, var1
# ========== Step 7 ==========
# Step 7.1 # Load the value stored in variable A to a register.
lw $t3, A
# Step 7.2 # Multiply the value in var1 to the register.
mul $t4, $t2, $t3
# Step 7.3 # Store the lower 32-bit of the multiplication result to the variable var2.
sw $t4, var2
# ========== Step 8 ==========
# Step 8.1 # Load the values stored in variable D to a register.
lw $t5, D
# Step 8.2 # Left shift the register value (by 4).
sll $t5, $t5, 4
# Step 8.3 # Subtract the bit shifted result from var2.
sub $t6, $t4, $t5
# Step 8.4 # Store the subtraction result to the variable var3.
sw $t6, var3
# ========== Step 9 ==========
# Step 9.1 # Load the value stored in variable E to a register.
lw $t7, E
# Step 9.2 # Divide the var3 in the register by the variable E.
div $t6, $t7 # lo: quotient, hi: remainder
# Step 9.3 # Store the quotient of the division result to the variable quotient.
mflo $t8
sw $t8, quotient
# Step 9.4 # Store the remainder of the division result to the variable remainder.
mfhi $t9
sw $t9, remainder
# Step 9.5 # Print all the intermediate results according to the sample output format.
li $v0, 4
la $a0, msg_var1
syscall
li $v0, 1
lw $a0, var1
syscall
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, msg_var2
syscall
li $v0, 1
lw $a0, var2
syscall
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, msg_var3
syscall
li $v0, 1
lw $a0, var3
syscall
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, msg_quot
syscall
li $v0, 1
lw $a0, quotient
syscall
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, msg_rem
syscall
li $v0, 1
lw $a0, remainder
syscall
li $v0, 4
la $a0, newline
syscall
# Step 9.6 # Print the Author message.
li $v0, 4
la $a0, author
syscall
# Step 9.7 # Exit from the program.
li $v0, 10
syscall
Important: Replace [Your Name, Class, Student ID] in the author string with your own details before submission.
4. Detailed Explanation of Each Solution Step
4.1 Data Section (.data)
- Prompts (
promptE,promptD, …):.asciizstrings for “Please enter the value E: ”, etc. Syscall 4 prints the string whose address is in$a0. - Result messages (
msg_var1, …): Strings like"var1 = (B + C) = "so the output matches the required format. - Variables (
E,D,C,B,A,var1,var2,var3,quotient,remainder):.word 0reserves one word (4 bytes) per variable. Inputs are stored here after each read; computed values are stored here as specified in the pseudo-code.
4.2 Steps 1–5: Reading E, D, C, B, A
Each step does the same pattern:
- Print prompt:
li $v0, 4,la $a0, promptX,syscall— MARS syscall 4 = “print string”; the string address must be in$a0. - Read integer:
li $v0, 5,syscall— syscall 5 = “read integer”; the value is returned in$v0. - Store to memory:
sw $v0, E(or D, C, B, A) — store the word in$v0at the labelE(or D, C, B, A).
We read in order E, D, C, B, A so that the user supplies values from smallest to largest, matching the constraint E < D < C < B < A.
4.3 Step 6: var1 = B + C
- 6.1 Load B and C:
lw $t0, Bandlw $t1, C. Now$t0 = B,$t1 = C. - 6.2 Add:
add $t2, $t0, $t1→$t2 = B + C. - 6.3 Store:
sw $t2, var1→var1 = B + C.
add is R-type; result is in the destination register. We keep $t2 for the next step (var1 is also needed for var2).
4.4 Step 7: var2 = var1 × A (lower 32 bits)
- 7.1 Load A:
lw $t3, A→$t3 = A. (We already have var1 in$t2.) - 7.2 Multiply:
mul $t4, $t2, $t3→$t4 = var1 × A. In MIPS32,mulputs the low 32 bits of the product in the destination register, which satisfies “only consider the lower 32-bit of the multiplication result”. - 7.3 Store:
sw $t4, var2→var2 = var1 * A.
4.5 Step 8: var3 = var2 − (D ≪ 4)
- 8.1 Load D:
lw $t5, D→$t5 = D. - 8.2 Left shift by 4:
sll $t5, $t5, 4→$t5 = D × 16(D ≪ 4).sllis R-type; the shift amount is 4; result overwrites the register. - 8.3 Subtract:
sub $t6, $t4, $t5→$t6 = var2 − (D ≪ 4). We use$t4(var2) and the shifted value in$t5. - 8.4 Store:
sw $t6, var3→var3 = var2 - (D << 4).
4.6 Step 9.1–9.4: Quotient and Remainder
- 9.1 Load E:
lw $t7, E→$t7 = E. - 9.2 Divide:
div $t6, $t7— divides$t6(var3) by$t7(E). MIPSdivputs quotient inloand remainder inhi. - 9.3 Quotient:
mflo $t8copieslointo$t8; thensw $t8, quotientstores it inquotient. - 9.4 Remainder:
mfhi $t9copieshiinto$t9; thensw $t9, remainderstores it inremainder.
So we get quotient = var3 / E and remainder = var3 % E using only course instructions.
4.7 Step 9.5: Printing Intermediate Results
For each of var1, var2, var3, quotient, and remainder we:
- Print a label string: syscall 4 with the address of the corresponding message.
- Print the number: syscall 1 with the value in
$a0(load from the variable withlw $a0, var1etc.). - Print a newline: syscall 4 with
newline.
This produces output in the form:
var1 = (B + C) = 15var2 = var1 * A = 135- etc.
4.8 Step 9.6–9.7: Author Message and Exit
- 9.6 Print the author line: syscall 4 with
la $a0, author. Remember to set theauthorstring in.datato your name, class, and student ID. - 9.7 Exit:
li $v0, 10,syscall— MARS syscall 10 terminates the program. The lab requires the program termination message to be shown in the Run I/O window.
5. Summary Table: Instructions Used
| Task | MIPS instructions / syscalls used |
|---|---|
| Print string | li $v0, 4, la $a0, label, syscall |
| Read integer | li $v0, 5, syscall; result in $v0 |
| Store input | sw $v0, E (or D, C, B, A) |
| Load from memory | lw reg, label |
| Add | add rd, rs, rt |
| Multiply (low 32) | mul rd, rs, rt |
| Subtract | sub rd, rs, rt |
| Left shift by 4 | sll rd, rt, 4 |
| Division | div rs, rt → quotient in lo, remainder in hi |
| Get quotient | mflo rd |
| Get remainder | mfhi rd |
| Print integer | li $v0, 1, lw $a0, var, syscall |
| Exit | li $v0, 10, syscall |
6. Checklist for Submission
- [ ] Fill in name, class, student ID (and photo if required) on the answer sheet.
- [ ] Use the exact prompts and formula from the lab (E, D, C, B, A order; var1, var2, var3, quotient, remainder).
- [ ] Replace the
authorstring with your details. - [ ] Run and single-step the program in MARS to verify (e.g. E=5, D=6, C=7, B=8, A=9 → var1=15, var2=135, var3=39, quotient=7, remainder=4).
- [ ] Include in the report: source code listing, assembled machine code (Text Segment), and Run I/O output including program termination.
- [ ] Submit both the lab report (PDF) and the
.asmfile with the required naming.
Using this solution and explanations, you can complete Lab 03 and understand each step in detail.