In: Computer Science
In this assignment we will be working with both text strings and a user input integer array from the data segment. The goal of the assignment is to have the user choose the number of items that they want to have in an array (we will keep it a small number for now, 1-10) and then enter those values which we will store into our array. We then want to allow them to search for a specific item in the list of number that they just entered and report if the number was found or not. Requirements:
• Name your program P02LastNameFirstName.asm
• You must make use of the data segment in order to both have strings to prompt the user and to have a location to store the values the user inputs.
• String prompts and information about the program must be given to the user in a well formatted and logical fashion so that someone using the program with no knowledge about it would understand what is happening.
• The user must be allowed to choose the number of items they are going to input, though we will limit it to a smaller number (1-10).
• The user can then enter their integers, which must be stored in the array defined in the data segment.
• You will then prompt the user for an integer to search for and then search through the input array to see if it is present. • If it is, report success. If it is not found, report failure.
Hints: • Remember that the data segment holds the larger pieces of data and the text segment has the instructions that will be run. • You can define an area for more data than you use. • Using “.align #” in the data segment can help if your array is not aligned on memory boundaries (an error you may encounter). The # refers to the boundary you want to align on, check the tooltip in MARS for more information. The value 2 is the likely choice. • Keep in mind that since we are working with integers, that we will need to move through our array with their size in mind.
SOULTION:
Program
#Data declaration part
.data
#In formation display about program
info: .asciiz "User Manual:\n This program allows user to enter
number of items in an array of integers.\n Then allow to enter
integers of that count into array.\n Maximum items are 1-10.\n The
user can search for the items\n\n
#Array to store items
intArr: .word 0 0 0 0 0 0 0 0 0 0
#Prompt for item
prompt1: .asciiz "Enter the item count(1-10): "
#Get integers
prompt2: .asciiz "\nEnter integers: "
#Search prompt
prompt3: .asciiz "\nEnter number to search: "
#Outputs
out1: .asciiz " Found in array!!!\n"
out2: .asciiz " Not Found in array!!!\n"
#Error messages
err1: .asciiz "Item count must be 1-10!!!Re-enter
please...\n\n"
#Program starts here
.globl main
.text
main:
#display user manual
la $a0,info
li $v0,4
syscall
#Get address of the array
la $s0,intArr
#Call function to get number of items going to enter in the
array
jal getCount
#Store count in s1
move $s1,$v0
#Get items into array
#Pass arguments to function
move $a0,$s0
move $a1,$s1
jal inputArray
#Search function call
move $a0,$s0
move $a1,$s1
jal searchArray
#End of the program
li $v0,10
syscall
#Implementation of function to get number of items going to
enter in the array
getCount:
#Display prompt
la $a0,prompt1
li $v0,4
syscall
#Read count
li $v0,5
syscall
#Check error conditions
blt $v0,1,error1
bgt $v0,10,error1
#Otherwise return count to main
jr $ra
#Display error
error1:
la $a0,err1
li $v0,4
syscall
j getCount
#Implementation of inputting array integers
inputArray:
#Array address in to
move $t0,$a0
#Inputting loop
loopInput:
#Condition
beq $a1,0,retMain
#Display prompt
la $a0,prompt2
li $v0,4
syscall
#Read value
li $v0,5
syscall
#Store into array
sw $v0,($t0)
#increment address
addi $t0,$t0,4
#Decrement count
addi $a1,$a1,-1
#repeat
j loopInput
#Return to main
retMain:
jr $ra
#Implementation of search function
searchArray:
#Array address in to
move $t0,$a0
#Get element to search
la $a0,prompt3
li $v0,4
syscall
#read number
li $v0,5
syscall
#Inputting loop
loopInput1:
#Condition
beq $a1,0,failure
#Get array element
lw $t1,($t0)
#compare
beq $t1,$v0,success
#increment address
addi $t0,$t0,4
#Decrement count
addi $a1,$a1,-1
#repeat
j loopInput1
#Success message
success:
move $a0,$v0
li $v0,1
syscall
la $a0,out1
li $v0,4
syscall
jr $ra
#Failure message
failure:
move $a0,$v0
li $v0,1
syscall
la $a0,out2
li $v0,4
syscall
jr $ra
Output
User Manual: This program allows user to enter number of items
in an array of integers.
Then allow to enter integers of that count into array.
Maximum items are 1-10.
The user can search for the items
Enter the item count(1-10): 0
Item count must be 1-10!!!Re-enter please...
Enter the item count(1-10): 11
Item count must be 1-10!!!Re-enter please...
Enter the item count(1-10):
User Manual:
This program allows user to enter number of items in an array of
integers.
Then allow to enter integers of that count into array.
Maximum items are 1-10.
The user can search for the items
Enter the item count(1-10): 11
Item count must be 1-10!!!Re-enter please...
Enter the item count(1-10): 0
Item count must be 1-10!!!Re-enter please...
Enter the item count(1-10): 4
Enter integers: 25
Enter integers: 14
Enter integers: 36
Enter integers: 10
Enter number to search: 10
10 Found in array!!!
-- program is finished running --
OUTPUT:
THANKU YOU