Question
Implement a MIPS assembly language program that defines main, readArray and searchElement procedures. The readArray takes an array of integers as its parameter, reads in
Implement a MIPS assembly language program that defines main, readArray and searchElement procedures. The readArray takes an array of integers as its parameter, reads in integers from a user to fill the array and also print each value as long as it is with the number of elements specified by the parameter "howMany" and "length". The searchElement procedure takes parameters of an array of integers, and its length, and asks a user how many integers to read in and calls the readArray procedure. It also asks a user to enter an integer to search. Then it should go through the array to see if each number is same as the integer to search. If an element in the array is the same as the integer to search, double its content, by multiplying it by 2, and set the variable found to 1 so that it can print out a message to say whether it was found or not. Then it prints out the updated content of the array. The main procedure asks a user how many times to call the searchElement procedure, and it repeats it based on the entered number. Please see the following C program to understand how it should work. If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it. Name your source code file assignment6.s.
The following shows how it looks like in a C program:
//The readArray procedure reads integers from user input void readArray(int array[], int length, int howMany) { int num, i = 0; while (i < howMany && i < length) { printf("Enter an integer: "); //read an integer from a user input and store it in num1 scanf("%d", &num); array[i] = num; i++; } printf("The array content: "); i = 0; while (i < howMany && i < length) { printf("%d ", array[i]); i++; } return; } //The searchElement calls readArray procedure to populate the parameter array, //then search an entered element from a user in the array. //If it is found, it doubles its value, then it prints the result array content //and also prints whether the element was found in the array or not. void searchElement(int array[], int length) { int i = 0; int howMany, search, found; printf("Specify how many numbers should be stored in the array (at most 9): "); scanf("%d", &howMany); readArray(array, length, howMany); printf("Enter an integer to search: "); scanf("%d", &search); found = 0; while (i < howMany && i < length) { if (array[i] == search) { found = 1; array[i] = array[i]*2; } i++; } printf("Result Array Content: "); i = 0; while (i < howMany && i < length) { printf("%d ", array[i]); i++; } if (found == 0) printf("The entered element was not found "); else printf("The entered element was found "); } // The main calls the searchElement // for the number of times requested by a user void main() { int arraysize = 9; int numbers[arraysize]; int i, repeat; printf("Enter an integer to specify how many times to repeat: "); scanf("%d", &repeat); for (i=0; i < repeat; i=i+1) { searchElement(numbers, arraysize); } return; }
The following is a sample output (user input is in bold):
Enter an integer to specify how many times to repeat: 2 Specify how many numbers should be stored in the array (at most 9): 8 Enter an integer: 1 Enter an integer: -12 Enter an integer: 53 Enter an integer: -4 Enter an integer: 5 Enter an integer: 32 Enter an integer: 1 Enter an integer: 7 The array content: 1 -12 53 -4 5 32 1 7 Enter an integer to search: 1 Result Array Content: 2 -12 53 -4 5 32 2 7 The entered element was found Specify how many numbers should be stored in the array (at most 9): 7 Enter an integer: 1 Enter an integer: 5 Enter an integer: 1 Enter an integer: -5 Enter an integer: -2 Enter an integer: 3 Enter an integer: 2 The array content: 1 5 1 -5 -2 3 2 Enter an integer to search: 8 Result Array Content: 1 5 1 -5 -2 3 2 The entered element was not found
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started