Answered step by step
Verified Expert Solution
Question
1 Approved Answer
what is causing my average to be calculated wrong? my average prints out as 3 8 but it should be 3 5 . here is
what is causing my average to be calculated wrong? my average prints out as but it should be here is my code
extern printf
section data
messstr: db "The array has the following values:
arrystr: db d
mystr: db "Max: d Min: d Average: dxA ; String format to use decimal followed by newline
myarr: dd
temp: dd
arraysize equ $ myarr
minval: dd
maxval: dd
sumval: dd
section text
global main
main:
; Initialize minimum and maximum with the first value of the array
mov rdi,messstr
mov rax,
call printf
mov eax,
mov ecx, myarr
mov edx, DWORD ecx
mov minval edx
mov maxval edx
; Loop to find min, max, and sum
xor eax, eax ; A
mov ecx, myarr ; C points to myarr
myloopp:
mov ebx, DWORD ecxeax ; Get the value B myarrA
mov temp ebx
push rax
push rcx
; Now print the result out
mov rdi, arrystr ; Format of the string to print
mov rsi, temp ; Value to print
mov rax,
call printf
pop rcx
pop rax
add eax, ; A
cmp eax, ; Does A
jl myloopp ; if less, jump to myloop
mov rax,
myloop:
mov ebx, DWORD ecx eax ; Get the value B myarrA
mov temp ebx
; Update minimum value
cmp ebx, minval
jge notmin
mov minval ebx
notmin:
; Update maximum value
cmp ebx, maxval
jle notmax
mov maxval ebx
notmax:
; Update sum value
add sumval ebx
add eax, ; A
cmp eax, arraysize ; Does A arraysize?
jl myloop ; if less, jump to myloop
; Calculate average
mov eax, sumval
mov ebx, arraysize
cdq
idiv ebx ; Divide sum by number of values
; Now print the result out
push rbp ; Preserve rbp
mov rbp rsp ; Set rbp as stack frame pointer
mov rdi, mystr ; Format of the string to print
mov rsi, maxval ; Maximum value to print
mov rdxminval ; Minimum value to print
mov rcx rax ; Average value to print
call printf
mov rsp rbp ; Reset rsp
pop rbp ; Restore rbp
mov eax,
ret
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