i have problems to trying to add a get the median functionality in my MIPS program - debugging

i write the following MIPS program that take 10 integers from user, then output (min value, max value, and average), then i added instructions to get the median(starting from line 72, from the comment # Calculate median), every time i run the program after entering the 10 numbers i get the following exception (Runtime exception at 0x004000e0: fetch address not aligned on word boundary 0x10010005).
.data
array: .space 40 #10 X 4(byte)
prompt: .asciiz "Please enter 10 random numbers :\n"
sort: .asciiz "\nThe descending order of the numbers are :\n"
sum: .asciiz "\n\nThe Sum of all numbers are : "
average: .asciiz "\n\nThe average of all the number is : "
max: .asciiz "\n\nThe maximum number is : "
min: .asciiz "\n\nThe minimum number is : "
space: .asciiz " "
dot: .asciiz "."
median: .asciiz "\n\nThe median of the numbers is: "
.text
.globl main
main:
li $v0, 4
la $a0, prompt #print out prompt
syscall
la $s0, array #load array into register s0
LoopInput:
li $v0, 5 #input numbers
syscall
sw $v0, 0($s0) # $v0 = i
addi $s0, $s0,4 #move array position
addi $t0, $t0,1 #loop array where i++
bne $t0, 10,LoopInput #for(t1=0;t1!=10;t1++) g
la $s0, array
sub $t0, $t0,$t0
Sorting: #sorting number from big to small
beq $t1,9, ConSort #if t1=9, go to conSort
lw $s1, 0($s0) #load the number in the first location into $s1
lw $s2, 4($s0) #load the number in the second location into $s2
addi $s0, $s0, 4 #update array position
addi $t1, $t1, 1 #move array position #for(s1=0;s1!=9;s1++)
bge $s1, $s2, Sorting #if s1>s2, back to sorting
sw $s1, 0($s0) #exchange position
sw $s2, -4($s0)
bne $t1, 9, Sorting #if t1!=9, go back to sorting
ConSort:
la $s0, array #continue sort with the same way
addi $t0, $t0, 1 #keep sorting the other numbers
addi $t1, $t0, 0
bne $t0, 9, Sorting #if t0!=9, go back to sorting
li $v0, 4 #print out prompt
la $a0, sort
syscall
la $s0, array
sub $t0, $t0, $t0
PrintSort:
li $v0, 1 #print sorting
lw $a0, 0($s0)
syscall
li $v0, 4
la $a0, space #print out prompt
syscall
addi $s0, $s0, 4
addi $t0, $t0, 1
bne $t0, 10, PrintSort #if t0!=10, go to printsort
# Calculate median
li $t4, 5 # t4 = 5
la $s0, array # s0 points to the beginning of the array
add $s0, $s0, $t4 # s0 points to the middle element of the array
lw $t5, 0($s0) # t5 = array[5]
li $v0, 4 # print message
la $a0, median # message to print: "The median of the numbers is: "
syscall
move $a0, $t5 # print median value
li $v0, 1 # print integer
syscall
sub $t0, $t0, $t0
sub $t1, $t1, $t1
sub $t2, $t2, $t2
sub $t3, $t3, $t3
la $s0, array #load array number to $s0
li $v0, 4 #print out prompt
la $a0, sum
syscall
SumA:
lw $t2, 0($s0) #load first $t2 to $s0
addi $s0, $s0,4 #update the array[s0] position
add $t3, $t2, $t3 #t3=t2+t3
addi $t0, $t0, 1 #t0= i++
bne $t0, 10, SumA #if t0!=10, go back to SumAll
move $a0, $t3 #move $t3 to $a0
li $v0, 1 #print out the sum of all numbers
syscall
li $v0, 4
la $a0, average #print out prompt
syscall
addi $t6, $t6, 10 #t6=10
div $t3, $t6 #t3=t3/10
mflo $s5 #move quotient to s5
mfhi $s3 #move remainder to s3
move $a0, $s5 #move s5 to a0
li $v0, 1 #print out quotient
syscall
li $v0, 4
la $a0, dot #print out dot
syscall
move $a0, $s3 #move s3to a0
li $v0, 1 #print out remainder
syscall
la $s0, array #load array into s0
li $v0, 4 #print out prompt
la $a0, max
syscall
lw $t5, 0($s0) #load number in array into t5
move $a0, $t5 #move t5 to a0
li $v0, 1 #print out number
syscall
li $v0, 4 #print out prompt
la $a0, min
syscall
lw $t4 , 36($s0) #load number in array into t4
move $a0, $t4 #move t4 to a0
li $v0, 1 #print out number
syscall
li $v0, 10
syscall #end of program
from my understating the problem caused by an attempt to access memory at an address that is not aligned on a word boundary.

Unlike C, assembly doesn't auto-scale your pointer arithmetic/array indexing. It seems you understand this, since you always use addi $s0,$s0,4 to advance to the next entry in your array, as an int on the 32-bit MIPS hardware takes up 4 bytes. The only place you made an error was here:
li $t4, 5 # t4 = 5
la $s0, array # s0 points to the beginning of the array
add $s0, $s0, $t4 # s0 points to the middle element of the array
Presumably, you want the middle value which is actually going to require you to use li $t4, 20. I'll use a chart below to explain this. I'll assume that your array is located at memory address 0x00000000 (it isn't, but it's just to make the demonstration a bit easier.) If, for example, the user inputs the numbers 2,3,4,5,6,7,8,9,10, and 11, this is what your array would look like in memory after being sorted (assuming a little-endian CPU)
0x00000000: 02 00 00 00
0x00000004: 03 00 00 00
0x00000008: 04 00 00 00
0x0000000C: 05 00 00 00
0x00000010: 06 00 00 00
0x00000014: 07 00 00 00 (this is what you will get when you load from offset 20)
0x00000018: 08 00 00 00
0x0000001C: 09 00 00 00
0x00000020: 0A 00 00 00 (ten)
0x00000024: 0B 00 00 00 (eleven)
Now since there are an odd number of values, you're going to have to average the two in the center. So you'll load from 0($s0) and -4($s0), add them, and bit shift right once.

Related

Runtime exception at 0x0040002c: address out of range 0x00000001 in MIPS Assembler for ceasar encryption

The Programm compiles just fine but as soon as i give input this error is thrown:
"Error in line 28: Runtime exception at 0x0040002c: address out of range 0x00000001
Processing terminated due to errors."
Here ist the code for my programm:
.data
line: .space 80
.text
main:
# read line from stdin
li $v0, 8
la $a0, line
li $a1, 80
syscall
# call caesar_line
li $a0, 1
jal caesar_line
# return 0
li $v0, 0
jr $ra
caesar_line:
# save start of line
move $t0, $a0
# loop through line
loop:
# load the character from memory
move $t1, $a0
lb $a0, 0($t1)
beqz $a0, end_loop
jal caesar_char
sb $v0, 0($t1)
addi $t1, $t1, 1
move $a0, $t1
j loop
end_loop:
# print line
li $v0, 4
move $a0, $t0
syscall
jr $ra
caesar_char:
# check if c is lowercase
blt $a0, 'a', upper
bgt $a0, 'z', not_alpha
addi $a1, $a1, 'a'
jal caesar_helper
j done
upper:
# check if c is uppercase
blt $a0, 'A', not_alpha
bgt $a0, 'Z', not_alpha
addi $a1, $a1, 'A'
jal caesar_helper
j done
not_alpha:
# not an alphabetical character
move $v0, $a0
j done
caesar_helper:
# subtract base from c
sub $t0, $a0, $a2 # $t0 = c - base
# add distance + 26 to c
addi $t0, $t0, 26 # $t0 += 26
add $t0, $t0, $a1 # $t0 += distance
# calculate c % 26
li $t1, 26 # $t1 = 26
div $t0, $t1 , $t1 # $lo = c % 26
mflo $t0
# add base to c
add $v0, $t0, $a2 # $v0 = c + base
done:
jr $ra
Not even ChatGBT could find an error.
The code should shift all chars of an string by a certain amount, but as soon as it is run the error above is thrown.

comparing a string in MIPS

Could someone tell me what is wrong with my code so far. I am trying to make a program that takes input from the user (roman numerals) and then converts it to integers. So far this is what I have:
.data
buffer: .space 20
onlyCaps: .asciiz "Please enter only Capital Numbers\n"
enter1: .asciiz "Number out of range. Please enter another number\n"
enter2: .asciiz "Please enter your roman numeral: "
debug: .asciiz "reach"
M: .asciiz "1000"
D: .asciiz "500"
C: .asciiz "100"
L: .asciiz "50"
X: .asciiz "10"
V: .asciiz "5"
I: .asciiz "1"
.text
main:
la $a0, onlyCaps # "Enter Only Capital numbers"
li $v0, 4
syscall
la $a0, enter2 #prompt user with "Please enter a roman numeral:"
li $v0, 4
syscall
la $a0, buffer #load byte space into address
li $a1, 3 # allot the byte space for string
li $v0, 8 #read user input, j
syscall
li $t4, 1
loop:
lb $t0, 0($a0) # Save $v0 value to $t0
beqz $t4, done # if it is equal to zero end the loop
add $a0, $a0, $t4 #increment the address
j mCheck
#while loop ends here
mCheck:
beq $t0, 'M', mChar # if $t0 equal M go to mChar
bne $t0, $t1, dCheck # move to see if equals D
dCheck:
beq $t0, 'D', dChar # if $t0 equal D go to dChar
bne $t0, $t1, cCheck
cCheck:
beq $t0, 'C', cChar # if $t0 equal C go to cChar
bne $t0, $t1, lCheck
lCheck:
beq $t0, 'L', lChar # if $t0 equal L go to lChar
bne $t0, $t1, xCheck
xCheck:
beq $t0, 'X', xChar # if $t0 equal X go to xChar
bne $t0, $t1, vCheck
vCheck:
beq $t0, 'V', vChar # if $t0 equal V go to vChar
bne $t0, $t1, iCheck
iCheck:
beq $t0, 'I', iChar # if $t0 equal I go to iChar
bne $t0, $t1, error
mChar:
la $a0, M #puts one hundred in $t3
li $v0, 4
syscall
j loop
dChar:
la $a0, D # prints out 500
li $v0, 4
syscall
j loop
cChar:
la $a0, C # prints out 500
li $v0, 4
syscall
j loop
lChar:
la $a0, L # prints out 500
li $v0, 4
syscall
j loop
xChar:
la $a0, X # prints out 500
li $v0, 4
syscall
j loop
vChar:
la $a0, V # prints out 500
li $v0, 4
syscall
j loop
iChar:
la $a0, I # prints out 500
li $v0, 4
syscall
j loop
error:
la $a0, enter1 # Print error meg. then back to main
li $v0, 4
syscall
j done
done:
li $v0, 10 # Exit
syscall
My question is how can you take input from the user and put it in $a0, and then use a while loop after that? I can't get the bytes to go to the next spot (i.e. MXX to go from M to X.) currently the program reads M, but not X. Eventually I will switch the program to sum these numbers and to also check to see if the number before is less (i.e. IV) to account for those roman numerals but I need help with the while loop first.

Sorting program with MIPS

I have some problem when I'm writing a sorting program in MIPS.
The function of this program is like this: Input 10 integers from keyboard and print them out, then sort them and print the sorted array again.
But the output is different from what I expect, some numbers are not sorted, would someone help me?
.data
str1: .asciiz "Please input integer numbers, maximum 10: \n"
str2: .asciiz "The 10 integer numbers you input are: \n"
str3: .asciiz "The numbers you entered are sorted as: \n"
array: .space 40
space: .asciiz " "
.text
.globl main
main:
la $t6, array #load the address of array into $t6
move $t7, $t6
addi $t7, $t7, 40 #point $t7 to the end of the array
jal readin
la $t6, array
la $a0, str2
li $v0, 4
syscall
jal print1 #print out the array before sort
la $t0, array #put the address of array into $t0
add $t0, $t0, 40 #put $t0 to the end of the array
move $t1, $zero #set $t1 as counter of the outerloop
li $t2, 10 #set $t2 as number of the outerloop
la $t3, array #put the address to $t3
jal innerloop
la $a0, str3
li $v0, 4
syscall
la $t3, array
#move $t7, $t3
#add $t7, $t7, 40
jal print2
li $v0, 10
syscall
innerloop:
lb $t4, 0($t3)
lb $t5, 4($t3)
bgt $t4, $t5, swap
addi $t3, $t3, 4
blt $t3, $t0, innerloop #if $t3 < $t0, keep in the innerloop to check two #neighbor numbers
jal outerloop #jump to outerloop if one round is finished
swap:
sb $t4, 4($t3)
sb $t5, 0($t3)
addi $t3, $t3, 4
#sll $t3, $t3, 2
blt $t3, $t0, innerloop #
jr $ra
outerloop:
la $t3, array
addi $t1, $t1, 1 #add the outerloop counter $t1 by one,
addi $t0, $t0, -1
blt $t1, $t2, innerloop #if $t1 < $t2, keep searching
jr $ra
readin:
la $a0, str1 #print str1
li $v0, 4
syscall
li $v0, 5 #read in the number
syscall
sb $v0, ($t6) #store the number in the array
add $t6, $t6, 4
blt $t6, $t7, readin
jr $ra
print1:
lb $a0, ($t6)
li $v0, 1
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
add $t6, $t6, 4
blt $t6, $t7, print1
jr $ra
print2:
lb $a0, ($t3)
li $v0, 1
syscall
li $a0, 32
li $v0, 11 # syscall number for printing character
syscall
add $t3, $t3, 4
blt $t3, $t0, print2
jr $ra
And the result in SPIM is like this:
Please input integer numbers, maximum 10:
44
Please input integer numbers, maximum 10:
11
Please input integer numbers, maximum 10:
33
Please input integer numbers, maximum 10:
22
Please input integer numbers, maximum 10:
66
Please input integer numbers, maximum 10:
55
Please input integer numbers, maximum 10:
99
Please input integer numbers, maximum 10:
77
Please input integer numbers, maximum 10:
88
Please input integer numbers, maximum 10:
24
The 10 integer numbers you input are:
44 11 33 22 66 55 99 77 88 24 The numbers you entered are sorted as:
11 33 22 44 55 66 77 88 24 32
Would someone please explain to me why there is a 32 in the end and why they are not sorted properly?

Case sorting in mips

I have a program that will capitalize all lowercase letters and lowercase all the uppercase letters entered in a string by the user. It does this by adding or subtracting 32 from the character value to get the desired character. My problem is that it doesn't change anything in the string. Any suggestions on what to change?
.data
prompt: .asciiz "\n\nEnter an string of characters: "
result: .asciiz "\n\nHere is the string you entered: "
after_sort: .asciiz "\n\nHere is the string after the case sorting: "
buffer: .space 80
.text
main:
#Prints the prompt string
li $v0, 4
la $a0, prompt
syscall
#reads string from user and saves in $a0
li $v0, 8
la $a0, buffer
li $a1, 80
syscall
#Prints the result string
li $v0, 4
la $a0, result
syscall
#Prints the string entered by the user
la $a0, buffer
li $v0, 4
syscall
li $t0, 0 # t0 = i = 0
for_loop:
slti $t1, $t0, 80 # t1 = 1 if and only if t0 < 80
beq $t1, $0, for_loop_done
slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower
upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
addi $t0, $t0, 1
j for_loop
for_loop_done:
#Prints the result string
li $v0, 4
la $a0, after_sort
syscall
#Prints the string entered by the user
la $a0, buffer
li $v0, 4
syscall
exitProgram: li $v0, 10 # system call to
syscall # terminate program
You are using $a0as a character, such as here:
slti $t2, $a0, 91
but it is never filled with the character. At the moment, it contains a memory address, not a character.
You should load the character using lb and store it back after making it upper/lowercase using sb.
Feel free to add a comment if you want a code example.
Edit: the changes in the relevant part of the code:
...
li $t0, 0 # t0 = i = 0
for_loop:
slti $t1, $t0, 80 # t1 = 1 if and only if t0 < 80
beq $t1, $0, for_loop_done
lb $t4, 0($a0)
beqz $t4, for_loop_done
beq $t4, 10, for_loop_done
slti $t2, $t4, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower
upper:
addi $t4, $t4, 32 #adds 32 to the character value to lowercase it
j done
lower:
addi $t4, $t4, -32 #subtracts 32 from the character value to capitalize it
done:
addi $t0, $t0, 1
sb $t4, 0($a0)
addi $a0, $a0, 1
j for_loop
for_loop_done:
#Prints the result string
...
It's easy to forget that in assembly, you can't do this:
if something
do this
else
do that
There is no "else", only shudder Goto.
So in this code:
slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
bne $t2, $t3, lower
upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
addi $t0, $t0, 1
When you branch to upper, it adds 32. Then it subtracts 32, because execution progressed to the next line. So your code capitalizes lower case, but does nothing to uppercase.
You need to add a jump to the first instruction after your if/then/else equivalent:
upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
j done # No, I don't want to subtract it again!
lower:
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
done:
addi $t0, $t0, 1
In fact, you should probably get rid of the bne altogether - it's redundant. If beq doesn't branch, then it's not equal. So this would be the finished product:
slti $t2, $a0, 91
li $t3, 1
beq $t2, $t3, upper #if the character value is less than 91 branch to upper addition
# Otherwise, it's lower
subi $a0, $a0, 32 #subtracts 32 from the character value to capitalize it
j done
upper:
addi $a0, $a0, 32 #adds 32 to the character value to lowercase it
done:
addi $t0, $t0, 1
Hope that helps!
(Edit: #Patrik is right too, you need to "dereference" $a0. My example doesn't take that into account.)
here is my code:
it works perfectly
.data
theString:
.space 20
prompt: .asciiz "Enter a string of characters: "
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 8
la $a0, theString
li $a1, 20
syscall
li $v0, 4
syscall
la $t1,theString
for: lb $a0, 0($t1)
beqz $a0,out #to find out end of string
beq $a0,10,out #to find out end of string
slti $t2, $a0,91 #if $a0<91 $t2=1
beq $t2,1,small
beq $t2,0,capital
capital:
subu $a0, $a0, 32
li $v0,11
syscall
addi $t1,$t1,1
j for
small:
addi $a0, $a0, 32
li $v0,11
syscall
addi $t1,$t1,1
j for
out:
li $v0, 10
syscall

Bubble Sort in MIPS

I am trying to sort an array of integers in MIPS using bubble sort but every time that I run bubble sort I get an address out of range error. I have been staring at the code for hours and have no idea why this is happening. Hopefully' it is something really obvious that someone with more experience can see and help me fix.
The point of the program is simply to read in integers and symbols (price of stock, and stock symbol) and then sort those based on the prices. Here is the code:
.data
welcome: .asciiz "Welcome!\n"
prompt1: .asciiz "Enter how many stocks you have.\n >"
prompt2: .asciiz "Enter the four character NASDAQ abbrevation and price for each stock.\n"
prompt3p1: .asciiz "You have entered "
prompt3p2: .asciiz " stock abbreviations and prices. How many stocks do you want to buy:\n"
prompt4: .asciiz "How many stocks do you want to sell:\n"
InfoPrompt: .asciiz "Here are the symbols and the corresponding numbers that were entered: \n"
numStocks: .word 0
stockPrice: .space 40 # this will be the list of the stock prices
stockSymbol: .word 0:20 # this will be for the lit of stock abbrevs
symbols: .space 12 #this should be used for a 40 character array
inputFormat: .asciiz "\n> "
length: .word 0
buffer: .space 4
.globl main
.text
main: # display all of the propts
li $v0, 4 # get ready to print welcome
la $a0, welcome
syscall # print welcome
li $v0, 4 # not sure this is necessary since it was already loaded into v0 before
la $a0, prompt1
syscall # print("enter how many stocks you have.")
li $v0, 5 # this will get ready to accept the number of stocks
syscall # this should store the number into the $v0 register
sw $v0, numStocks # store in memory so we dont lose it
move $t0, $v0 # this will hold the num of stocks
#before we go into the procedure we need to declare and move the stack for the $t registers.
la $t1, symbols
la $t2, stockPrice
sub $sp, $sp, 8
sw $t1, 0($sp)
sw $t2, 4($sp)
jal getInfo
# now that we have all the information lets run a test to see how successful we were
# jal printInfo
li $v0, 4 #get ready to ask how many items you want to buy
la $a0, prompt3p1
syscall
li $v0, 1
lw $a0, numStocks # prints the number of stocks in the portfolio
syscall
li $v0, 4
la $a0, prompt3p2
syscall
# we need to now get the number of stocks the person wants to buy
li $v0, 4
la $a0, inputFormat
syscall #format the next input
li $v0, 5
syscall
move $t3, $v0 # the number of stocks we want to buy is now stored as $t3
#we need to get how many they want to buy
li $v0, 4
la $a0, prompt4
syscall
li $v0, 4
la $a0, inputFormat
syscall #format the next input
li $v0, 5
syscall
move $t4, $v0 # the number of stocks that you want to sell
# now we have to sort the list to figure out what elements we are going to sell and buy
la $a2, stockPrice
la $a0, stockPrice
la $a1, numStocks
jal buble_sort
jal printInfo
# end program
li $v0,10 #load the syscall number for terminating
syscall #terminate
####################################################################
# This will iterate for the number of stocks
# Only accepts the number of stocks
####################################################################
getInfo:
sub $sp, $sp, 8
sw $ra, 0($sp) # store the return value
sw $t0, 4($sp) # Save the t registers that we will be using
li $v0, 4 # set up the first call to initialize the calls for the abbreviations and numbers
la $a0, prompt2
syscall
# we want to have a place to store the symbols
la $t1, symbols
la $t2, stockPrice
GI_loop:
beq $t0, $zero, GI_loop_done # if the counter == 0 then we are done
li $v0, 4
la $a0, inputFormat
syscall #format the next input
# ask for the string input
li $v0, 8
la $a0, 0($t1)
li $a1, 6
syscall
#store the value in the array
addi $t1, $t1, 6 #increment our "array"
# ask for the integer input
li $v0, 4
la $a0, inputFormat
syscall #format the next input
li $v0, 5
syscall # get the integer value that we require
sw $v0, 0($t2) #store the value
addi $t2, $t2,4 #increment our counter
addi $t0, $t0, -1 # decrement our counter
j GI_loop
GI_loop_done:
lw $ra, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
lw $t2, 12($sp)
add $sp, $sp, 8
jr $ra
####################################################################
# This will go through the lists and print out what was stored
# will go through the symbols then the numbers
####################################################################
printInfo:
sub $sp, $sp, 12
sw $ra, 0($sp)
sw $t0, 4($sp) # this will store the number of stocks that were entered
sw $t1, 8($sp)
li $v0, 4
la $a0, InfoPrompt
syscall
# we know that $t0 stores the correct number that was originally enetered so we need to loop through and print all the integers
InfoLoop:
beq $t0, $zero, InfoLoopDone # a basic counter check
#li $v0, 4
#la $a0, 0($t1)
#syscall
#addi $t1, $t1, 6
#addi $t0, $t0, -1
#j InfoLoop
################################### INTEGER PRINT WORKING
li $v0, 1 # this will print out the integers
lw $a0, 0($t2) # we have to load the world that is found in the address of $t2
syscall
addi $t2,$t2, 4 # this will increment the array
addi $t0, $t0, -1 ## this will fix our counter
j InfoLoop
InfoLoopDone:
lw $ra, 0($sp)
lw $t0, 4($sp)
add $sp, $sp, 8
jr $ra
################################
# BUBBLE SORT
################################
buble_sort:
#a0=address of table
#a1=sizeof table
add $t0,$zero,$zero #counter1( i )=0
loop1:
addi $t0,$t0,1 #i++
bgt $t0,$a1,endloop1 #if t0 > a1 break;
add $t1,$a1,$zero #counter2=size=6
loop2:
bge $t0,$t1,loop1 #j < = i
#slt $t3,$t1,$t0
#bne $t3,$zero,loop1
addi $t1,$t1,-1 #j--
mul $t4,$t1,4 #t4+a0=table[j]
addi $t3,$t4,-4 #t3+a0=table[j-1]
add $t7,$t4,$a2 #t7=table[j]
add $t8,$t3,$a2 #t8=table[j-1]
lw $t5,0($t7)
lw $t6,0($t8)
bgt $t5,$t6,loop2
#switch t5,t6
sw $t5,0($t8)
sw $t6,0($t7)
j loop2
endloop1:
jr $ra
Your problem is in the statement before calling your bubble sort routine:
la $a1, numStocks
It will load the address where the number of stocks are saved, not the number of stocks itself.
You should change it with
lw $a1, numStocks

Resources