In this project, you will write a program to sort an array using bubble sort. Your code should provide the MIPS assembly code for the following C code. Be sure and use the exact same labels created in the C Program. In this project, you will need all of the tools we’ve already discussed including jumps/branches, syscalls, and memory access. #include int main() { int numArray [] = {5, 3, 12, 10, 4}; int outIndex, inIndex, index, hold; int size = 5; for(index = 0; index < size; index++) { printf(“%3d”, numArray[index]); } for(outIndex = 0; outIndex < size – 1; outIndex++) { for(inIndex = 0; inIndex < size – outIndex – 1; inIndex++) { if(numArray[inIndex] > numArray[inIndex + 1]) { hold = numArray[inIndex]; numArray[inIndex] = numArray[inIndex + 1]; numArray[inIndex + 1] = hold; } } } printf(“\n”); for(index = 0; index < size; index++) { printf(“%3d”, numArray[index]); } printf(“\n”); return 0; } 1 NOTE* Your program should work with different array values and more/less values
The Correct Answer and Explanation is:
✅ MIPS Assembly Code for Bubble Sort
Below is the MIPS assembly translation of the given C program:
assemblyCopyEdit.data
numArray: .word 5, 3, 12, 10, 4 # The unsorted array
size: .word 5 # Size of array
newline: .asciiz "\n" # Newline for output
.text
.globl main
main:
# Load size into $t0
la $t1, size
lw $t0, 0($t1) # $t0 = size
# Print original array
li $t2, 0 # index = 0
print_loop:
bge $t2, $t0, sort_outer
la $t3, numArray
mul $t4, $t2, 4
add $t4, $t4, $t3 # address of numArray[index]
lw $a0, 0($t4)
li $v0, 1 # syscall to print int
syscall
li $a0, 32 # print space
li $v0, 11
syscall
addi $t2, $t2, 1
j print_loop
# Bubble Sort Outer Loop: outIndex = 0 to size - 2
sort_outer:
li $t5, 0 # outIndex
outer_loop:
sub $t6, $t0, $t5 # t6 = size - outIndex
addi $t6, $t6, -1 # t6 = size - outIndex - 1
li $t7, 0 # inIndex
inner_loop:
bge $t7, $t6, outer_next
la $t8, numArray
mul $t9, $t7, 4
add $t9, $t9, $t8 # address of numArray[inIndex]
lw $s0, 0($t9) # numArray[inIndex]
addi $t9, $t9, 4 # address of numArray[inIndex + 1]
lw $s1, 0($t9) # numArray[inIndex + 1]
ble $s0, $s1, skip_swap
# Swap
sw $s1, -4($t9) # numArray[inIndex] = numArray[inIndex+1]
sw $s0, 0($t9) # numArray[inIndex+1] = hold
skip_swap:
addi $t7, $t7, 1
j inner_loop
outer_next:
addi $t5, $t5, 1
sub $t6, $t0, 1
blt $t5, $t6, outer_loop
# Print sorted array
li $t2, 0
li $v0, 4
la $a0, newline
syscall
print_sorted:
bge $t2, $t0, end_program
la $t3, numArray
mul $t4, $t2, 4
add $t4, $t4, $t3
lw $a0, 0($t4)
li $v0, 1
syscall
li $a0, 32
li $v0, 11
syscall
addi $t2, $t2, 1
j print_sorted
end_program:
li $v0, 4
la $a0, newline
syscall
li $v0, 0
jr $ra
📘 Explanation (Like Textbooks)
This MIPS assembly program translates a bubble sort algorithm implemented in C. It starts by printing the original unsorted array, sorts it using the bubble sort algorithm, and prints the sorted array.
- Data Section:
It defines the arraynumArrayand itssize, as well as a string for newline formatting. - Printing Array:
A loop uses syscalls to display each integer followed by a space. The index is multiplied by 4 (since each word = 4 bytes) to access the correct array element. - Bubble Sort Algorithm:
Two nested loops implement the bubble sort. The outer loop runs(size - 1)times, and the inner loop compares adjacent elements. If the left element is greater than the right, a swap is performed using temporary registers$s0and$s1. - Swapping:
The values are exchanged using simplesw(store word) andlw(load word) instructions with address calculations. - Final Output:
After sorting, the sorted array is printed in the same way as the initial print.
This MIPS program accurately demonstrates how high-level constructs like loops, arrays, conditionals, and I/O translate into low-level assembly operations while maintaining program logic integrity.
