READ CAREFULLY BEFORE YOU POST AND WASTE POINTS! This program is written for QtS
ID: 3531276 • Letter: R
Question
READ CAREFULLY BEFORE YOU POST AND WASTE POINTS! This program is written for QtSpim only answere this is you are 100% compitent with QTSPIM. This QtSpim code is an assemlby language function for handling arrays and functions for arrays what I have completed so far is the mainbody function for arrays and then a array function for linear search scroll to the bottom to see the functionI want you to implement. This function needs to run 100% in Qtspim and needs to be implemented to run in the main function. I would like the user to be able to choose wether to do the linear search function or the count occurence function. This program right now runs the linear search perfectly i just need the Count occurence function to be made and usable. The description for this function is at the bottom in these comment marks #. If the code you make runs perfectly i will give 5 stars if not, no stars will be given. Try to stay within the same style of assembly that I have done so far. Thankyou!
.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: "
.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
jalinit_array# call init_array
move$a0, $s0
li$a1, 10
jalprint_array# call print_array
la$a0, msg2# enter a key
li$v0, 4
syscall
li$v0, 5# key stored in $v0
syscall
move$a0, $s0
li$a1, 10
move$a2, $v0
jal linear_search
move$a0, $v0# print out search result
li$v0, 1
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
jL1# 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
jL2# 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
jLSL1# go to L1
FOUND:
move$v0, $s0
jLSEXIT
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
Explanation / Answer
.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 occurrenc
es of element in array: "
msg7: .asciiz "Number of occurrences of element is: "
.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 # 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:
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_Occurrence
# Inputs:
# $a0 = beginning address of array
# $a1 = number of elements in array
# $a2 = value to search for
#
# Return values
# $v0 = number of occurrences of $a2 in array
#
# Note: By only using the "t" registers, we do not have
# to worry about saving anything on the stack
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
# we will be searching.
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.