Question: ####################################################################### # Lab #4 # Name: # Date: # # # Description: # # The purpose of this lab is to help you understand dynamic
#######################################################################
# Lab #4
# Name:
# Date:
#
#
# Description:
#
# The purpose of this lab is to help you understand dynamic arrays,
# how to access a specific element in an array through its index,
# and how to iterate or search in an array.
#
# - In first part of your program, you should ask user input
# an array length (greater than 0), then use allocalte_array
# subprogram to dynamically allocate an array in heap, and
# store base address and length to static variables. After
# that, use the provided subprogram to read input from user
# and print out the array.
#
# - In second part of your program, main should use a loop
# repeatedly ask user to input a number, call subprogram
# array_contains to see whether the array contains this
# number, and print out the index of the number after
# return. The loop should end if user input -1. array_contains
# will take 3 arguments IN (array base address, array
# length, and user input number) and 1 argument OUT (index
# of the number if array contains the number, -1 if not).
#
# - In third part of your program, main should use another
# loop ask user to input an index (0 <= index < 10), and
# call print_nth to locate and print the element at that
# index. Again, loop should end if user input -1. You
# should print an error message if user input an invalid
# index. print_nth will take 2 arguments IN (array base
# address, and user input index) and no argument out.
#
# (Hint) check exit code before validate user input since
# -1 is also an invalid input.
#
# - Please notice that all register usage are recomandations
# not requirements, feel free to follow your own design.
#
#######################################################################
# Register Usage
#
# $t0
# $t1
# $t2
# $t3
# $t4
# $t5
# $t6
# $t7 temp register
# $t8 holds 10
# $t9 holds -1
#######################################################################
.data
array_prompt_p: .asciiz " Please enter array length (0 < n <=10): "
array_error_p: .asciiz "Invalid length, should be positive and no more than 10. "
number_prompt_p: .asciiz " Please enter a number to search (-1 to stop): "
not_exist_p: .asciiz "Array does not contain the number. "
exist_p: .asciiz "The index of the number is: "
index_prompt_p: .asciiz " Please enter an index less than array length (-1 to stop): "
index_error_p: .asciiz "Wrong index (0 <= index < array length). "
array_dynamic: .word 0
array_length: .word 0
#######################################################################
.text
main:
li $t9, -1 # load -1 to $t9 as condition
li $t8, 10
create_array:
li $v0, 4 # prompt user for array length
la $a0, array_prompt_p
syscall
li $v0, 5 # user input
syscall
blez $v0, invalid_length # go to invalid if input <= z
bgt $v0, $t8, invalid_length # go to invalid if input > 10
la $t7, array_length # store valid array length
sw $v0, 0($t7) # to static variable
# TODO: put array length in $a0
move $a0, $v0
# TODO: call subprogram allocate_array
jal allocate_array
# TODO: store return value ($v0) in variable array_dynamic
b read_input # go to read input if array length is valid
invalid_length:
li $v0, 4 # print error msg
la $a0, array_error_p
syscall
b create_array # branch back
read_input:
# TODO: load array base address into $a0
# TODO: load array length into $a1
# TODO: call subprogram read_array
jal read_array
# TODO: load array base address into $a0
# TODO: load array length into $a1
# TODO: call subprogram print_array
jal print_array
search_number:
li $v0, 4 # prompt user for a number
la $a0, number_prompt_p
syscall
li $v0, 5 # user input
syscall
beq $v0, $t9, search_index # branch to continue if user input -1
la $t7, array_dynamic # load array base address from
lw $a0, 0($t7) # static variable to $a0
la $t7, array_length # load array length from
lw $a1, 0($t7) # static variable to $a1
move $a2, $v0 # load user input to $a2
jal array_contains # jump to array_contains subprogram
move $t1, $v0 # move return value to $t1
bne $t1, $t9, exist # if return value != -1, branch to exist
li $v0, 4 # otherwise print not exist message
la $a0, not_exist_p
syscall
b search_number # branch back for another number
exist:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
