So I am trying to recursively reverse an array but for some reason it's only printing the original array. I need it to print the reverse array with it being reversed. My code is below. Any help would be greatly appreciated.
.data
list: .word 1 2 3 4 5 6
size: .space 6
head: .asciiz "The Original Array is:\n"
back: .asciiz "The Reverse Array is:\n"
.text
.globl main
main:
li $v0,4 # print syscall
la $a0,list
syscall
li $v0,8 # read array
la $a0,list
li $a1,1000
syscall
la $a0,list
jal arrlen
move $a1,$v0
la $a0,list
jal reverse
li $v0,4
la $a0,list
syscall
li $v0,10
syscall
reverse:
move $t0,$ra
addu $a1,$a0,$a1
addiu $a1,$a1,-1
j reverse_next
reverse_loop:
jal swap
addiu $a0,$a0,1
addiu $a1,$a1,-1
reverse_next:
ble $a0,$a1,reverse_loop
jr $t0
swap:
lb $v0,0($a0)
lb $v1,0($a1)
sb $v1,0($a0)
sb $v0,0($a1)
jr $ra
arrlen:
move $v0,$a0
arraylen_loop:
lb $t1,0($a0)
addi $a0,$a0,1
bnez $t1,arraylen_loop
subu $v0,$a0,$v0
subi $v0,$v0,1
jr $ra
Related
I have to print an array in mips, but I have to work with some given code that I'm not allowed to change.
As of right now it is printing the correct amount of times, but it prints out a huge number that isn't what I am inputting for the numbers in the list. Everything in the following code is given besides what I've written under printList. Can someone help me fix this or at least steer me in the right direction? Any tips for the remaining functions would be welcome too.
.data
original_list: .space 100
sorted_list: .space 100
str0: .asciiz "Enter size of list (between 1 and 25): "
str1: .asciiz "Enter one list element: \n"
str2: .asciiz "Content of original list: "
str3: .asciiz "Enter a key to search for: "
str4: .asciiz "Content of sorted list: "
strYes: .asciiz "Key found!"
strNo: .asciiz "Key not found!"
.text
#This is the main program.
#It first asks user to enter the size of a list.
#It then asks user to input the elements of the list, one at a time.
#It then calls printList to print out content of the list.
#It then calls inSort to perform insertion sort
#It then asks user to enter a search key and calls bSearch on the sorted list.
#It then prints out search result based on return value of bSearch
main:
addi $sp, $sp -8
sw $ra, 0($sp)
li $v0, 4
la $a0, str0
syscall
li $v0, 5 #read size of list from user
syscall
move $s0, $v0
move $t0, $0
la $s1, original_list
loop_in:
li $v0, 4
la $a0, str1
syscall
sll $t1, $t0, 2
add $t1, $t1, $s1
li $v0, 5 #read elements from user
syscall
sw $v0, 0($t1)
addi $t0, $t0, 1
bne $t0, $s0, loop_in
move $a0, $s1
move $a1, $s0
jal inSort #Call inSort to perform insertion sort in original list
sw $v0, 4($sp)
li $v0, 4
la $a0, str2
syscall
la $a0, original_list
move $a1, $s0
jal printList #Print original list
li $v0, 4
la $a0, str4
syscall
lw $a0, 4($sp)
jal printList #Print sorted list
li $v0, 4
la $a0, str3
syscall
li $v0, 5 #read search key from user
syscall
move $a3, $v0
lw $a0, 4($sp)
jal bSearch #call bSearch to perform binary search
beq $v0, $0, notFound
li $v0, 4
la $a0, strYes
syscall
j end
notFound:
li $v0, 4
la $a0, strNo
syscall
end:
lw $ra, 0($sp)
addi $sp, $sp 8
li $v0, 10
syscall
#printList takes in a list and its size as arguments.
#It prints all the elements in one line.
printList:
move $t3, $0
loop1:
sll $t4, $t3, 2
add $t4, $t4, $s1
li $v0, 1
move $a0, $t4
syscall
sw $v0, 0($t1)
addi $t3, $t3, 1
bne $t3, $s0, loop1
jr $ra
#inSort takes in a list and it size as arguments.
#It performs INSERTION sort in ascending order and returns a new sorted list
#You may use the pre-defined sorted_list to store the result
inSort:
#Your implementation of inSort here
jr $ra
#bSearch takes in a list, its size, and a search key as arguments.
#It performs binary search RECURSIVELY to look for the search key.
#It will return a 1 if the key is found, or a 0 otherwise.
#Note: you MUST NOT use iterative approach in this function.
bSearch:
#Your implementation of bSearch here
jr $ra
So ive been able to create an array that is the size of what the user chooses and the code proceeds to Sort just fine when debugging, the issue is for some reason its not getting past Sort and into swap and im not quite sure why. In the debugger it seems to read the code to swap but then skips over it and repeats sort in an infinite loop. Am i doing something wrong?
.data
count: .word 0
prompt1: .asciiz "Enter the array size \n"
prompt2: .asciiz "Enter integers to fill the array \n"
prompt3: .asciiz "Sorted Array: "
.text
li $v0, 4
la $a0, prompt1
syscall
li $v0, 5
syscall
sw $v0, count
sll $a0, $v0, 2
li $v0, 9
syscall
move $t0, $v0
lw $t1, count
loop:
li $v0, 4
la $a0, prompt2
syscall
li $v0, 5
syscall
beqz $v0, sort
sw $v0, 0($t0)
addi $t0, $t0, 4
sub $t1, $t1, 1
bnez $t1, loop
sort:
lw $t3, 0($t0)
lw $t4, 4($t0)
blt $t4, $t3, swap
addiu $t0, $t0, 4
addi $t1, $t1, -1
bne $t1, $zero, sort
jr $ra
swap:
sw $t3, 4($t0)
sw $t4, 0($t0)
j sort
end:
exit:
li $v0, 10
syscall
For some reason the integer values i'm putting into memory of the array of base $t0 cant be accessed later on even though when debugging i can clearly see that they are stored correctly in memory at 0($t0) and 4($t0) in the case of 2 integers. Its when i go into the passloop and attempt to load them into t6 or t7 that no value is being passed. Can someone explain why this is?
# bubble sort
.data
prompt1: .asciiz "Enter the array size \n"
prompt2: .asciiz "Enter integers to fill the array \n"
prompt3: .asciiz "Sorted Array: "
.text
.globl main
main:
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
add $t1, $zero, $v0
add $t2, $zero, $v0
sll $a0, $v0, 2
li $v0, 9
syscall
add $t0, $zero, $v0
loop:
la $a0, prompt2
li $v0, 4
syscall
li $v0, 5
syscall
beqz $t1, mainloop
sw $v0, ($t0)
addiu $t0, $t0, 4
subiu $t1, $t1, 1
bnez $t1, loop
mainloop:
subi $a1,$t2,1
blez $a1,done
li $t3,0
jal passloop
beqz $t3,done
subi $t2,$t2,1
j mainloop
#print sorted data, code still needed. Need to fix accessing issue first
done:
j end
passloop:
lw $t6,0($t0)
lw $t7,4($t0)
bgt $t6,$t7,swap
next:
addiu $t0,$t0,4
subiu $a1,$a1,1
bgtz $a1,passloop
jr $ra
swap:
sw $t6,4($t0)
sw $t7,0($t0)
li $t3,1
j next
end:
li $v0,10
syscall
I am trying to store a dynamic array into a program using a stack, by first reading an input n and then creating a stack with [n] inputs. However, when I try to store word, it creates an error that I don't understand.
.data
ask_for_size: .asciiz "\nInsert the array size\n"
ask_for_int: .asciiz "\nInput number\n"
input: .space 16
.text
Main:
li $v0,4
la $a0,ask_for_size
syscall
li $v0,5
la $a0,input
syscall
add $t0,$v0,$zero
li $sp,0x00001000 #stack pointer
li $s0,0x00001000 #base pointer
la $a0,ask_for_int
Loop1:
li $v0,4
syscall
li $v0,5
syscall
sw $v0,0($sp)
addi $sp,$sp,-4
addi $t0,$t0,-1
bne $t0,$zero,Loop1
j Loop2
Loop2:
Shouldn't you add -4 to $sp before you sw $v0 into it?
addi $sp,$sp,-4
sw $v0,0($sp)
It seems to me that at this time you're overwriting past the end of $sp allocated for your process.
Also you load $a0 only once. Are you sure it does not get modified? Otherwise, maybe that would work better?
Loop1:
li $v0,4
la $a0,ask_for_int
syscall
...
How to find length of an array in mips,spim?
I wrote this for practice. I tested it, and it works well. You probably already figured this out, but if not, there it is.
.data
array1: .word 1,2,3,4,5,6,7,8,9
.text
main:
la $a0,array1
jal lenArray
move $a0,$v0
syscall $print_int
exit:
li $a0,10
syscall
lenArray: #Fn returns the number of elements in an array
addi $sp,$sp,-8
sw $ra,0($sp)
sw $a0,4($sp)
li $t1,0
laWhile:
lw $t2,0($a0)
beq $t2,$0,endLaWh
addi $t1,$t1,1
addi $a0,$a0,4
j laWhile
endLaWh:
move $v0,$t1
lw $ra,0($sp)
lw $a0,4($sp)
addi $sp,$sp,8
jr $ra