Question Description
I’m working on a c++ question and need a sample draft to help me study.
Objectives:
-write assembly language programs to:
-perform arithmetic on floating point numbers
-use syscall operations to display floating point numbers and strings on the console window
-use syscall operations to read floating point numbers from the keyboard.
Assignment 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 an array, then prints the original content of the array. Then it should go through each element in the array to see which numbers are the smaller ones among them, and change them to 1000.0, and print them. After that, all numbers that are less than 1000.0 will be shifted to the beginning of the array resulting all 1000.0s will be shifted towards the end of the array, and it should print them again. 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:
——————————————
int main( ) { int howMany = 0, arraysize = 12, length; float array[arraysize]; int i = 0, j = 0; float num, temp; printf("Specify how many numbers should be stored in the array (at most 12):n"); scanf("%d", &howMany); //find the smaller number if (arraysize < howMany) length = arraysize; else length = howMany; //this loop is to read in multiple numbers from a user //and store them in an array while (i < length) { printf("Enter a number:n"); scanf("%f", &num); array[i] = num; i++; } //print out the array conent printf("The array contains the following:n"); i = 0; while (i < length) { printf("%fn", array[i]); i++; } for (i = 0; i < length/2; i++) { int index = 0; for (j = 0; j < length; j++) { if(array[index] > array[j]) { index =j; } } array[index] = 1000.0; } //print out the array conent printf("The array currently contains the following:n"); i = 0; while (i < length) { printf("%fn", array[i]); i++; } j = -1; for(int i = 0; i < length; i++){ if(array[i] < 1000.0){ j = j+1; temp = array[j]; array[j] = array[i]; array[i] = temp; } } //print out the array conent printf("The result array contains the following:n"); i = 0; while (i < arraysize && i < howMany) { printf("%fn", array[i]); i++; } return ; }
——————————————
Here are sample outputs (user input is in bold): — note that you might get some rounding errors.
——————————————–
Specify how many numbers should be stored in the array (at most 12):
8
Enter a number:
1.2
Enter a number:
-12.4
Enter a number:
53.3
Enter a number:
-12.4
Enter a number:
5.5
Enter a number:
53.3
Enter a number:
-12.4
Enter a number:
7.2
The array contains the following:
1.200000
-12.400000
53.299999
-12.400000
5.500000
53.299999
-12.400000
7.200000
The array currently contains the following:
1000.000000
1000.000000
53.299999
1000.000000
5.500000
53.299999
1000.000000
7.200000
The result array contains the following:
53.299999
5.500000
53.299999
7.200000
1000.000000
1000.000000
1000.000000
1000.000000