Question
Implement a MIPS assembly language program that defines main, readArray, printArray, and exchangeElements procedures/functions. The readArray takes an array of integers as its parameter, then
Implement a MIPS assembly language program that defines main, readArray, printArray, and exchangeElements procedures/functions. The readArray takes an array of integers as its parameter, then reads in integers from a user to fill the array. The printArray takes an array of integers as its parameter, prints each integer of the array. The exchangeElements procedure/function takes parameters of two arrays of integers, asks a user to specify how many integers to read, then calls the readArray function twice for each of two arrays, then exchanges two elements from two arrays when the index is even. (Please see the C program below), then print out the result contents for the two arrays. It also returns how many numbers are exchanged. The main procedure/function calls exchangeElements function and print out the number of elements exchanged. 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.
You can create an array in the following way:
numbers1: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
numbers2: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
The following shows how it looks like in a C program:
//The readArray procedure reads integers from user input and store them in the array void readArray(int array[], int length, int howMany, int number) { 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 %d content: ", number); i = 0; while (i < howMany && i < length) { printf("%d ", array[i]); i++; } return; } //The printArray procedure prints integers of the array void printArray(int array[], int length, int howMany, int number) { int i; printf(" Result array %d Content: ", number); i = 0; while (i < howMany && i < length) { printf("%d ", array[i]); i++; } return; } //The exchangeElements calls readArray procedure to populate two arrays, //then exchange elements from two arrays at even indexes, then print the result arrays. int exchangeElements(int array1[], int array2[], int length) { int i, temp; int howMany, count; printf("Specify how many numbers should be stored in the arrays (at most 11): "); scanf("%d", &howMany); readArray(array1, length, howMany, 1); readArray(array2, length, howMany, 2); i = 0; count = 0; //exchange two elements from two arrays at even indexes while (i < howMany && i < length) { if (i%2 == 0) { temp = array1[i]; array1[i] = array2[i]; array2[i] = temp; count++; } i++; } printArray(array1, length, howMany, 1); printArray(array2, length, howMany, 2); return count; } // The main calls the exchangeElements void main() { int arraysize = 11; int numbers1[arraysize], numbers2[arraysize]; int number = exchangeElements(numbers1, numbers2, arraysize); //print out how many elements are exchanged printf("%d element(s) exchanged ", number); return; }
The following is a sample output (user input is in bold):
Specify how many numbers should be stored in the arrays (at most 11): 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 1 content: 1 -12 53 -4 5 32 1 7 Enter an integer: 1 Enter an integer: 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 The array 2 content: 1 7 1 5 1 -5 -2 3 Result array 1 Content: 1 -12 1 -4 1 32 -2 7 Result array 2 Content: 1 7 53 5 5 -5 1 3 4 element(s) exchanged
Each procedure/function needs to have a header using the following format:
############################################################################ # Procedure/Function readArray # Description: ----- # parameters: $a0 = address of array1 , $a1 = addres of array2, $a3 = length # return value: $v1 = count # registers to be used: $s3 and $s4 will be used. ############################################################################
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