Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can i modify the code i have to have the output be The array has the following values: 1 0 2 0 3 0

How can i modify the code i have to have the output be "The array has the following values: 102030405060 max: 60, min: 10, average: 35" all on one line . the current output is " The array has the following values: max:60, min: 10, average:38. as you can see the current average is off by 3 and the array values are missing. DO NOT HARDCODE THE ARRAY VALUES IN MYSTR. and the output can all be on oneline
it should compile with nasm -f elf 64 and gcc in assembly x86 in snowball
extern printf
section .data
mystr: db "The array has the following values: 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 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
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

From Herds To Insights Harnessing Data Analytics For Sustainable Livestock Farming

Authors: Prof Suresh Neethirajan

1st Edition

B0CFD6K6KK, 979-8857075487

More Books

Students also viewed these Databases questions