MIPS, Get location of Binary Search and Linear Search, - algorithm

I'm writing a program in MIPS to get the target's location from the ascending array (1-10) using both linear search and binary search. Right now, I manage to complete binary algorithm with the correct output, but for the linear, some of the integers in the array result a incorrect output.
My output right now:
//Let's say the target to find = 2
1 //from binary search
-1 //from linear search, result -1 when can't find target
On the other hand, how do I keep the numbers of comparison of each algorithm? I know that linear case is O(n), basically how many times the loop run. But I'm not sure about binary search.
Please help me.
.data
array: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
length: .word 10
newline: .asciiz " \n"
.text
main:
la $a0, array # Load array into $a0
li $a1, 0 # Load 0 into a1
lw $a2, length # Load length into $a2
li $a3, 4 # Load item to search for into $a1
jal binarySearch
jal linear
move $a0, $v0 # Move $v0 into $a0
li $v0, 1 #print location of binary search
syscall #print
la $a0, newline
li $v0,4 #newline
syscall
move $a0, $v1
li $v0,1 #print location of target by linear search
syscall
la $a0, newline
li $v0,4 #new line
syscall
li $v0, 10
syscall
#length of the array: $a2
####################################
# Binary Search #
####################################
# Input: #
# $a0, the array of words #
# $a1, left index #
# $a2, right index #
# $a3, item to search for #
####################################
# Output: #
# $v0, index of item (-1 if failed)#
####################################
# Used Registers: #
# $t0, Midpoint #
# $t1, used in math #
####################################
binarySearch:
addi $sp, $sp, -4 # Lower stack pointer
sw $ra, 0($sp) # Store return address
blt $a2, $a1, binaryFailed # If r < l then we failed
# addi $t7, $t7, 1
add $t0, $a1, $0 # Move left into t0
sub $t1, $a2, $a1 # Right - Left
srl $t1, $t1, 1 # (Right - Left) / 2
add $t0, $t1, $t0 # left + (right - left) / 2 (MID)
sll $t1, $t0, 2 # Convert index into word format
add $t1, $t1, $a0 # Add offset into array
lw $t1, 0($t1) # Load array element into memory
beq $t1, $a3, binaryFound # The element was found
bgt $t1, $a3, binaryGreater # The element is greater than the pointer
addi $a1, $t0, 1 # Set left to mid+1
j binarySearch # Search with new variable
j binaryEnd
binaryFound:
add $v0, $t0, $0 # Move mid answer into memory
#move $s7, $t7
j binaryEnd # End
binaryGreater:
addi $a2, $t0, -1 # Set right to mid-1
jal binarySearch # Search with new right
j binaryEnd
binaryFailed:
li $v0, -1 # Load failed value
j binaryEnd # Load end
binaryEnd:
lw $ra, 0($sp) # Load the return
addi $sp, $sp, 4 # Raise stack poiner
jr $ra # Return
####################################
# End Binary Search #
####################################
###################################
#. Linear Search #
###################################
linear:
li $s4, 0 #index i = 0
j linearLoop
linearLoop:
bge $s4, $a2, linearFailed #if $t0 > target, it jumps out of loop
lw $t1, 0($a0) #load the array into t1
beq $t1, $a3, linearFound #if array[element] = target, found target
addi $a0, $a0, 4 #add 4 to the array size
addi $s4, $s4, 1 #add one to the index
j linearLoop #SOS THIS IS NO FUN !!!!!#
linearFound:
move $v1, $s4 #move $t0 into $v1
j exitLoop
linearFailed:
li $v1, -1
j exitLoop
exitLoop:
jr $ra
#############################
# END LINEAR SEARCH #
############################

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.

How to fix bad address error in MIPS assembly

