Write a MIPS Assembly program to sort an array of numbers. The array should have minimum of 10 numbers and can be initialized as shown in the demo program.
Demo Program:
.data
list: .word 0, 2, 6, 9, 13, 21, 23, 24, 50, 9
str1: .asciiz “\nBefore swap\n”
str2: .asciiz “\nAfter swap:\n”
space: .asciiz ” “
.text
la $a0, str1
li $v0,4
syscall
la $t0, list #pointer to list
lw $s0, 0($t0)#read list[0] into $s0
lw $s1, 4($t0)#read list[1] into $s1
print list
li $v0,1
move $a0,$s0
syscall
la $a0, space
li $v0, 4
syscall
li $v0,1
move $a0,$s1
syscall
bgt $s0, $s1, label #if list[0] > list [1] dont swap exit
else swap $s5 is a temporary variable
move $s5, $s0
move $s0, $s1
move $s1, $s5
update the list after swapping
sw $s0, 0($t0)
sw $s1, 4($t0)
print the updated list now
la $a0, str2
li $v0,4
syscall
print list[0] and list[1]
la $t0, list #pointer to list
lw $s0, 0($t0)#read list[0] into $s0
lw $s1, 4($t0)#read list[1] into $s1
print list
li $v0,1
move $a0,$s0
syscall
la $a0, space
li $v0, 4
syscall
li $v0,1
move $a0,$s1
syscall
label:
li $v0, 10
syscall
The Correct Answer and Explanation is :
To implement a sorting algorithm in MIPS Assembly, we will modify your demo program and use Bubble Sort, which is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
MIPS Assembly Code for Bubble Sort:
.data
list: .word 0, 2, 6, 9, 13, 21, 23, 24, 50, 9
str1: .asciiz "\nBefore swap\n"
str2: .asciiz "\nAfter swap:\n"
space: .asciiz " "
.text
.globl main
main:
# Print "Before swap"
la $a0, str1
li $v0, 4
syscall
# Load the address of the list into $t0
la $t0, list
# Print the list before sorting
li $t1, 0 # Index i = 0
print_list_before:
lw $a0, 0($t0) # Load list[i] into $a0
li $v0, 1 # Print integer syscall
syscall
la $a0, space
li $v0, 4 # Print space
syscall
addi $t1, $t1, 1 # Increment index
addi $t0, $t0, 4 # Move to next word in the list
blt $t1, 10, print_list_before # Repeat for 10 elements
# Bubble Sort Algorithm
la $t0, list # Reset pointer to start of list
li $t2, 10 # Set the number of elements to sort (10)
li $t3, 0 # Set a flag to track if swap happened
outer_loop:
li $t1, 0 # Reset index for inner loop
li $t3, 0 # Reset swap flag to 0
inner_loop:
lw $s0, 0($t0) # Load list[i] into $s0
lw $s1, 4($t0) # Load list[i+1] into $s1
# Compare list[i] and list[i+1]
bgt $s0, $s1, swap # If list[i] > list[i+1], swap
# No swap, move to next pair
addi $t0, $t0, 4 # Increment pointer to next pair
addi $t1, $t1, 1 # Increment index
blt $t1, $t2, inner_loop # Continue inner loop
# If no swaps occurred, sorting is done
beqz $t3, end_sort
# Reset pointer to start of list for next iteration
la $t0, list
sub $t2, $t2, 1 # Decrease number of elements to sort
j outer_loop
swap:
# Swap the elements
move $s5, $s0 # Save list[i] in $s5
move $s0, $s1 # list[i] = list[i+1]
move $s1, $s5 # list[i+1] = $s5
# Store swapped values back in list
sw $s0, 0($t0)
sw $s1, 4($t0)
# Set swap flag to 1
li $t3, 1
# Move to next pair
addi $t0, $t0, 4
addi $t1, $t1, 1
blt $t1, $t2, inner_loop
end_sort:
# Print "After swap"
la $a0, str2
li $v0, 4
syscall
# Print the sorted list
li $t1, 0 # Reset index for printing
la $t0, list # Reset pointer to start of list
print_list_after:
lw $a0, 0($t0) # Load list[i] into $a0
li $v0, 1 # Print integer syscall
syscall
la $a0, space
li $v0, 4 # Print space
syscall
addi $t1, $t1, 1 # Increment index
addi $t0, $t0, 4 # Move to next word in the list
blt $t1, 10, print_list_after # Repeat for 10 elements
# Exit the program
li $v0, 10
syscall
Explanation of the Code:
- Initialization and Setup:
- The list of numbers is initialized in the
.datasection, along with the strings for the before and after messages. - Registers are initialized:
$t0holds the address of the list,$t1is used for the index, and$s0,$s1for comparing adjacent elements.
- Printing the List Before Sorting:
- The program prints the “Before swap” message and then iterates through the list to print each number separated by spaces.
- Bubble Sort Algorithm:
- The outer loop runs through the entire list, decreasing the range of the list to be sorted after each iteration (as the largest elements “bubble” to the end of the list).
- The inner loop compares adjacent elements and swaps them if they are out of order.
- If a swap is made, the
swaplabel is executed, which uses a temporary register$s5to store one of the elements during the swap. The swap flag ($t3) is set to 1 if any swap occurs.
- Printing the List After Sorting:
- After sorting, the program prints the “After swap” message and then iterates through the sorted list to print the numbers.
- Program Exit:
- The program finishes by calling the exit system call (
li $v0, 10).
Output:
The program will print the list of numbers before and after sorting. The list will be sorted in ascending order using the bubble sort algorithm.