0% found this document useful (0 votes)
13 views

Assignment 01

The document contains questions about MIPS assembly language programs to perform tasks such as finding the sum and maximum of elements in an array, checking if a string is a palindrome, sorting an array, converting a decimal number to binary, and calculating the Fibonacci sequence up to a limit. MIPS programs are provided as examples to implement these tasks.

Uploaded by

Umar Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment 01

The document contains questions about MIPS assembly language programs to perform tasks such as finding the sum and maximum of elements in an array, checking if a string is a palindrome, sorting an array, converting a decimal number to binary, and calculating the Fibonacci sequence up to a limit. MIPS programs are provided as examples to implement these tasks.

Uploaded by

Umar Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

QUESTION 01:

Write a MIPS program to find the sum of all elements in an array.


.data
array: .word 1, 2, 3, 4, 5 # Example array
array_length: .word 5 # Length of the array

.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

# $s0 now contains the sum of array elements


# You can use $s0 for further computations or print it

# Print the result


move $a0, $s0 # Load the sum into argument register $a0
li $v0, 1 # System call code for print integer
syscall

# Exit the program


li $v0, 10 # System call code for exit
syscall

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

li $v0, 1 # System call for print_int


move $a0, $t1
syscall

li $v0, 10 # Exit the program


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

addi $t0, $t0, -1 # Decrement outer loop counter


bnez $t0, outer_loop # Continue outer loop if not all rows are processed

# Debug output
move $a0, $s0 # Load the sum into argument register $a0
li $v0, 1 # System call code for print integer
syscall

# Exit the program


li $v0, 10 # System call code for exit
syscall
Output:

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

# Find the length of the string


move $t0, $a0 # Copy the string address to $t0
find_length:
lb $t1, 0($t0) # Load a byte from the string
beq $t1, $zero, end_find_length # Exit loop if the null terminator is found
addi $t0, $t0, 1 # Move to the next character
j find_length

end_find_length:
subi $t0, $t0, 1 # Adjust $t0 to point to the last character of the string

# Check if the string is a palindrome


is_palindrome:
beq $a0, $t0, yes_palindrome # If the pointers meet, it's a palindrome
lb $t2, 0($a0) # Load a byte from the start of the string
lb $t3, 0($t0) # Load a byte from the end of the string
bne $t2, $t3, not_palindrome # If the characters don't match, it's not a palindrome
addi $a0, $a0, 1 # Move to the next character from the start
subi $t0, $t0, 1 # Move to the next character from the end
j is_palindrome

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

# Print the sorted array


la $a0, array
jal print_array

# 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

# If array[i+1] is the sentinel, we are done with sorting


bnez $t2, outer_loop # If swaps occurred in the previous pass, continue outer loop
j end_sort

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

addi $a0, $a0, -4 # Move back to the start of the array


bnez $t2, outer_loop # Continue outer loop if a swap occurred in this pass

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

# If the element is the sentinel, print a newline and return


li $v0, 11 # System call code for print_character
li $a0, 10 # ASCII code for newline
syscall
jr $ra # Return from subroutine

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

addi $t0, $t0, 4 # Move to the next element in the array


j print_loop
Output:
QUESTION 06:
Create a MIPS program to convert a decimal number to binary.
.data
decimal_num: .word 27 # Change this value to the decimal number you want to convert

.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

# Loop to convert decimal to binary


convert_loop:
and $t2, $t0, $t1 # Extract the rightmost bit of the decimal number
beqz $t2, print_zero # If the bit is 0, print 0
li $v0, 1 # System call code for print_int
li $a0, 1 # Print 1
syscall
j next_iteration

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

# Initialize Fibonacci sequence


li $t1, 0 # First Fibonacci number
li $t2, 1 # Second Fibonacci number

# Print header
li $v0, 4 # System call code for print_str
la $a0, header_str # Load the address of the header string
syscall

# Print initial values


li $v0, 1 # System call code for print_int
move $a0, $t1 # Print the first Fibonacci number
syscall
li $v0, 4 # System call code for print_str
la $a0, comma_space # Load the address of the comma and space string
syscall
li $v0, 1 # System call code for print_int
move $a0, $t2 # Print the second Fibonacci number
syscall
li $v0, 4 # System call code for print_str
la $a0, newline_str # Load the address of the newline string
syscall

# Calculate and print Fibonacci sequence up to the limit


li $t3, 2 # Counter starting from the 3rd Fibonacci number

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

# Print the Fibonacci number


li $v0, 1 # System call code for print_int
move $a0, $t4 # Print the next Fibonacci number
syscall
li $v0, 4 # System call code for print_str
la $a0, comma_space # Load the address of the comma and space string
syscall

# Update Fibonacci numbers for the next iteration


move $t1, $t2
move $t2, $t4

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

# Read the first number from input


li $v0, 5
syscall
move $t0, $v0 # Save the first number in $t0

# Display prompt to enter the second number


li $v0, 4
la $a0, prompt2
syscall

# Read the second number from input


li $v0, 5
syscall
move $t1, $v0 # Save the second number in $t1

# Display the result message


li $v0, 4
la $a0, result
syscall

# Calculate and print the GCD


jal euclid_gcd

# Exit the program


li $v0, 10
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

# Repeat the loop until t1 becomes 0


bnez $t1, euclid_loop

end_euclid_gcd:
# Print the GCD
li $v0, 1
move $a0, $t0
syscall

# Print a newline
li $v0, 4
la $a0, newline
syscall

# Return from the function


jr $ra

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

# Read the count of numbers from the user


li $v0, 5 # System call code for read_int
syscall
move $t0, $v0 # Store the count in $t0

# Check if the count is zero


beqz $t0, exit_program

# Initialize sum to 0
li $t1, 0

# Loop to input numbers and calculate sum


la $a0, input_message # Load the address of the input message
li $v0, 4 # System call code for print_str
syscall

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

# Decrement the count


sub $t0, $t0, 1

# Check if more numbers need to be entered


bnez $t0, input_loop

# Calculate and print the average


li $v0, 4 # System call code for print_str
la $a0, output_result # Load the address of the output result
syscall

# Perform division only if the count is not zero


div $t1, $t1, $v0 # Divide sum by count
mflo $t1 # Save the quotient (average) in $t1

# Print the average


li $v0, 1 # System call code for print_int
move $a0, $t1 # Move the average to $a0
syscall

# 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

# Read the first number from input


li $v0, 5
syscall
move $s0, $v0 # Save the first number in $s0

# Display prompt to enter the second number


li $v0, 4
la $a0, prompt2
syscall

# Read the second number from input


li $v0, 5
syscall
move $s1, $v0 # Save the second number in $s1

# 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

# Check if the second number is not zero before division


bnez $s1, perform_division

# Handle division by zero


li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, error_div
syscall
j end_program

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:

You might also like