I am very new to MIPs programming and have been struggling alone to solve to problem. I am in need of help from people of expertise, to help me through the way.
I am trying to build a program that takes in multiple integer inputs from the console and sort it through mergesort.
The problem is, though I believe logic is right, I keep on facing "Bad address in data/stack read: Ox...." error in the merge phase of the logic.
It would be of great help if you could help.
Thank you in advance.
.data
array: .space 40 # store max up to 10 elements integer array
eol: .asciiz "\n"
mess: .asciiz " Enter 10 numbers to be stored in the array. "
# Some test data
eight: .word 8
five: .word 5
four: .word 4
nine: .word 9
one: .word 1
seven: .word 7
six: .word 6
ten: .word 10
three: .word 3
two: .word 2
# An array of pointers (indirect array)
length: .word 10 # Array length
info: .word seven
.word three
.word ten
.word one
.word five
.word two
.word nine
.word eight
.word four
.word six
.text
main :
la $t0, 0
jal readInput
la $a0, array # Load the start address of the array
lw $t0, length # Load the array length
sll $t0, $t0, 2 # Multiple the array length by 4 (the size of the elements)
add $a1, $a0, $t0 # Calculate the array end address
jal mergesort # Call the merge sort function
# b sortend # We are finished sorting
b prdone
readInput :
slt $t1, $t0, 40
beq $t1, 1, readInputLoop
jr $ra
readInputLoop :
beq $t0, 40, readInput
# Printout the message for input
li $v0, 4
la $a0, mess
syscall
# Receive inputs from the user
li $v0, 5
syscall
sw $v0, array($t0)
li $v0, 1
lw $a0, array($t0)
syscall
# Increment the loop index by 4, and loop again
addi $t0, $t0, 4
b readInputLoop
##
# Recrusive mergesort function
#
# #param $a0 first address of the array
# #param $a1 last address of the array
##
mergesort:
addi $sp, $sp, -16 # Adjust stack pointer
sw $ra, 0($sp) # Store the return address on the stack
sw $a0, 4($sp) # Store the array start address on the stack
sw $a1, 8($sp) # Store the array end address on the stack
sub $t0, $a1, $a0 # Calculate the difference between the start and end address (i.e. number of elements * 4)
ble $t0, 4, mergesortend # If the array only contains a single element, just return
srl $t0, $t0, 3 # Divide the array size by 8 to half the number of elements (shift right 3 bits)
sll $t0, $t0, 2 # Multiple that number by 4 to get half of the array size (shift left 2 bits)
add $a1, $a0, $t0 # Calculate the midpoint address of the array
sw $a1, 12($sp) # Store the array midpoint address on the stack
jal mergesort # Call recursively on the first half of the array
lw $a0, 12($sp) # Load the midpoint address of the array from the stack
lw $a1, 8($sp) # Load the end address of the array from the stack
jal mergesort # Call recursively on the second half of the array
lw $a0, 4($sp) # Load the array start address from the stack
lw $a1, 12($sp) # Load the array midpoint address from the stack
lw $a2, 8($sp) # Load the array end address from the stack
jal merge # Merge the two array halves
mergesortend:
lw $ra, 0($sp) # Load the return address from the stack
addi $sp, $sp, 16 # Adjust the stack pointer
jr $ra # Return
##
# Merge two sorted, adjacent arrays into one, in-place
#
# #param $a0 First address of first array
# #param $a1 First address of second array
# #param $a2 Last address of second array
##
merge:
addi $sp, $sp, -16 # Adjust the stack pointer
sw $ra, 0($sp) # Store the return address on the stack
sw $a0, 4($sp) # Store the start address on the stack
sw $a1, 8($sp) # Store the midpoint address on the stack
sw $a2, 12($sp) # Store the end address on the stack
move $s0, $a0 # Create a working copy of the first half address
move $s1, $a1 # Create a working copy of the second half address
mergeloop:
lw $t0, 0($s0) # Load the first half position pointer
lw $t1, 0($s1) # Load the second half position pointer
###### SOURCE LOCATION OF THE PROBLEM #####
lw $t0, 0($t0) # Load the first half position value
lw $t1, 0($t1) # Load the second half position value
bgt $t1, $t0, noshift # If the lower value is already first, don't shift
move $a0, $s1 # Load the argument for the element to move
move $a1, $s0 # Load the argument for the address to move it to
jal shift # Shift the element to the new position
addi $s1, $s1, 4 # Increment the second half index
noshift:
addi $s0, $s0, 4 # Increment the first half index
lw $a2, 12($sp) # Reload the end address
bge $s0, $a2, mergeloopend # End the loop when both halves are empty
bge $s1, $a2, mergeloopend # End the loop when both halves are empty
b mergeloop
mergeloopend:
lw $ra, 0($sp) # Load the return address
addi $sp, $sp, 16 # Adjust the stack pointer
jr $ra # Return
##
# Shift an array element to another position, at a lower address
#
# #param $a0 address of element to shift
# #param $a1 destination address of element
##
shift:
li $t0, 10
ble $a0, $a1, shiftend # If we are at the location, stop shifting
addi $t6, $a0, -4 # Find the previous address in the array
lw $t7, 0($a0) # Get the current pointer
lw $t8, 0($t6) # Get the previous pointer
sw $t7, 0($t6) # Save the current pointer to the previous address
sw $t8, 0($a0) # Save the previous pointer to the current address
move $a0, $t6 # Shift the current position back
b shift # Loop again
shiftend:
jr $ra # Return
sortend: # Point to jump to when sorting is complete
# Print out the indirect array
li $t0, 0 # Initialize the current index
prloop:
lw $t1,length # Load the array length
bge $t0,$t1,prdone # If we hit the end of the array, we are done
sll $t2,$t0,2 # Multiply the index by 4 (2^2)
lw $t3,array($t2) # Get the pointer
lw $a0,0($t3) # Get the value pointed to and store it for printing
li $v0,1
syscall # Print the value
la $a0,eol # Set the value to print to the newline
li $v0,4
syscall # Print the value
addi $t0,$t0,1 # Increment the current index
b prloop # Run through the print block again
prdone: # We are finished
li $v0,10
syscall
Expected result would be printed integers of a sorted array, but i keep on receiving the error message of
"Exception occurred at PC=0x0040015c Bad address in data/stack read:
0x00000000".

