Description
Write a MIPS assembly language program that prompts for a user to enter how many floating point numbers to enter, then prompts to enter a series of floating point numbers and reads in numbers and store them in two arrays, then prints the original content of the arrays. Then it should go through each element in the arrays to see if their difference is greater than -10.0 and less than 10.0. If the conditions hold, then it swaps these numbers from two arrays. Then it should display how many sets of numbers are swapped and their changed array contents. Please see the given C program and the sample output to understand how it works.
Consult the green sheet and the chapter 3 for assembly instructions for floating point numbers. Here is one instruction that you might use:
c.lt.s $f2, $f4
bc1t Label1
Here if the value in the register $f2 is less than the value in $f4, it jumps to the Label1. If it should jump when the value in the register $f2 is NOT less than the value in $f4, then it should be:
c.lt.s $f2, $f4
bc1f Label1
To load a single precision floating point number (instead of lw for an integer), you might use:
l.s $f12, 0($t0)
To store a single precision floating point number (instead of sw for an integer), you might use:
s.s $f12, 0($t0)
To assign a constant floating point number (instead of li for an integer), you might use:
li.s $f12, 123.45
To copy a floating point number from one register to another (instead of move for an integer), you might use:
mov.s $f10, $f12
The following shows the syscall numbers needed for this assignment.
System Call System Call System Call
Number Operation Description
2 print_float $v0 = 2, $f12 = float number to be printed
4 print_string $v0 = 4, $a0 = address of beginning of ASCIIZ string
6 read_float $v0 = 6; user types a float number at keyboard; value is stored in $f0
8 read_string $v0 = 8; user types a string at keybd; addr of beginning of string is stored in $a0; len in $a1
——————————————
The following shows how it looks like in a C program:
——————————————
void main( ) { int arraysize = 11; float array1[arraysize], array2[arraysize]; int i, howMany, count; float num, temp, difference; printf("Specify how many numbers should be stored in the array (at most 11):n"); scanf("%d", &howMany); //The following loop reads in floating point numbers //and stores them into the array 1 printf("First Array:n"); i = 0; while (i < arraysize && i < howMany) { printf("Enter a number:n"); //read an integer from a user input and store it in num scanf("%f", &num); array1[i] = num; i++; } //Print out each number in the array printf("First Array Content:n"); i = 0; while (i < arraysize && i < howMany) { printf("%fn", array1[i]); i++; } //The following loop reads in floating point numbers //and stores them into the 2 printf("Second Array:n"); i = 0; while (i < arraysize && i < howMany) { printf("Enter a number:n"); //read an integer from a user input and store it in num scanf("%f", &num); array2[i] = num; i++; } //Print out each number in the array printf("Second Array Content:n"); i = 0; while (i < arraysize && i < howMany) { printf("%fn", array2[i]); i++; } //The following is checking if their difference is greater than 10.0 and less than -10.0. //If the conditions hold, then it swaps these numbers from two arrays. i = 0; while (i < arraysize && i < howMany) { difference = array1[i]-array2[i]; if (difference < 10.0 && difference > -10.0) { temp = array1[i]; array1[i] = array2[i]; array2[i] = temp; count++; } i++; } //Print how many pairs of numbers were swapped printf("%d pair(s) of number are swappedn", count); //Print the result array content, //it should not contain a same number twice other than zero. printf("First Array Result Content:n"); i = 0; while (i < arraysize && i < howMany) { printf("%fn", array1[i]); i++; } //Print the result array content, //it should not contain a same number twice other than zero. printf("Second Array Result Content:n"); i = 0; while (i < arraysize && i < howMany) { printf("%fn", array2[i]); i++; } return ; }
——————————————
Here are sample outputs (user input is in bold): — note that you might get some rounding errors, for instance, 53.3 is printed as 53.299999. Also, if a user enters a number more than 11, your program should stop reading and use only the first 11 numbers.
——————————————–
Specify how many numbers should be stored in the array (at most 11):
8
First Array:
Enter a number:
1.2
Enter a number:
-12.4
Enter a number:
53.3
Enter a number:
-4.5
Enter a number:
5.5
Enter a number:
32.9
Enter a number:
1.3
Enter a number:
7.2
First Array Content:
1.200000
-12.400000
53.299999
-4.500000
5.500000
32.900002
1.300000
7.200000
Second Array:
Enter a number:
1.1
Enter a number:
7.9
Enter a number:
1.7
Enter a number:
5.4
Enter a number:
1.7
Enter a number:
-5.6
Enter a number:
-2.1
Enter a number:
3.7
Second Array Content:
1.100000
7.900000
1.700000
5.400000
1.700000
-5.600000
-2.100000
3.700000
5 pair(s) of numbers are swapped
First Array Result Content:
1.100000
-12.400000
53.299999
5.400000
1.700000
32.900002
-2.100000
3.700000
Second Array Result Content:
1.200000
7.900000
1.700000
-4.500000
5.500000
-5.600000
1.300000
7.200000
——————————————–