Question: Write subprograms allocate_array, array_contains, and print_nth in MIPS(with instructions) ####################################################################### ####################################################################### # allocate_array # # This subprogram will take 1 arguments IN: # - array
Write subprograms allocate_array, array_contains, and print_nth in MIPS(with instructions)
#######################################################################
#######################################################################
# allocate_array
#
# This subprogram will take 1 arguments IN:
# - array length (n)
# and 1 arguments OUT:
# - array base address
#
# it will dynamically allocate an array in heep which has enough
# space for n integers (4n bytes)
#######################################################################
# Arguments IN and OUT
#
# $a0 array length
# $a1
# $a2
# $a3
# $v0 array base address
# $v1
#######################################################################
# Register Usage
#
# $t0 array length
# $t1
# $t2
# $t3
# $t4
# $t5
# $t6
# $t7
# $t8
# $t9
#######################################################################
.data
#######################################################################
.text
allocate_array:
# TODO: write subprogram
jr $ra # return to main
#######################################################################
#######################################################################
# read_array
#
# This subprogram will take 2 arguments IN and no argument OUT:
# - array base address
# - array length
#
# it will keep asking user inputs to fill out the entire array.
# input should be non-negative. (This subprogram is provided)
#######################################################################
# Arguments IN and OUT
#
# $a0 array base address
# $a1 array length
# $a2
# $a3
# $v0
# $v1
#######################################################################
# Register Usage
#
# $t0 array base address
# $t1 array length
# $t2
# $t3
# $t4
# $t5
# $t6
# $t7
# $t8
# $t9
#######################################################################
.data
read_array_prompt_p: .asciiz "Please enter a number: "
read_array_error_p: .asciiz "Input should be non_negative. "
#######################################################################
.text
read_array:
# TODO: get array base address and length from $a0 and $a1 into $t0 and $t1
read_array_input:
blez $t1, read_array_end # end loop if counter $t1
li $v0, 4 # print prompt
la $a0, read_array_prompt_p
syscall
li $v0, 5 # $v0
syscall
bgtz $v0, read_array_valid # to go valid if input >= 0
li $v0, 4 # otherwise print error msg
la $a0, read_array_error_p
syscall
b read_array_input # branch back to input
read_array_valid:
sw $v0, 0($t0) # store user input into array
addi $t0, $t0, 4 # update array pointer
addi $t1, $t1, -1 # update counter
b read_array_input # branch back to input
read_array_end:
jr $ra # return to main
#######################################################################
#######################################################################
# print_array
#
# This subprogram will take 2 arguments IN and no argument OUT:
# - array base address
# - array length
#
# it will print out the entire array
#
#######################################################################
# Arguments IN and OUT
#
# $a0 array base address
# $a1 array length
# $a2
# $a3
# $v0
# $v1
#######################################################################
# Register Usage
#
# $t0 array base address
# $t1 array length
# $t2
# $t3
# $t4
# $t5
# $t6
# $t7
# $t8
# $t9
#######################################################################
.data
print_array_p: .asciiz " Array: "
print_array_comma_p: .asciiz ", "
print_array_newline_p: .asciiz " "
#######################################################################
.text
print_array:
# TODO: get array base address and length from $a0 and $a1 into $t0 and $t1
li $v0, 4 # print "Array: "
la $a0, print_array_p
syscall
print_array_loop:
blez $t1, print_array_end # end loop if counter $t1
li $v0, 1 # print integer
lw $a0, 0($t0) # load current element from array
syscall
li $v0, 4 # print tab
la $a0, print_array_comma_p
syscall
addi $t0, $t0, 4 # update array pointer
addi $t1, $t1, -1 # update counter
b print_array_loop # branch back to loop
print_array_end:
li $v0, 4 # print newline char
la $a0, print_array_newline_p
syscall
jr $ra # return to main
#######################################################################
#######################################################################
# search_array
#
# This subprogram will take 3 arguments IN and 1 argument OUT:
# - array base address (argument IN)
# - array length (argument IN)
# - search number (argument IN)
# - index of the number (argument OUT)
#
# It will iterate through the array to see whether the array
# contains the passed in number.
# - If yes, this subprogram will return the index of that
# number
# - If no, this subprogram will return -1.
#
#######################################################################
# Arguments IN and OUT
#
# $a0 array base address
# $a1 array length
# $a2 user input number
# $a3
# $v0 index of the number, -1 if not exist
# $v1
#######################################################################
# Register Usage
#
# $t0 base address
# $t1 array length
# $t2 user input number
# $t3 counter
# $t4 current element
# $t5
# $t6
# $t7
# $t8
# $t9 counter
#######################################################################
.data
#######################################################################
.text
array_contains:
# TODO: write subprogram
jr $ra # return to main
#######################################################################
#######################################################################
# print_nth
#
# This subprogram will take 2 arguments IN and no argument OUT:
# - array base address
# - user input index
#
# it will calculate the address of the element at that index
# using equation: address = base address + index * size
# then load the element from memory and print to console.
#######################################################################
# Arguments IN and OUT
#
# $a0 array base address
# $a1 user input index
# $a2
# $a3
# $v0
# $v1
#######################################################################
# Register Usage
#
# $t0 array base address
# $t1 user input index
# $t2 temp register to calculate address
# $t3 element at the index
# $t4
# $t5
# $t6
# $t7
# $t8
# $t9
#######################################################################
.data
print_nth_p: .asciiz "The number at index "
print_nth_is_p: .asciiz " is: "
#######################################################################
.text
print_nth:
# TODO: write subprogram
jr $ra # return to main
#######################################################################
Write subprograms allocate array, array contains and print_nth read_array and print_array have been (mostly) completed for you -- you just need to write the code to retrieve the arguments IN and put them in the proper registers (hint: look at the TODO comments) Write the calls to allocate_array, read array, and print_array in main The other calls have been done for you -- you should put the necessary code by the relevant TODO sections in main . Store the base address and the length obtained from allocate array into the provided integer variables array_contains does not have to be an efficient search algorithm . Return the first instance of the number if it is found, or -1 if it is not in the array print_nth should return the number found at the provided index You can assume the given index is a valid index -- the logic to handle invalid indices is already provided for you in main Arrays in MIPS start at "index"O RECALL: given an array base address in a register (say Sto), the format to obtain a value from a memory address is as follows: lw $t1, Stos Where $t1 contains the value in the array once the instruction has been executed. We cannot replace the offset 0 with a register, as this will give a syntax error, so you will need to alter the base address itself To find the correct location, you will need to multiply the index with the number of bytes each integer takes up in memory (4), and add that to the base address to get the correct position Write subprograms allocate array, array contains and print_nth read_array and print_array have been (mostly) completed for you -- you just need to write the code to retrieve the arguments IN and put them in the proper registers (hint: look at the TODO comments) Write the calls to allocate_array, read array, and print_array in main The other calls have been done for you -- you should put the necessary code by the relevant TODO sections in main . Store the base address and the length obtained from allocate array into the provided integer variables array_contains does not have to be an efficient search algorithm . Return the first instance of the number if it is found, or -1 if it is not in the array print_nth should return the number found at the provided index You can assume the given index is a valid index -- the logic to handle invalid indices is already provided for you in main Arrays in MIPS start at "index"O RECALL: given an array base address in a register (say Sto), the format to obtain a value from a memory address is as follows: lw $t1, Stos Where $t1 contains the value in the array once the instruction has been executed. We cannot replace the offset 0 with a register, as this will give a syntax error, so you will need to alter the base address itself To find the correct location, you will need to multiply the index with the number of bytes each integer takes up in memory (4), and add that to the base address to get the correct position
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