MIPS Selection Sort Not Working

I am trying to write a selection sort algorithm in MIPS but am having problems. I have tested my swap and findSmallest functions individually and they appear to be working but when I run the code I get some addresses (I'm assuming) in the output.
.data
.align 2
values: .word 12, 15, 13, 2, 25, 3, 5
valueCount: .word 7
initialPrint: .asciiz "\Unsorted Array: \n"
donePrint: .asciiz "\n Sorted Array: \n"
comma: .asciiz ", "
.text
.globl main
main: # Beginning stuff
la $a0, initialPrint # load initialPrint string into a0
li $v0, 4 # set code (4) to print string
syscall # print initialPrint
la $s6, values
la $s7, valueCount
lw $s7, 0($s7) # extract valueCount word from s7
move $a0, $s6 # move values into $a0 for function call
move $a1, $s7 # move valueCount into $a1 for function call
jal printList # print initial array
li $s1, 0 # index = 1
selectionSort: # Middle stuff
bge $s1, $s7, endPrint
move $a0, $s6 # move values into $a0 for function call
move $a1, $s7 # move valueCount into $a1 for function call
move $a2, $s1 # move index into $a2 for function call
jal findSmallest # Let's get this show on the road
move $a0, $v0 # move smallestIndex into a0
move $a1, $s1 # move currentndex into $a1
move $a2, $s6 # move values into $a2
jal swap # swap
move $s6, $v0 # move swapped array into $s6
addi $s6, $s6, 4 # next position in array
addi $s1, $s1, 1 # index++
j selectionSort
# Ending stuff
endPrint: la $a0, donePrint # load donePrint string into a0
li $v0, 4 # set code (4) to print string
syscall # print donePrint
move $a0, $s6 # move values into $a0 for function call
move $a1, $s7 # move valueCount into $a1 for function call
jal printList # print initial array
li $v0, 10 # load end call
syscall # end
##########################################################################################
############# findSmallest Function ##################################################
##########################################################################################
## takes in array, current index, and valueCount
## examines all elements from n-index through n
## outputs index of lowest value
findSmallest: ## Begin Function
addi $sp, $sp, -24 # preserve state before function call
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
sw $s4, 20($sp)
sw $t1, 24($sp)
## s0 = values
## s1 = valueCount
## s2 = index
## s3 = smallestIndex
## s4 = smallestValue
## t1 = currentValue
## Function Body
move $s0, $a0 # move values into s0
move $s1, $a1 # move valueCount into s1
move $s2, $a2 # move index into s2
lw $s4, 0($s0) # load first word
addi $s0, $s0, 4 # move to next word
addi $s2, $s2, 1
floop: bge $s2, $s1, endLoop # if index >= valueCount then done
lw $t1, 0($s0) # load currentValue
ble $s4, $t1, skip # if smallestValue < currentValue then skip
move $s4, $t1 # set smallestValue = currentValue
move $s3, $s2 # set smallestIndex = currentIndex
skip:
addi $s0, $s0, 4 # iterate to next word in values
addi $s2, $s2, 1 # index++
j floop # repeat loop
endLoop: move $v0, $s3
# End Function Stuff
lw $ra, 0($sp) # restore state before function call
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $s4, 20($sp)
lw $t1, 24($sp)
addi $sp, $sp, 24
jr $ra
##########################################################################################
############# swap Function ##########################################################
##########################################################################################
swap: # Begin Function
addi $sp, $sp, -32 # preserve state before function call
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $t1, 12($sp)
sw $t2, 16($sp)
sw $t3, 20($sp)
sw $t4, 24($sp)
sw $s6, 28($sp)
# main function
move $s0, $a0 # move smallestIndex into function
move $s1, $a1 # move targetIndex into function
move $s6, $a2
sll $s0, $s0, 2
add $t1, $s0, $s6
lw $t2, 0($t1) # get the value from the array cell
sll $s1, $s1, 2
add $t3, $s1, $s6
lw $t4, 0($t3) # get the value from the array cell
sw $t2, 0($t3)
sw $t4, 0($t1)
move $v0, $s6
# End Function # restore state before function call
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $t1, 12($sp)
lw $t2, 16($sp)
lw $t3, 20($sp)
lw $t4, 24($sp)
lw $s6, 28($sp)
addi $sp, $sp, 32
jr $ra
##########################################################################################
############# printList Function #####################################################
##########################################################################################
# a0 - values array (address)
# a1 - valueCount (word)
# s6 - values
# s7 - valueCount (word)
printList:
## Setup
## Begin Function
addi $sp, $sp, -16 # allocate stack space for 3 values
sw $ra, 0($sp) # store return adder
sw $s6, 4($sp)
sw $s7, 8($sp)
sw $s0, 12($sp)
move $s6, $a0
move $s7, $a1
li $s0, 1 # index = 1
print:
lw $a0, 0($s6)
li $v0, 1 # load output call
syscall # output value
la $a0, comma # load comma string into a0
li $v0, 4 # set code (4) to print string
syscall # print initialPrint
addi $s6, $s6, 4
addi $s0, $s0, 1
ble $s0, $s7, print
# End Function Stuff
lw $ra, 0($sp) # store return adder
lw $s6, 4($sp)
lw $s7, 8($sp)
lw $s0, 12($sp)
addi $sp, $sp, 16
jr $ra
My output:
Unsorted Array:
12, 15, 13, 2, 25, 3, 5, soSorted Array:
25, 1684370546, 13, 3, 540703073, 1869835861, 1953656659,
It has to be something with the way the array is being stored because there are memory addresses in the output.
I found a number of issues in your code:
move $a0, $v0 # move smallestIndex into a0
move $a1, $s1 # move currentndex into $a1
move $a2, $s6 # move values into $a2
jal swap # swap
Here you're passing a couple of indices that are relative to the beginning of the array, but $s6 is incremented at the end of each iteration, so it will only point to the beginning of the array during the very first iteration. Instead of move $a2, $s6 you should use la $a2, values.
move $s6, $v0 # move swapped array into $s6
This should be removed altogether.
move $a0, $s6 # move values into $a0 for function call
move $a1, $s7 # move valueCount into $a1 for function call
jal printList
Here you should place the base address of the array in $a0, so it should be la $a0, values instead of move $a0, $s6.
And in findSmallest you don't seem to initialize $s3 for the case where the first element is the smallest one.

Printing to Screen in MARS for MIPS program

Hi I have to write a program in MIPS that does a bubble sort and then prints the sorted array and plays a note(s). I am having trouble getting it to execute in Mars and I am wondering what I need to add to my program:
.include "ec2_data.asm" # load values into array
add $s0, $zero, 0x10010000
add $t0, $zero, 0x10010000 #how many times to loop
addi $t1, $zero, 0 #initilize the counter
addi $t2, $zero ,0 #initilize position x
addi $t3, $zero, 4 #initilize position y
lw $s2, $t3($s0) #get second position of $s0=y
LOOP:
addi $t0, $t0, -1 #subtract one from the counter
slt $t4, $s1, $s2 #t4 set to 1 if x > y
beqz $t4, BREAK #if t0 is zero (x<y) we dont' have to sort, so go to break
# sort:
add $t5, $zero, $s1 #temp. stores the value of x in t5
add $s1, $zero, $s2 #moves the value of y into x
add $s2, $zero, $t5 #moves the value of x into y
sw $s1, $t2($s0) #save the new x to register
sw $s2, $t3($s0) #save the new y to register
j BREAK
BREAK:
#in here: check to see if we have reached the end
#if not, increment t2 and t3 by 4 to get the next values and jump back to loop to go again
beq $t0, $zero, END #if we are done with the array, go to end
addi $t2, $t2, 4
addi $t3, $t3, 4
j LOOP #jump back to loop again
END:
li $v0, 1 # print integer
syscall
addi $a0, $0, 0xA # load line code into register
addi $v0, $0, 0xB # print new line
syscall
addi $v0, $zero, 33 # midi out synchronous
addi $a0, $zero, 60 # Middle-C
addi $a1, $zero, 250 # ms
addi $a2, $zero, 120 # some instrument
addi $a3, $zero, 64 # some volume
add $a0, $t7, $zero # load value into $a0
syscall # play note!
addi $t6, $t6, 4 # shift memory location by 32 bits
addi $s1, $s1, 1 # increment counter ++
j loop # loop
Exit:
li $v0, 10 # load exit code
syscall # exit
If by "having trouble getting it to execute", you mean it acts strange with the .include file, then you might not have a required option checked in MARS. Go to the Settings menu and check the box for "Initialize Program Counter to global Main if defined". That seems to be something that has to be in effect for included files to work - and for some reason, it isn't turned on by default.

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