I am working on a project that requires me to use MIPS to bubblesort an array. E
ID: 3672772 • Letter: I
Question
I am working on a project that requires me to use MIPS to bubblesort an array. Everything is working but I cannot figure out why it is not sorting correctly. Here is the code:
.data
WellcomeReadText:
.asciiz "Please enter integer numbers in the array: "
Array:
.space 20 #5 integer array: 4 bytes for each integer.
#WellcomePrintText: # Set a global variable for wellcome text
.asciiz "Print out array:"
WhiteSpace: # Set a global variable for white space
.asciiz " "
prompt: .asciiz "Please enter the number of elements "
prompt2: .asciiz "Unsorted array "
prompt3: .asciiz " sorted array "
prompt4: .asciiz " outerloop "
prompt5: .asciiz " inner loop "
prompt6: .asciiz " update "
.globl main
.text
main:
#print prompt
la $a0 prompt
li $v0 4
syscall
#get int
li $v0 5
syscall
#allocate space
sll $a0 $v0 2 #number of bytes now in $a0
li $v0 9
syscall
#address of space now in $v0
add $t8, $a0, $zero
###################INPUT READIN###################
li $v0, 4 #Let processor know we want to print out a string text (wellcome text)
la $a0, WellcomeReadText #load wellcometext address to $a0
syscall
#Execute "print"
la $t0, Array #load array base address to $t0
addi $t1, $zero, 0 #Setup array address offset to $t1
ReadLoop:
li $v0, 5 #Let processor know we want to user input an integer
syscall #Execute "read"
add $t2, $t1, $t0 #Calculate address for storing the input integer
sb $v0, 0($t2) #if we use "sw" here, system will check if the address is "word-aligned".
#The address is word-aligned should be like 0xFFF0010, 0xFFF0014, 0xFFF0018.
#However, random assign address by computer will not match this word=align policy.
#We use "sb" (save byte) would solve the problem.
addi $t1, $t1, 4 #Increase data offset by 4
blt $t1, $t8, ReadLoop #Compare if we have reached 5 input integers. If no, continue; if yes, stop
###################OUTPUT PRINT###################
li $v0, 4 #Let processor know we are going to print out a string text.
la $a0, prompt2 #Load string base address to $a0
syscall #Execute "print"
la $a0, WhiteSpace #Load the white space address to $a0
syscall #Execute "print"
la $t0, Array #Load Array address to $t0
addi $t1, $zero, 0 #Setup array address offset to $t1
PrintLoop:
add $t2, $t1, $t0 #Calculate address of the element in the Array
li $v0, 1 #Let processor know we want to print out an integer
lb $a0, 0($t2) #load byte (instead of load word) from address $t2
syscall #Execute "print"
li $v0, 4 #We want to print out a white space
la $a0, WhiteSpace #load white space address to $a0
syscall #Execute "print"
addi $t1, $t1, 4 #Increase array address offset by 4
blt $t1, $t8, PrintLoop #Compare if we reach 5 elements
#######Bubblesort#######
la $t0, Array # move address of the array into $t0
li $s0, 1 # boolean flag = false 0 = false 1 = true
li $t2, 0 # j = 0;
li $t1, 0 # i = 0;
add $s3, $t8, $zero # array length n
Outerloop:
beqz $s0, exit # exit if flag = false
li $s0, 0 # flag = false;
addi $t2, $t2, 1 # j++;
move $t1, $0 # i = 0;
sub $s2, $s3, $t2 # s2 = length - j
li $v0, 4 #Let processor know we are going to print out a string text.
la $a0, prompt4 #Load string base address to $a0
syscall
InnerLoop:
bge $t1, $s2, exitInnerLoop # if i>=s2, exit
lw $t1, 0($t0) # a0 = array[i]
lw $t2, 4($t0) # a1 = array[i+1]
ble $t1, $t2, update # if array[i]<=array[i+1] then forward i by 1
sw $t2, 0($t0) # a[i+1] = a[i]
sw $t1, 4($t0) # a[i] = a[i+1]
li $s0, 1 # flag = true;
li $v0, 4 #Let processor know we are going to print out a string text.
la $a0, prompt5 #Load string base address to $a0
syscall
update:
li $v0, 4 #Let processor know we are going to print out a string text.
la $a0, prompt6 #Load string base address to $a0
syscall
addi $t2, $t1, 4 # i++
sll $t3, $t1, 2 # t3 = i*4
add $t0, $t0, $t3 # point to next element -->
j InnerLoop
exitInnerLoop:
j Outerloop
exit:
####Print Sorted Array#######
li $v0, 4 #Let processor know we are going to print out a string text.
la $a0, prompt3 #Load string base address to $a0
syscall #Execute "print"
la $a0, WhiteSpace #Load the white space address to $a0
syscall #Execute "print"
la $t0, Array #Load Array address to $t0
addi $t1, $zero, 0 #Setup array address offset to $t1
PrintLoop2:
add $t2, $t1, $t0 #Calculate address of the element in the Array
li $v0, 1 #Let processor know we want to print out an integer
lb $a0, 0($t2) #load byte (instead of load word) from address $t2
syscall #Execute "print"
li $v0, 4 #We want to print out a white space
la $a0, WhiteSpace #load white space address to $a0
syscall #Execute "print"
addi $t1, $t1, 4 #Increase array address offset by 4
blt $t1, $t8, PrintLoop2 #Compare if we reach 5 elements
Explanation / Answer
Please find the link for source code below.
https://drive.google.com/file/d/0B9P4w87kevGhaE1xekRRbjJlYVE/view?usp=sharing
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.