Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I ONLY NEED 3&4 ANSWERED PLEASE: Write and test a MIPS program consisting of fou

ID: 3739868 • Letter: I

Question

I ONLY NEED 3&4 ANSWERED PLEASE:

Write and test a MIPS program consisting of four functions. In the following descriptions, the symbol & means “address of”.

1). void main(): The main function must 1) print your name 2) call the readData function 3) call count function 4) complete the program. The main function must set up all parameters before calling each of the functions.

2). int readData (&array): The starting address of an array is passed to the function as a parameter using $a0. The function must prompt for and read and store integers in the array until either a zero is entered or 10 numbers are read. Once the tenth integer is entered, your program must stop reading. The function must return (using $v0) the number of values read and stored in the array (10 or less). The zero input is not part of the array and must not be stored in the array. If the first input is a zero, then the array will be empty and the count returned is zero.

3). float average (&array, count): The starting address of an array (in $a0) and the number of integers stored in the array (in $a1) are passed to the function. The purpose of the function is to calculate and return (using $f0) the floating point average of the numbers. To find the average, sum all the integers together and then divide by the count as a floating point value. If the count is zero, return 0.0. Use appropriate registers for the parameters and the return value.

4). void count (&array, count): The starting address of an array (in $a0) and the number of integers stored in the array (in $a1) are passed to the function. The function determines how many of the integers in the array are greater than or equal to the average. First the parameters for the average function are set and then average function is called to determine the average value. The returned value is then printed. Then each value in the array is tested to determine if its value is greater than or equal to the average. The count of the values greater than or equal to the average is then printed.

Example input and output:

Linda Chatting
Enter a number 3
Enter a number 6
Enter a number 2
Enter a number 4
Enter a number 0
The average is 3.750000000
There are 2 numbers greater than or equal to the average.

In addition,

? Functions must be called using jal and return using jr.

? Parameters must be passed as specified and used correctly. The readData, average and count

functions must access the array through the parameter, not directly.

? Be sure to comment all instructions. All functions must include comments describing the

function and its parameters and return value (if any). Be sure that your name and class ID appear at the beginning.

Explanation / Answer

Answer:

Assembly Language Code :

1)

.LC0:

.string " Please Enter How many Number you want?"

.LC1:

.string "%d"

.LC2:

.string " Please Enter the elements one by one"

.LC3:

.string " Sum of the %d Numbers = %d"

.LC4:

.string " Average of the %d Numbers = %.2f"

main:

push rbp

mov rbp, rsp

sub rsp, 32

mov DWORD PTR [rbp-8], 0

mov edi, OFFSET FLAT:.LC0

call puts

lea rax, [rbp-16]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call __isoc99_scanf

mov edi, OFFSET FLAT:.LC2

call puts

mov DWORD PTR [rbp-4], 0

jmp .L2

.L3:

lea rax, [rbp-20]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call __isoc99_scanf

mov eax, DWORD PTR [rbp-20]

add DWORD PTR [rbp-8], eax

add DWORD PTR [rbp-4], 1

.L2:

mov eax, DWORD PTR [rbp-16]

cmp DWORD PTR [rbp-4], eax

jl .L3

mov ecx, DWORD PTR [rbp-16]

mov eax, DWORD PTR [rbp-8]

cdq

idiv ecx

cvtsi2ss xmm0, eax

movss DWORD PTR [rbp-12], xmm0

mov eax, DWORD PTR [rbp-16]

mov edx, DWORD PTR [rbp-8]

mov esi, eax

mov edi, OFFSET FLAT:.LC3

mov eax, 0

call printf

cvtss2sd xmm0, DWORD PTR [rbp-12]

mov eax, DWORD PTR [rbp-16]

mov esi, eax

mov edi, OFFSET FLAT:.LC4

mov eax, 1

call printf

nop

leave

ret

2)

.LC0:

.string " Please Enter How many Number you want?"

.LC1:

.string "%d"

.LC2:

.string " Please Enter the elements one by one"

.LC3:

