Assignment 01
Assignment 01
.text
main:
lw $t0, array_length # Load the length of the array into register $t0 (Temporary)
la $t1, array # Load the base address of the array into register $t1 (Temporary)
li $s0, 0 # Initialize sum to 0, stored in register $s0 (Saved)
loop:
lw $t2, 0($t1) # Load current element into register $t2 (Temporary)
add $s0, $s0, $t2 # Add element to sum (update register $s0)
addi $t1, $t1, 4 # Move to the next element address
addi $t0, $t0, -1 # Decrease the length counter (update register $t0)
bnez $t0, loop # Continue the loop if length > 0
Output:
QUESTION 02:
Implement a MIPS program to find the maximum element in an array.
.data
array: .word 5, 3, 9, 7, 1, 8, 6, 2, 4
array_length: .word 9 # Length of the array
max_label: .asciiz "Maximum Element: " # Label for printing
.text
main:
la $a0, array # Load array's starting address into $a0
lw $t0, array_length # Load array length into $t0
lw $t1, 0($a0) # Initialize max with the first element
find_max_loop:
beq $t0, $zero, end_find_max # Exit loop if array length is 0
lw $t2, 0($a0) # Load the current element
bgt $t2, $t1, update_max # If current > max, update max
j next_iteration
update_max:
move $t1, $t2 # Update max with the current element
next_iteration:
addi $a0, $a0, 4 # Move to the next element
subi $t0, $t0, 1 # Decrease array length
j find_max_loop
end_find_max:
li $v0, 4 # System call for print_str
la $a0, max_label
syscall
Output:
QUESTION 03:
Find the sum of all elements in a 2D array:
.data
array: .word 1, 2, 3, 4
.word 5, 6, 7, 8
.word 9, 10, 11, 12
rows: .word 3 # Number of rows
cols: .word 4 # Number of columns
.text
main:
lw $t0, rows # Load number of rows into register $t0 (Temporary)
lw $t1, cols # Load number of columns into register $t1 (Temporary)
la $t2, array # Load base address of 2D array into register $t2 (Temporary)
li $s0, 0 # Initialize sum to 0, stored in register $s0 (Saved)
outer_loop:
li $t3, 0 # Initialize inner loop counter to 0
inner_loop:
lw $t4, 0($t2) # Load current element into register $t4 (Temporary)
add $s0, $s0, $t4 # Add element to sum (update register $s0)
addi $t2, $t2, 4 # Move to the next element in the row
addi $t3, $t3, 1 # Increment inner loop counter
blt $t3, $t1, inner_loop # Continue inner loop if not all columns are processed
# Debug output
move $a0, $s0 # Load the sum into argument register $a0
li $v0, 1 # System call code for print integer
syscall
QUESTION 04:
Write a MIPS program to check if a given string, is a palindrome.
.data
string: .asciiz "level" # Example string
.text
main:
# Load the string address into $a0
la $a0, string
end_find_length:
subi $t0, $t0, 1 # Adjust $t0 to point to the last character of the string
not_palindrome:
# Print the result
li $v0, 4 # System call for print_str
la $a0, not_palindrome_msg
syscall
j end_program
yes_palindrome:
# Print the result
li $v0, 4 # System call for print_str
la $a0, palindrome_msg
syscall
end_program:
# Exit the program
li $v0, 10
syscall
.data
palindrome_msg: .asciiz "The string is a palindrome.\n"
not_palindrome_msg: .asciiz "The string is not a palindrome.\n"
Output:
QUESTION 05:
Implement a MIPS program to sort an array of integers in ascending order.
.data
array: .word 2, 1, 5, 3, 6, 8, 7, 4 # Array of integers (terminated with 0)
.text
main:
la $a0, array # Load base address of the array into $a0
jal bubble_sort # Jump to the bubble_sort subroutine
# Exit program
li $v0, 10 # System call code for program exit
syscall
bubble_sort:
li $t0, 1 # Outer loop index
outer_loop:
li $t1, 0 # Inner loop index
li $t2, 0 # Swap flag
inner_loop:
lw $t3, 4($a0) # Load array[i+1] into $t3
bnez $t3, check_swap # Check if array[i+1] is not the sentinel
check_swap:
lw $t4, 0($a0) # Load array[i] into $t4
ble $t4, $t3, no_swap # If array[i] <= array[i+1], no swap needed
# Swap array[i] and array[i+1]
sw $t3, 0($a0)
sw $t4, 4($a0)
li $t2, 1 # Set the swap flag to indicate a swap occurred
no_swap:
addi $a0, $a0, 4 # Move to the next element in the array
addi $t1, $t1, 1 # Increment inner loop index
bnez $t3, inner_loop # Continue inner loop if array[i+1] is not the sentinel
end_sort:
jr $ra # Return from subroutine
print_array:
la $t0, array # Load base address of the array into $t0
print_loop:
lw $t1, 0($t0) # Load array element into $t1
bnez $t1, print_element # Check if the element is not the sentinel
print_element:
# Print integer
li $v0, 1 # System call code for print_integer
move $a0, $t1 # Move the integer to $a0
syscall
# Print space
li $v0, 11 # System call code for print_character
li $a0, 32 # ASCII code for space
syscall
.text
.globl main
main:
# Load the decimal number
lw $t0, decimal_num
# Initialize variables
li $t1, 1 # Set the mask to the rightmost bit
# Print header
li $v0, 4 # System call code for print_str
la $a0, header_str # Load the address of the header string
syscall
print_zero:
li $v0, 1 # System call code for print_int
li $a0, 0 # Print 0
syscall
next_iteration:
srl $t0, $t0, 1 # Shift the decimal number to the right
sll $t1, $t1, 1 # Shift the mask to the left
bnez $t0, convert_loop # Continue the loop if the decimal number is not zero
# Print a newline
li $v0, 4 # System call code for print_str
la $a0, newline_str # Load the address of the newline string
syscall
# Exit program
li $v0, 10 # System call code for program exit
syscall
.data
header_str: .asciiz "Binary representation: "
newline_str: .asciiz "\n"
Output:
QUESTION 07:
Write a MIPS program to calculate the Fibonacci sequence up to a given
.data
limit: .word 10 # Change this value to the desired limit
.text
.globl main
main:
# Load the limit
lw $t0, limit
# Print header
li $v0, 4 # System call code for print_str
la $a0, header_str # Load the address of the header string
syscall
fibonacci_loop:
add $t3, $t3, 1 # Increment counter
add $t4, $t1, $t2 # Calculate the next Fibonacci number
bge $t3, $t0, end_program # Exit loop if the counter exceeds the limit
j fibonacci_loop
end_program:
li $v0, 10 # System call code for program exit
syscall
.data
header_str: .asciiz "Fibonacci sequence up to the limit: "
comma_space: .asciiz ", "
newline_str: .asciiz "\n"
Output:
QUESTION 08:
Implement a MIPS program to find the GCD (Greatest Common Divisor) of two
numbers.
.data
prompt1: .asciiz "Enter the first number: "
prompt2: .asciiz "Enter the second number: "
result: .asciiz "GCD: "
newline: .asciiz "\n" # Newline character
.text
main:
# Display prompt to enter the first number
li $v0, 4
la $a0, prompt1
syscall
euclid_gcd:
# GCD calculation using Euclid's algorithm
beqz $t1, end_euclid_gcd # if t1 is 0, the GCD is t0
euclid_loop:
# t2 = t0 % t1
div $t0, $t1
mfhi $t2
# t0 = t1, t1 = t2
move $t0, $t1
move $t1, $t2
end_euclid_gcd:
# Print the GCD
li $v0, 1
move $a0, $t0
syscall
# Print a newline
li $v0, 4
la $a0, newline
syscall
Output:
QUESTION 09:
Write a MIPS program to calculate the average of a set of numbers.
.data
user_prompt: .asciiz "Please enter the number of values: "
input_message: .asciiz "Enter a value: "
output_result: .asciiz "Average: "
newline_character: .asciiz "\n"
.text
main:
# Display a prompt to get the count of numbers
li $v0, 4 # System call code for print_str
la $a0, user_prompt # Load the address of the user prompt
syscall
# Initialize sum to 0
li $t1, 0
input_loop:
# Read a number from the user
li $v0, 5 # System call code for read_int
syscall
add $t1, $t1, $v0 # Add the input to the sum
# Print a newline
li $v0, 4 # System call code for print_str
la $a0, newline_character # Load the address of the newline character
syscall
exit_program:
# Exit the program
li $v0, 10 # System call code for program exit
syscall
Output:
QUESTION 10:
Implement a MIPS program to perform basic arithmetic operations on two numbers.
.data
prompt1: .asciiz "Enter the first number: "
prompt2: .asciiz "Enter the second number: "
result_add: .asciiz "Addition result: "
result_sub: .asciiz "Subtraction result: "
result_mul: .asciiz "Multiplication result: "
result_div: .asciiz "Division result: "
error_div: .asciiz "Error: Division by zero"
newline: .asciiz "\n"
.text
main:
# Display prompt to enter the first number
li $v0, 4
la $a0, prompt1
syscall
# Perform addition
add $s2, $s0, $s1
# Display addition result
li $v0, 4
la $a0, result_add
syscall
li $v0, 1
move $a0, $s2
syscall
# Print newline
li $v0, 4
la $a0, newline
syscall
# Perform subtraction
sub $s3, $s0, $s1
# Display subtraction result
li $v0, 4
la $a0, result_sub
syscall
li $v0, 1
move $a0, $s3
syscall
# Print newline
li $v0, 4
la $a0, newline
syscall
# Perform multiplication
mul $s4, $s0, $s1
# Display multiplication result
li $v0, 4
la $a0, result_mul
syscall
li $v0, 1
move $a0, $s4
syscall
# Print newline
li $v0, 4
la $a0, newline
syscall
perform_division:
# Perform division
div $s0, $s1
# Display division result
li $v0, 4
la $a0, result_div
syscall
li $v0, 1
mflo $a0
syscall
# Print newline
li $v0, 4
la $a0, newline
syscall
end_program:
# Exit the program
li $v0, 10
syscall
Output: