so, I have a MIPS program trying to sort an array of ints by selection sorting. I'm attempting to test it before I print it back out, (so right after the ints are supposed to be stored back into the array) I get "Exception occurred at PC=0x004000e8" and then "Unaligned address in store: 0x00000001" and then the program crashes. If anyone would like to help me understand why, that would be awesome! My program so far is:
the largest amount of nums in the array can be 256, hence the .space 1024 .data prompt1: .asciiz "enter number of elements in the array "
prompt2: .asciiz "enter array elements, one per line:\n"
newline: .asciiz "\n"
.align 2
list: .space 1024
#i is $s0
#n is $s1
#address of list will be in $s2
#minpos will be in $s3
#j will be in $s4
#temp will be $t5
.text
main: la $s2, list
#promt the user for array size
la $a0, prompt1
li $v0, 4
syscall
#read int from console for n
li $v0, 5
syscall
move $s1, $v0
#prompt for elements
la $a0, prompt2
li $v0, 4
syscall
li $s0, 0
#loop $s1 times
for: #i<n:branch on i>=n
bge $s0, $s1, for_exit
sll $t0, $s0, 2
addu $t0, $t0, $s2
li $v0, 5
syscall
sw $v0, 0($t0)
addi $s0, $s0, 1
j for
for_exit:
li $s0, 0
#put n-1 in $t0
move $t0, $s1
addi $t0, $t0, -1
#find correct element
#i<n-1: branch on $s0 >= $t0
for2:
bge $s0, $t0, for_exit2
#look for correct element
move $s3, $s0 #minpos = i
addi $s4, $s0, 1 #j = i+1
#j<n: branch if $s4, >= $s1
innerfor:
bge $s4, $s1, innerfor_exit
#time to get list[j] andd list[min_pos]
move $t1, $s3 #t1 = minpos
move $t2, $s4 #t2 = j
sll $t1, $t1, 2 #minpos*2
sll $t2, $t2, 2 #j *2
add $t1, $t1, $s2 #(minpos*2) + array address
add $t2, $t2, $s2 #(j*2) + array address
lw $t8, 0($t1) #t1 = list[minpos]
lw $t9, 0($t2) #t2 = list[j]
#if(list[j] < list[minpos])
#branch if list[j] >= list[minpos]
if:
bge $t9, $t8, if_exit
move $s3, $s4
if_exit:
addi $s4, $s4, 1 #j++
j innerfor
innerfor_exit:
#t5(temp)gets list[i]
move $t4, $s0 #t4 = i
sll $t4, $t4, 2 #4i
add $t4, $t4, $s2 #t4 = list[i]
lw $t5, 0($t4) #temp = list[i]
sw $t8, 0($t4) #list[i] = list[minpos]
sw $t5, 0($t8) #list[minpos] = temp (= orig list[i])
addi $s0, $s0, 1
j for2
for_exit2:
la $a0, newline
li $v0, 4
syscall
li $s0, 0
#for i<n: branch if $s0>= $s1
I know I get a little wild with temps down near the bottom, but that's just because I was starting to stress. Thanks in advance!
User contributions licensed under CC BY-SA 3.0