.string " Sum of the %d Numbers = %d"

.LC4:

.string " Average of the %d Numbers = %.2f"

main:

push rbp

mov rbp, rsp

sub rsp, 32

mov DWORD PTR [rbp-8], 0

mov edi, OFFSET FLAT:.LC0

call puts

lea rax, [rbp-16]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call scanf

mov edi, OFFSET FLAT:.LC2

call puts

mov DWORD PTR [rbp-4], 0

.L3:

mov eax, DWORD PTR [rbp-16]

cmp DWORD PTR [rbp-4], eax

jge .L2

lea rax, [rbp-20]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call scanf

mov eax, DWORD PTR [rbp-20]

add DWORD PTR [rbp-8], eax

add DWORD PTR [rbp-4], 1

jmp .L3

.L2:

mov ecx, DWORD PTR [rbp-16]

mov eax, DWORD PTR [rbp-8]

cdq

idiv ecx

cvtsi2ss xmm0, eax

movss DWORD PTR [rbp-12], xmm0

mov eax, DWORD PTR [rbp-16]

mov edx, DWORD PTR [rbp-8]

mov esi, eax

mov edi, OFFSET FLAT:.LC3

mov eax, 0

call printf

cvtss2sd xmm0, DWORD PTR [rbp-12]

mov eax, DWORD PTR [rbp-16]

mov esi, eax

mov edi, OFFSET FLAT:.LC4

mov eax, 1

call printf

mov eax, 0

leave

ret

3)

.LC1:

.string "Enter the numbers of elements: "

.LC2:

.string "%d"

.LC3:

.string "Error! number should in range of (1 to 100)."

.LC4:

.string "Enter the number again: "

.LC5:

.string "%d. Enter number: "

.LC6:

.string "%f"

.LC7:

.string "Average = %.2f"

main:

push rbp

mov rbp, rsp

sub rsp, 416

pxor xmm0, xmm0

movss DWORD PTR [rbp-8], xmm0

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call printf

lea rax, [rbp-16]

mov rsi, rax

mov edi, OFFSET FLAT:.LC2

mov eax, 0

call scanf

jmp .L2

.L3:

mov edi, OFFSET FLAT:.LC3

call puts

mov edi, OFFSET FLAT:.LC4

mov eax, 0

call printf

lea rax, [rbp-16]

mov rsi, rax

mov edi, OFFSET FLAT:.LC2

mov eax, 0

call scanf

.L2:

mov eax, DWORD PTR [rbp-16]

cmp eax, 100

jg .L3

mov eax, DWORD PTR [rbp-16]

test eax, eax

jle .L3

mov DWORD PTR [rbp-4], 0

jmp .L4

.L5:

mov eax, DWORD PTR [rbp-4]

add eax, 1

mov esi, eax

mov edi, OFFSET FLAT:.LC5

mov eax, 0

call printf

lea rax, [rbp-416]

mov edx, DWORD PTR [rbp-4]

movsx rdx, edx

sal rdx, 2

add rax, rdx

mov rsi, rax

mov edi, OFFSET FLAT:.LC6

mov eax, 0

call scanf

mov eax, DWORD PTR [rbp-4]

cdqe

movss xmm0, DWORD PTR [rbp-416+rax*4]

movss xmm1, DWORD PTR [rbp-8]

addss xmm0, xmm1

movss DWORD PTR [rbp-8], xmm0

add DWORD PTR [rbp-4], 1

.L4:

mov eax, DWORD PTR [rbp-16]

cmp DWORD PTR [rbp-4], eax

jl .L5

mov eax, DWORD PTR [rbp-16]

cvtsi2ss xmm0, eax

movss xmm1, DWORD PTR [rbp-8]

divss xmm1, xmm0

movaps xmm0, xmm1

movss DWORD PTR [rbp-12], xmm0

cvtss2sd xmm0, DWORD PTR [rbp-12]

mov edi, OFFSET FLAT:.LC7

mov eax, 1

call printf

mov eax, 0

leave

ret