I can\'t seem to figure out why my sort function won\'t sort correctly (alphabet
ID: 3645498 • Letter: I
Question
I can't seem to figure out why my sort function won't sort correctly (alphabetically). Need some help/comments/suggestions. Thanks!**the program is supposed to take random letters from the input and sort them alphabetically and return them.
##############################################################################################################
# #
# #
##############################################################################################################
.data
insert:
.word 4
string_ask:
.asciiz " Please Enter Your Letters: " ##address = string_ask
string_return:
.asciiz " Result: " ##address = string_return
.text ##enables text input/output
main:
la $a0, string_ask
li $v0, 4
syscall
#-----------------------Read in String----------------------------------#
li $v0, 8 ##Read in String
move $s0, $a0 ##Copy to $s0
syscall
#-----------------------Sort the string---------------------------------#
move $v0, $s0 ##Copy string to $v0 to pass to sort function
jal sort ##Sort the string
move $s0, $v0 ##Save sorted string into $s0
#---------------------Print the sorted string---------------------------#
la $a0,string_return ##
li $v0, 4
syscall
move $a0, $s0
li $v0, 4
syscall
li $v0, 10 ## return from main routine
syscall
.end main
#####################################~END MAIN~####################################################
#----------------------Bubble Sort---------------------------------------#
sort:
move $t9, $v0 ## t9=Address of string
lw $t1,insert ## $t1 = insert
li $t0, 0 ## $t0 = 0 (i)
for1:
beq $t1, $t0, exit ## if $t0 == insert, exit
addi $t4, $t0, -1 ## $t4(J) = counter-1
for2:
bltz $t4, exit2 ## j < 0
mul $t5, $t4, 2 ## $t5 = 2 * j
add $t5, $t9, $t5 ## $t5 = &a[j]
lb $t2, 0($t5) ## $t2 = a[j]
lb $t3, 1($t5) ## $t3 = a[j+1]
ble $t3, $t2, else ## a[j+1] >= a[j]
j swap
else:
addi $t4, -1 ## --j
j for2
exit: move $v0, $t9 ##Return $t9 (sorted string)
jr $ra
exit2:
addi $t0, 1 ## ++i
j for1
swap: sb $t3, 0($t5)
sb $t2, 2($t5)
j for2
#########################################################################################################
Explanation / Answer
mul $t5, $t4, 2 ## $t5 = 2 * j add $t5, $t9, $t5 ## $t5 = &a[j] How is $t5 = &a[j] ?? shouldnt it be $t5 = &a[2j] (since $t5 = 2j in the previous step)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.