Basically I am trying to write a quicksort method in one of my programs in Assembly and its turning out to be a real nightmare. Anyone have any suggestions. I think that I am going wrong when I am allocating loading the stacks, although the partition method isn't running smoothly either.
most of the .data that I will post will not be relevant to the code, because it pertains to other parts of the program.
.data
.align 2
single: .space 4
charactr: .space 104
initspce: .space 400 ############################
donearry: .space 400 # allocate space for arrays
table: .space 104 ############################
exitMsg: .asciiz "
Thank you, goodbye!
"
instruct: .asciiz "Enter a sentence less than 100 characters:
"
endprob1: .asciiz "
This is the array without spaces or special characters:
"
dash: .asciiz "-"
space: .asciiz " "
newLn: .asciiz "
"
freq: .asciiz "
Frequencies of each letter alphabetically:
"
.text
part3: la $s0,table
#left bound of the array
addi $s1,$s0,25
#right bound of the array
#quicksort: beq $s0,$s1,printpl
# blt $s1,$s0,recurse
# j partition
#partref: addi $sp,$sp,-8
# sw $s1,4($sp)
# sw $s3,0($sp)
# addi $s1,$s3,-1
# jal quicksort
#this: addi $s0,$s0,1
# j quicksort
#recurse: lw $s0,0($sp)
# lw $s1,4($sp)
# addi $sp,$sp,8
# jr $ra
partition: addi $s3,$s0,-1
#left bound minus one (i on algorithm)
addi $s4,$s1,0
#right bound of the array (j on algorithm)
lb $s5,0($s1)
#pivot value (v on algorithm)
while1: addi $s3,$s3,1 # this is the first while loop. if the
lb $t0,0($s3) # leftmost value is less than the pivot
blt $t0,$s5,while2 # go to loop 2. if not go to next value
beq $s5,$zero,while2 #
j while1 # to the right and so forth
while2: addi $s4,$s4,-1 # this is the second while loop. if the
lb $t1,0($s4) # rightmost value is greater than or equal
blt $s5,$t1,point # to the pivot, go to point. also if you
beq $s5,$t1,point # get to the beginning of the array go to
beq $s4,$s0,point # point. If its not greater, go to next
j while2 # value to the left and so forth
point: blt $s4,$s3,breakpt # if your indices are equal or they have
beq $s4,$s3,breakpt # crossed then go to breakpt
sb $t1,0($s3) #
sb $t0,0($s4) # else, switch the two indices and go back
j partition # to partition (infinite loop until it breaks)
breakpt: lb $t2,0($s3) # here the indices are equal or have crossed
lb $t3,0($s1) # so switch the pivot with the value that i
sb $t2,0($s1) # was on. everything left of the pivot is
sb $t3,0($s3) # greater or equal, and everything right is less
# j partref
printpl: la $a0,newLn
li $v0,4
syscall
la $s2,table
addi $t5,$zero,25
addi $t6,$zero,0
back: lb $a0,0($s2)
li $v0,1
syscall
addi $s2,$s2,1
addi $t6,$t6,1
beq $t6,$t5,exit
j back
exit: la $a0,exitMsg ##########################################
li $v0,4 # Print out an exit message and then
syscall # exit the program
li $v0,10 #
syscall ##########################################
sorry about the aesthetics of the code. The copy and paste method isn't exactly nice. Any help would be greatly appreciated.
Comments
l8rz
: Basically I am trying to write a quicksort method in one of my programs in Assembly and its turning out to be a real nightmare. Anyone have any suggestions. I think that I am going wrong when I am allocating loading the stacks, although the partition method isn't running smoothly either.
: most of the .data that I will post will not be relevant to the code, because it pertains to other parts of the program.
:
: .data
: .align 2
: single: .space 4
: charactr: .space 104
: initspce: .space 400 ############################
: donearry: .space 400 # allocate space for arrays
: table: .space 104 ############################
: exitMsg: .asciiz "
Thank you, goodbye!
"
: instruct: .asciiz "Enter a sentence less than 100 characters:
"
: endprob1: .asciiz "
This is the array without spaces or special characters:
"
: dash: .asciiz "-"
: space: .asciiz " "
: newLn: .asciiz "
"
: freq: .asciiz "
Frequencies of each letter alphabetically:
"
:
: .text
:
:
: part3: la $s0,table #left bound of the array
: addi $s1,$s0,25 #right bound of the array
: #quicksort: beq $s0,$s1,printpl
: # blt $s1,$s0,recurse
: # j partition
: #partref: addi $sp,$sp,-8
: # sw $s1,4($sp)
: # sw $s3,0($sp)
: # addi $s1,$s3,-1
: # jal quicksort
: #this: addi $s0,$s0,1
: # j quicksort
: #recurse: lw $s0,0($sp)
: # lw $s1,4($sp)
: # addi $sp,$sp,8
: # jr $ra
:
: partition: addi $s3,$s0,-1 #left bound minus one (i on algorithm)
: addi $s4,$s1,0 #right bound of the array (j on algorithm)
: lb $s5,0($s1) #pivot value (v on algorithm)
:
: while1: addi $s3,$s3,1 # this is the first while loop. if the
: lb $t0,0($s3) # leftmost value is less than the pivot
: blt $t0,$s5,while2 # go to loop 2. if not go to next value
: beq $s5,$zero,while2 #
: j while1 # to the right and so forth
:
: while2: addi $s4,$s4,-1 # this is the second while loop. if the
: lb $t1,0($s4) # rightmost value is greater than or equal
: blt $s5,$t1,point # to the pivot, go to point. also if you
: beq $s5,$t1,point # get to the beginning of the array go to
: beq $s4,$s0,point # point. If its not greater, go to next
: j while2 # value to the left and so forth
:
: point: blt $s4,$s3,breakpt # if your indices are equal or they have
: beq $s4,$s3,breakpt # crossed then go to breakpt
: sb $t1,0($s3) #
: sb $t0,0($s4) # else, switch the two indices and go back
: j partition # to partition (infinite loop until it breaks)
:
: breakpt: lb $t2,0($s3) # here the indices are equal or have crossed
: lb $t3,0($s1) # so switch the pivot with the value that i
: sb $t2,0($s1) # was on. everything left of the pivot is
: sb $t3,0($s3) # greater or equal, and everything right is less
: # j partref
:
: printpl: la $a0,newLn
: li $v0,4
: syscall
: la $s2,table
: addi $t5,$zero,25
: addi $t6,$zero,0
: back: lb $a0,0($s2)
: li $v0,1
: syscall
: addi $s2,$s2,1
: addi $t6,$t6,1
: beq $t6,$t5,exit
: j back
:
:
:
:
: exit: la $a0,exitMsg ##########################################
: li $v0,4 # Print out an exit message and then
: syscall # exit the program
: li $v0,10 #
: syscall ##########################################
:
:
: sorry about the aesthetics of the code. The copy and paste method isn't exactly nice. Any help would be greatly appreciated.