Read Carefully Before Posting. I will show you two functions, one is my main arr
ID: 3531564 • Letter: R
Question
Read Carefully Before Posting. I will show you two functions, one is my main array function with linear search and count occurence please run the program in Qtspim so you know what it does. Then I will post the two other functions an N factorial and an N sum function in seperate comments below, also run those so you know how it goes. What I need is for someone to take the N Factorial and the N sum function and combine them into the first larger function and by using a loop allowing the user to choose whether to do an array(linear search/occurence, which is already done) and the two new functions (Nsum or Nfact) It needs to prompt a message and print out the result. All the functions are completely done, all I need is for them to be combined. Please repost the code in your answere, once I run the code and it runs successfully only then will I rate you your 5 stars.
Main Function
.data
msg1: .asciiz "Enter 10 integers for arrays: "
msg2: .asciiz "Enter the search key: "
msg3: .asciiz "After sort, the new array is: "
msg4: .asciiz "The element is not in the array. "
msg5: .asciiz "The element's index is: "
msg6: .asciiz "Select 1 to perform linear search or 2 to count occurrences of element in array: "
msg7: .asciiz "Number of occurrences of element is: "
msg8: .asciiz " Program Exit,Goodbye"
.text
main:
li $a0, 40 # declare int a[10];
li $v0, 9
syscall
move $s0, $v0 # $s0 hold the address of a
la $a0, msg1 # print out: "Enter 10 integers:"
li $v0, 4
syscall
move $a0, $s0 # pass the array address and size
li $a1, 10
jal init_array # call init_array
move $a0, $s0
li $a1, 10
jal print_array # call print_array
la $a0, msg2 # enter a key
li $v0, 4
syscall
li $v0, 5 # key stored in $v0
syscall
move $s1, $v0 # Save key in $s1
Ask_question:
la $a0, msg6 # Ask user to decide whether to search for key or count occurrences
li $v0, 4
syscall # print it out
li $v0, 5
syscall # Get answer, store in $v0
addi $t0, $v0, -1 # If result == 1, perform linear search
beq $t0, $0, Do_linear_search
addi $t0, $v0, -2 # If result == 2, count occurrences
beq $t0, $0, Do_count_occurrences
nop
j Ask_question # Ask the question again.
Do_linear_search:
move $a0, $s0
li $a1, 10
move $a2, $s1
jal linear_search
move $t0, $v0 # Save the result of linear_search in $t0
bgez $t0, LS_Found # And if we found it, go to LS_Found
la $a0, msg4 # We didn't find it.
li $v0, 4
syscall # Print out that the element is not in the array
j LSF_end
LS_Found:
la $a0, msg5 # Print msg5
li $v0, 4
syscall
move $a0, $t0 # print out search result
li $v0, 1
syscall
LSF_end:
j program_exit
Do_count_occurrences:
move $a0, $s0
li $a1, 10
move $a2, $s1
jal Count_Occurence
move $t0, $v0 # Save result in $t0
la $a0, msg7 # print msg7
li $v0, 4
syscall
move $a0, $t0
li $v0, 1 # Print out results
syscall
program_exit:
la $a0, msg8
li $v0, 4
syscall
li $v0, 10 # exit main
syscall
init_array: # array address in $a0, array size in $a1
addi $sp, $sp, -12
sw $a0, 8($sp)
sw $a1, 4($sp)
sw $s0, 0($sp) # preserve index i
move $t0, $a0
move $t1, $a1
move $s0, $0 # set i to be 0
L1:
bge $s0, $t1, E1 # i >= array size, exit
sll $t2, $s0, 2
add $t3, $t2, $t0 # $t3 hold absolute address of a[i]
li $v0, 5
syscall
sw $v0, 0($t3) # arr[i] initialized
addi $s0, $s0, 1 # i = i+1
j L1 # loop
E1:
lw $s0, 0($sp)
lw $a1, 4($sp)
lw $a0, 8($sp)
addi $sp, $sp, 12
jr $ra
print_array: # array address in $a0, array size in $a1
addi $sp, $sp, -12
sw $a0, 8($sp)
sw $a1, 4($sp)
sw $s0, 0($sp) # preserve index i
move $t0, $a0
move $t1, $a1
move $s0, $0 # set i to be 0
L2:
bge $s0, $t1, E2 # i >= array size, exit
sll $t2, $s0, 2
add $t3, $t2, $t0 # $t3 hold absolute address of a[i]
lw $a0, 0($t3)
li $v0, 1 # print int
syscall
li $a0, 32 # print out a space
li $v0, 11
syscall
addi $s0, $s0, 1 # i = i+1
j L2 # loop
E2:
lw $s0, 0($sp)
lw $a1, 4($sp)
lw $a0, 8($sp)
addi $sp, $sp, 12
jr $ra
linear_search:
addi $sp, $sp, -4 # preserve memory
sw $s0, 0($sp)
move $s0, $0 # initialize index to be 0
LSL1:
bge $s0, $a1, NOT_FOUND # if index is >= size, exit
sll $t0, $s0, 2
add $t1, $a0, $t0
lw $t2, 0($t1) #$t2 = a[i]
beq $t2, $a2, FOUND
addi $s0, $s0, 1 # increase index by 1
j LSL1 # go to L1
FOUND:
move $v0, $s0
j LSEXIT
NOT_FOUND:
li $v0, -1 # place -1 to $v0
LSEXIT:
lw $s0, 0($sp)
addi $sp, $sp, 4 # restore the memory
jr $ra # return
Count_Occurence: # Count occurence function with three inputs,
# an int array, array size, and a key
# This function returns the number of
# times a key occurs in an array return not
# found if count = 0
move $t0, $0 # $t0 is our count
move $t1, $a0 # $t1 is a pointer to the beginning of array
sll $t2, $a1, 2 # $t2 = $a2 * 4 - this is the number of bytes
COL1:
lw $t3, ($t1) # Load next word in array
addi $t1, $t1, 4 # Increment array pointer by 4
seq $t4, $t3, $a2 # $t4 = 1 if (and only if) the element is equal to $a2
add $t0, $t0, $t4 # Increment our counter
subu $t5, $t1, $a0 # $t5 = number of bytes we've checked
blt $t5, $t2, COL1 # If we've not checked $t2 bytes, loop
move $v0, $t0 # Store our result in $v0
jr $ra # And return
Explanation / Answer
Where is the code for factorial ans sum??
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.