Answered step by step
Verified Expert Solution
Link Copied!

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 38 but it should be 35. here is my code
extern printf
section .data
messstr: db "The array has the following values: ",0
arrystr: db "%d ",0
mystr: db "Max: %d, Min: %d, Average: %d",0xA,0 ; String format to use (decimal), followed by newline
myarr: dd 10,20,30,40,50,60
temp: dd 0
array_size equ ($ - myarr)/4
min_val: dd 0
max_val: dd 0
sum_val: dd 0
section .text
global main
main:
; Initialize minimum and maximum with the first value of the array
mov rdi,messstr
mov rax,0
call printf
mov eax, 0
mov ecx, myarr
mov edx, DWORD [ecx]
mov [min_val], edx
mov [max_val], edx
; Loop to find min, max, and sum
xor eax, eax ; A =0
mov ecx, myarr ; C points to myarr
myloopp:
mov ebx, DWORD [ecx+4*eax] ; Get the value B = myarr[A]
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, 0
call printf
pop rcx
pop rax
add eax, 1 ; A++
cmp eax, 6 ; Does A ==6?
jl myloopp ; if less, jump to myloop
mov rax, 0
myloop:
mov ebx, DWORD [ecx +4* eax] ; Get the value B = myarr[A]
mov [temp], ebx
; Update minimum value
cmp ebx, [min_val]
jge not_min
mov [min_val], ebx
not_min:
; Update maximum value
cmp ebx, [max_val]
jle not_max
mov [max_val], ebx
not_max:
; Update sum value
add [sum_val], ebx
add eax, 1 ; A++
cmp eax, array_size ; Does A == array_size?
jl myloop ; if less, jump to myloop
; Calculate average
mov eax, [sum_val]
mov ebx, array_size
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, [max_val] ; Maximum value to print
mov rdx,[min_val] ; Minimum value to print
mov rcx, rax ; Average value to print
call printf
mov rsp, rbp ; Reset rsp
pop rbp ; Restore rbp
mov eax, 0
ret

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions