Coa Lab-1
Coa Lab-1
ISLAMABAD
CSC371
COMPUTER ARCHITECTURE AND
ORGANIZATION
LAB REPORT =01
DATE: 25/09/2024
INTRODUCTION TO MIPS ASSEMBLY
TASK NO-01:
Write a program that takes marks of 10 students and then displays the average.
CODE:
.data # Data segment
marks: .word 0:10 # Reserve space for 10 integers (marks of students)
prompt: .asciiz "Enter marks: " # Prompt message
result_msg: .asciiz "Average marks: "
.text # Code segment
.globl main # Declare main as global entry point
main:
# Initialize variables
li $t0, 0 # $t0 = i (loop counter)
li $t1, 0 # $t1 = sum (for storing sum of marks)
input_loop:
# Check if we have taken input for 10 students
li $t2, 10 # $t2 = 10
beq $t0, $t2, calc_average # If $t0 == 10, go to average calculation
# Prompt user for input
li $v0, 4 # syscall to print string
la $a0, prompt # load address of prompt
syscall # make system call
# Read input (student's marks)
li $v0, 5 # syscall to read integer
syscall # make system call
move $t3, $v0 # store input in $t3
# Add input to sum
add $t1, $t1, $t3 # sum += marks
# Increment loop counter
addi $t0, $t0, 1 # i++
# Repeat loop
j input_loop
calc_average:
# Calculate average
li $t2, 10 # $t2 = 10 (number of students)
div $t1, $t2 # Divide sum by 10
mflo $t4 # Move quotient (average) to $t4
# Display result message
li $v0, 4 # syscall to print string
la $a0, result_msg # load address of result message
syscall # make system call
# Print average
li $v0, 1 # syscall to print integer
move $a0, $t4 # move average to $a0
syscall # make system call
# Exit the program
li $v0, 10 # syscall to exit
syscall
OUTPUT:
TASK NO-02:
Write a program that inputs three 1-digit numbers and combines them to form a 3 digit
number. For example input 4, 8 ,7, output=487.
CODE:
.data
prompt1: .asciiz "Enter first digit: " # Prompt for the first digit
prompt2: .asciiz "Enter second digit: " # Prompt for the second digit
prompt3: .asciiz "Enter third digit: " # Prompt for the third digit
result_msg: .asciiz "The combined number is: " # Result message
.text
.globl main
main:
# Step 1: Input first digit
li $v0, 4 # syscall to print string
la $a0, prompt1 # load address of prompt1
syscall # make system call
li $v0, 5 # syscall to read integer
syscall # get input for first digit
move $t0, $v0 # store first digit in $t0
# Step 2: Input second digit
li $v0, 4 # syscall to print string
la $a0, prompt2 # load address of prompt2
syscall # make system call
li $v0, 5 # syscall to read integer
syscall # get input for second digit
move $t1, $v0 # store second digit in $t1
# Step 3: Input third digit
li $v0, 4 # syscall to print string
la $a0, prompt3 # load address of prompt3
syscall # make system call
li $v0, 5 # syscall to read integer
syscall # get input for third digit
move $t2, $v0 # store third digit in $t
# Step 4: Combine the digits to form the three-digit number
li $t3, 100 # load 100 into $t3
mul $t0, $t0, $t3 # first digit * 100
li $t3, 10 # load 10 into $t3
mul $t1, $t1, $t3 # second digit * 10
add $t0, $t0, $t1 # add first and second digits
add $t0, $t0, $t2 # add third digit to form final number
# Step 5: Print result message
li $v0, 4 # syscall to print string
la $a0, result_msg # load address of result message
syscall # make system call
# Step 6: Print the combined number
li $v0, 1 # syscall to print integer
move $a0, $t0 # move the combined number to $a0
syscall # make system call
# Exit the program
li $v0, 10 # syscall to exit
syscall
OUTPUT:
TASK NO-03:
Write a program that inputs marks of four quizzes and four assignments, each out of 10,
and the calculates total marks out of 25. Total quiz marks 15/25 and Assignment marks
= 10/25
CODE:
.data
prompt_quiz1: .asciiz "Enter marks for Quiz 1 (out of 10): "
prompt_quiz2: .asciiz "Enter marks for Quiz 2 (out of 10): "
prompt_quiz3: .asciiz "Enter marks for Quiz 3 (out of 10): "
prompt_quiz4: .asciiz "Enter marks for Quiz 4 (out of 10): "
prompt_ass1: .asciiz "Enter marks for Assignment 1 (out of 10): "
prompt_ass2: .asciiz "Enter marks for Assignment 2 (out of 10): "
prompt_ass3: .asciiz "Enter marks for Assignment 3 (out of 10): "
prompt_ass4: .asciiz "Enter marks for Assignment 4 (out of 10): "
result_msg: .asciiz "Total marks out of 25: "
.text
.globl main
main:
# Input Quiz Marks
li $v0, 4 # syscall to print string
la $a0, prompt_quiz1 # load prompt for Quiz 1
syscall # display prompt
li $v0, 5 # syscall to read integer
syscall # read Quiz 1 marks
move $t0, $v0 # store in $t0 (Quiz 1 marks)
li $v0, 4
la $a0, prompt_quiz2 # load prompt for Quiz 2
syscall
li $v0, 5
syscall
move $t1, $v0 # store in $t1 (Quiz 2 marks)
li $v0, 4
la $a0, prompt_quiz3 # load prompt for Quiz 3
syscall
li $v0, 5
syscall
move $t2, $v0 # store in $t2 (Quiz 3 marks)
li $v0, 4
la $a0, prompt_quiz4 # load prompt for Quiz 4
syscall
li $v0, 5
syscall
move $t3, $v0 # store in $t3 (Quiz 4 marks)
li $v0, 4
la $a0, prompt_ass2 # load prompt for Assignment 2
syscall
li $v0, 5
syscall
move $t5, $v0 # store in $t5 (Assignment 2 marks)
li $v0, 4
la $a0, prompt_ass3 # load prompt for Assignment 3
syscall
li $v0, 5
syscall
move $t6, $v0 # store in $t6 (Assignment 3 marks)
li $v0, 4
la $a0, prompt_ass4 # load prompt for Assignment 4
syscall
li $v0, 5
syscall
move $t7, $v0 # store in $t7 (Assignment 4 marks)
OUTPUT:
CRITICAL ANALYSIS
In Task 1, the code efficiently uses loops to gather marks from 10 students, sum them
up, and calculate the average. Core MIPS assembly instructions like `addi`, `li`, `beq`,
and `div` manage the arithmetic and loop control, with a clear condition to exit after 10
inputs. Task 2 demonstrates how to combine three 1-digit numbers into a 3-digit number
using multiplication and addition. It multiplies the first and second digits by powers of 10
to correctly position them in the hundreds and tens places. Task 3 calculates total quiz
and assignment marks out of 25, scaling them to 15/25 and 10/25, respectively. The final
result is displayed by combining these scaled marks, making the logic easy to follow .