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.
Related
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.
I know that in c or java the quick fix to changing an ascending sort to descending would be to find the < or > somewhere and reverse it. But try as I might, I can't find the equivalent (or equivalent swap) to change this code that I've written to be descending instead of ascending. (Right now, it works perfectly in printing the linkedlist in ascending order). There's a fair 100 or so lines above what I've included that I cut out as they don't do the sort, but they assign values--so that isn't the problem. I feel like I'm just missing something obvious? Would the change be in the way I print or the way I insert or both? Any help would be appreciated.
loop_to_find_correct_spot:
beqz $t0, if_current_node_next_equals_null
l.s $f0, 0($t0)
c.lt.s $f12, $f0
bc1t if_current_node_does_not_equal_null
or $t1, $t0, $zero
lw $t0, 4($t0)
j loop_to_find_correct_spot
if_current_node_next_equals_null:
# link this node to the previous, $v0 = &(previous node)
# copy address of the new node into the previous node
sw $v0, 4($t1)
# initalize the current node value to the passed in argument on the $f12 register
swc1 $f12, 0($v0)
# initalize the current node next value to null
sw $zero, 4($v0)
j end_insert_if_else
if_current_node_does_not_equal_null:
# initalize the current node value to the passed in argument on the $f12 register
swc1 $f12, 0($v0)
sw $v0, 4($t1)
sw $t0, 4($v0)
j end_insert_if_else
end_insert_if_else:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
allocate_memory_for_node:
addi $sp, $sp, -4
sw $ra, 0($sp)
# allocate 12 bytes in memory for the new node
li $v0, 9
li $a0, 8
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
print_linked_list:
addi $sp, $sp, -4
sw $ra, 0($sp)
lw $s0, head # get a pointer to the head node
lw $s0, 4($s0)
begin_loop_print_linked_list:
# while (next != null)
beqz $s0, end_loop_print_linked_list
# get the value for the current node
l.s $f12, 0($s0)
# print out current item
li $v0, 2
syscall
la $a0, end_line_prompt #puts newlines in printed list
li $v0, 4
syscall
#get the pointer to the next node
lw $s0, 4($s0)
b begin_loop_print_linked_list
end_loop_print_linked_list:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
Hello I'm trying to make a Quicksort, sorting algorithm using assembly (MIPS). I'm fairly new to assembly and I'm having some problems with how to use the stack in my program.
What I am trying to do is to use the stack in order to make the quicksort subroutine recursive. The problem I get when doing this is that the array that I'm trying to sort when moving the stack pointer.
Here's the code:
main:
li $s4, 0 # Min value of the range inside the array you want to sort
li $s5, 15 # Max value of the range inside the array that you want to sort
add $t6, $zero, $s4 # left-value
add $t7, $zero, $s5 # right-value
addiu $sp, $sp, -12 # We need space for the register $ra and register $a0
sw $ra, 0($sp) # A must have, if you wanna come back and avoid strange behaviour
sw $s4, 4($sp)
sw $s5, 8($sp)
jal quickSort
nop
ori $v0,$zero,10 # Prepare syscall to exit program cleanly
syscall
quickSort:
move $a0, $zero
add $a0, $zero, $t6 # Min value of the range inside the array you want to sort
move $a1, $zero
add $a1, $zero, $t7 # Max value of the range inside the array that you want to sort
li $t5, 4 # Temp to mult with
move $t3, $zero
add $t3, $a0, $a1
div $t3, $t3, 2
mult $t3, $t5
mflo $t3
lw $s0, data($t3) # Pivot = (min size + max size) / 2
mult $a1, $t5 # offset for min value
mflo $s1, # offset for array min
mult $a2, $t5 # offset for max value
mflo $s2 # offset for array max
sortLoop:
bgt $a0, $a1, endLoop # While min value is less or equal to max value
nop
leftLoop:
lw $t4, data($s1)
bge $t4, $s0, rightLoop
nop
add $s1, $s1, 4
add $a0, $a0, 1
j leftLoop
nop
rightLoop:
lw $t5, data($s2)
ble $t5, $s0, switchValue
nop
add $s2, $s2, -4
add $a1, $a1, -1
j rightLoop
nop
switchValue:
bgt $a0, $a1, repeat
nop
sw $t5, data($s1)
sw $t4, data($s2)
add $s1, $s1, 4
add $a0, $a0, 1
add $s2, $s2, -4
add $a1, $a1, -1
repeat:
j sortLoop
endLoop:
recursion:
bge $s4, $a1, recursion2
nop
addiu $sp, $sp, -12
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $a1, 8($sp)
move $t6, $zero
add $t6, $t6, $s4 # left-value
move $t7, $zero
add $t7, $t7, $a1
jal quickSort
nop
recursion2:
bge $a0, $s5,endSorting
nop
addiu $sp, $sp, -12
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $a1, 8($sp)
move $t6, $zero
add $t6, $zero, $a0
move $t7, $zero
add $t7, $zero, $s5
jal quickSort
nop
endSorting:
lw $ra, 0($sp)
lw $s4, 4($sp)
lw $s5, 8($sp)
addi $sp, $sp, 12
jr $ra
nop
My questions are as following:
Does the .data values also go to the stack?
Is this the proper use of the stack?
If not is there any good tutorials or something that can help me learn it better?
I load two arrays. I have to return a third array that contain the sum of the two and has the dimension of the smaller. Print the last one.
Example:
INPUT: Array1: [1 2 3 4 5] Array2: [2 6 1]
OUTPUT: Array3: [3 8 4]
Program runs... it give me the exact number of elements but every element is 0.
.data
vett1: .word 0:100
vett2: .word 0:100
vett3: .word 0:100
x: .word 0
space: .asciiz " "
.text
.globl main
main:
la $a0, vett1
la $a1, vett2
la $a2, vett3
la $a3, x
li $t0, 0
li $t1, 0
li $t2, 0
jal loadA1 #mi salvo 26 salto a caricavettore1
jal loadA2 #mi salvo 27 salto a caricavettore2
jal lenght #mi salvo 28 salto a controllalunghezza
lw $t2, ($a3)
lw $t3, ($a3)
la $a0, vett1
la $a1, vett2
la $a2, vett3
jal summ
jal print
li $v0, 10
syscall
loadA1:
li $v0, 5
syscall
beq $v0, -1, exit
sw $v0, ($a0)
addi $t0, $t0, 1
addi $a0, $a0, 4
j loadA1
exit: jr $ra
loadA2:
li $v0, 5
syscall
beq $v0, -1, exit2
addi $t1, $t1, 1
sw $v0, ($a1)
addi $a1, $a1, 4
j loadA2
exit2: jr $ra
lenght:
blt $t0, $t1, cond1
sw $t1, ($a3)
jr $ra
cond1: sw $t0, ($a3)
jr $ra
summ:
subi $sp, $sp, 4
sw $ra, 0($sp)
bnez $t2, rec
j exit3
rec: lw $s0, ($a0)
lw $s1, ($a1)
add $v0, $s0, $s1
sw $v0, ($a2)
addi $a0, $a0, 4
addi $a1, $a1, 4
addi $a2, $a2, 4
subi $t2, $t2, 1
jal summ
exit3:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
print:
beqz $t3, exit4
lw $a0, ($a2)
li $v0, 1
syscall
la $a0, space
li $v0, 4
syscall
addi $a2, $a2, 4
subi $t3, $t3, 1
j print
exit4: jr $ra
When you sum the two arrays you add 4 to $a2 at the end of each iteration to point it to the next element of vett3. So after the summing is complete $a2 will point to the first memory location after the end of vett3.
Then you call print but don't reset $a2 to point at the start of vett3, so you end up printing garbage data (which could happen to be all zeroes - or mostly zeroes at least, since x and space will be there).
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.