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
Related
I am working on a program that calculates combinations in MIPS. The user enters the size of the pool, and number of balls to pick from it, and the program should output the total combinations. The program correctly calculates the numerator, but when it comes to dividing the numerator (n!) by the denominator, which is also correctly calculated, the combinations number is wrong.
I think something goes wrong with $t2 when it is used in main, but not sure what exactly or how to fix it.
.data
message1: .asciiz "Enter input for the size of the large pool: "
message2: .asciiz "Enter input for # of balls to be selected from large pool: "
message5: .asciiz "The numerator is: "
newline: .asciiz "\n"
.text
.globl main
main:
# Call on function messages
jal messages
# Calculate factrl for # from large pool size
move $a0, $s2
jal factrl # Call on factrl function
move $t5, $v0
# Find (n-k) for large pool
sub $t0, $s1, $s2
# Intialize $t2 to 1
li $t2, 1
# Call function while for large pool
move $a1, $s1
move $a2, $t0
jal while
move $v0, $t2
# Find probability for large
move $a0, $v0
move $a1, $t0
jal combination
move $t7 $v0
li $v0, 4
la $a0, newline
syscall
li $v0, 1
add $t7, $t7, $zero
syscall
# Exit Program
li $v0, 10
syscall
messages:
# Request size of large pool
li $v0, 4
la $a0, message1
syscall
# Get input for size of large pool
li $v0, 5
syscall
# Store input in register
move $s1, $v0
# Request # selected from large pool
li $v0, 4
la $a0, message2
syscall
# Get input for # of large pool
li $v0, 5
syscall
# Store input in register
move $s2, $v0
li $v0, 4
la $a0, newline
syscall
jr $ra
factrl:
sw $ra, 4($sp) # save the return address
sw $a0, 0($sp) # save the current value of n
addi $sp, $sp, -8 # move stack pointer
slti $t0, $a0, 2 # save 1 iteration, n=0 or n =1; n!=1
beq $t0, $zero, L1 # not less than 2 , calculate n(n - 1)!
addi $v0, $zero, 1 # n=1; n!=1
jr $ra # now multiply
L1:
addi $a0, $a0, -1 # n = n - 1
jal factrl # now (n - 1)!
addi $sp, $sp, 8 # reset the stack pointer
lw $a0, 0($sp) # fetch saved (n - 1)
lw $ra, 4($sp) # fetch return address
mul $v0, $a0, $v0 # multiply (n)*(n - 1)
jr $ra # return value n!
while:
addi $sp, $sp, -8 # allocate space in stack
sw $ra, 4($sp)
sw $a1, 0($sp) # store the value of $s1 on the stack
beq $a1, $a2, numerator
mult $t2, $a1 # multiply val x n
mflo $t3
move $t2, $t3
addi $a1, $a1, -1 # n = n-1
j while
numerator:
li $v0, 4
la $a0, message5
syscall
# Print the numerator
li $v0, 1
move $a0, $t2
syscall
lw $a1, 0($sp)
addi $sp, $sp, 8 # reset stack pointer
jr $ra
combination:
div $v0, $a1, $a0
jr $ra
exit:
li $v0, 10
syscall
I'm new to MIPS and having some trouble with my program.
Right now my code works at inserting an integer value into the array at a specified index. But currently, when the integer is inserted it replaces the value that was previously there. How do I instead shift all elements after said index so the array's length increases by 1 and no values are lost?
Note that I'm using a special integer -22 to indicate the end of my array.
Any help would be greatly appreciated! Thank you!
Below is my code:
.data
beginarray: .word 1, 2, 3, 4, -22
array: .space 4000
str_command: .asciiz
str1: .asciiz "\nEnter an Integer: "
str2: .asciiz "\nAt what index: "
.text
.globl main
main:
# get length
la $a0, beginarray
jal length
addi $s0, $v0, 0 # $s0 = returned length
# copy beginarray into array
la $a0, beginarray
la $a1, array
jal copyarray
jal insert
# print array
la $a0, array
jal printarray
li $v0,10
syscall
# takes pointer to array as input
length: addi $t0, $zero, -22 # $t0 = -22
addi $v0, $zero, 0 # $v0 = 0 (length)
add $t1, $a0, $zero # $t1 = pointer to first int
calclength: lw $t2, 0($t1) # $t1 = value of current int
beq $t2, $t0, returnlength # if $t1 = -22
addi $v0, $v0, 1 # increment length
addi $t1, $t1, 4 # move pointer to next int
j calclength # loop
returnlength: jr $ra
# takes two pointers as input (pointer to first array and pointer to second array)
# copies contents of first array into second array
copyarray: addi $t0, $a0, 0 # $t0 = pointer to array1
# addi $t1, $a1, 0 # $t1 = pointer to array2
addi $t4, $zero, -22 # $t4 = -22
calccopy: lw $t2, 0($t0) # $t2 = value of array1
#lw $t3, 0($t1) # $t3 = value of array2
sw $t2, 0($a1) # store int in array
beq $t2, $t4, returncopy # if $t2 = -22
addi $t0, $t0, 4 # move a1 pointer to next int
addi $a1, $a1, 4 # move a2 pointer to next int
j calccopy
returncopy: jr $ra
# print array
printarray: add $t1, $a0, $zero # $t1 = pointer to first int
addi $t0, $zero, 0 # set counter to zero
printloop: beq $t0, $s0, done
# load word from addrs and goes to the next addrs
lw $t2, 0($t1)
addi $t1, $t1, 4
# syscall to print value
li $v0, 1
move $a0, $t2
syscall
# syscall for printing space
li $a0, 32
li $v0, 11
syscall
#increment counter
addi $t0, $t0, 1
j printloop
done: jr $ra
insert:
li $v0, 4 # system call code for print_str
la $a0, str1
syscall
li $v0 5 # system call code for read_int
syscall
move $t3, $v0 # $t3 = integer entered
li $v0, 4 # system call code for print_str
la $a0, str2
syscall
li $v0 5 # system call code for read_int
syscall
move $t1, $v0 # $t1 = index entered
li $t0, 0 # $t0 = counter
la $t2, array # $t2 = base address of array
for: bge $t0, $t1, end_for
add $t2, $t2, 4 # get address of next array element
add $t0, $t0, 1 # increment loop induction variable
b for
end_for:sw $t3, ($t2)
jr $ra
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
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 making a program for a user to input a range of numbers, and calculate the min, max and median. Right now, I am only trying to collect the numbers and echo them back to make sure I am getting them. Here is the problem:
I input numbers like this:
1
2
3
4
5
And when the array is printed out I get:
15345
It does not matter what numbers are used, the 2nd element in the array is always replace by the last element.
Here is my mips code, I know it is kind of long, but it is the shortest workable example I can make.
Note: you must enter 9999 for the program to exit the loop.
.data
welcomeString: .asciiz "Please input one number at a time, and then press enter.\n"
intArray: .word 4000
size: .word 0
.text
main:
li $v0, 4
la $a0, welcomeString
syscall
la $a0, intArray
jal gather_numbers
la $a0, intArray
jal print_array
####################################################################################
gather_numbers:
addi $sp, $sp, -12
sw $a0, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $t1, 12($sp)
move $s0, $a0 #the address of the array
lw $s1, size # load the size
li $t1, 0 # so it enters the loop
start_gather_numbers: beq $t1, 9999, exit_gather_numbers
li $v0, 5 # read the integer
syscall
sw $v0, 0($s0)
move $t1, $v0 # put the value into t1 to be tested
addi $s0, $s0, 4 #increment the address
addi $s1, $s1, 1 # increment the size
j start_gather_numbers
exit_gather_numbers: addi $s1, $s1, -1 # fix the size
sw $s1, size # store the size
lw $a0, 0($sp) # pop the stack
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $t1, 12($sp)
addi $sp, $sp, 12
####################################################################################
####################################################################################
print_array:
addi $sp, $sp, -16
sw $a0, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $t0, 12($sp)
sw $t1, 16($sp)
move $s0, $a0 # the address of the array
lw $s1, size # load the size of the array
li $t0, 0 # i = 0
start_print_array: bge $t0, $s1, exit_print_array
lw $t1, 0($s0) # load the int to print
li $v0, 1 # print the integer
move $a0, $t1
syscall
addi $s0, $s0, 4
addi $t0, $t0, 1
j start_print_array
exit_print_array: lw $a0, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $t0, 12($sp)
lw $t1, 16($sp)
addi $sp, $sp, 16
There are a few things going wrong here.
Firstly all of your functions are missing the terminating jr $ra.
Also, your stack manipulation is wrong. You are consistently allocating 4 less bytes than you use. If you want to put 5 words on the stack you should expand the stack by 20 not 16.
Most importantly though here is your intArray directive. You have used .word 4000 I'm guessing to allocate an array of ints, but rather you have allocated space for 1 word with the value 4000.
To instead allocate an array of 1000 ints you could use .space 4000 or equally .word 0:1000.
When I made these changes your program began to function as desired.