Question: Modify the array sum.asm file to find the largest number in the array. You do this by using the cmp arg 1 , arg 2
Modify the array sum.asm file to find the largest number in the array. You do this by using the cmp arg arg command. This command compares the two values by subtracting them but it does not actually change anything Instead, the results are stored in the flags register for use by a jmp command. You would use the jump command to store the larger number in a variable with each iteration of the loop. There is an example of how to use the jump commands to do this is below. Hint: You may need more registers to do this. How to use jump commands for conditions:
cmp A B
; you will not use all these commands and you must use them in the order you choose
jg greater ; A B
jle less ; A B
je equal ; A B
jge greatereq ; A B
jmp label ; unconditional
THIS IS THE SUM.ASM FILE
section data
array dd ; Array of bit integers
len equ ; Array length
output db "Sum is: ; Output prefix
section bss
result resb ; Reserve space for the result string max digits newline null terminator
section text
global start ; Entry point for the program
start:
; Initialize variables
mov ecx, len ; Loop counter
mov eax, ; Sum accumulator
mov esi, ; Array index
sumloop:
; Add current element to sum
add eax, array esi ; bytes per element
; Move to next element
inc esi
; Check loop condition
dec ecx
jnz sumloop ; Continue if not zero
; At this point, EAX contains the sum
; Convert EAX the sum to a string and store in 'result'
mov edi, result ; Start filling result from the end
mov byte edi ; Null terminator
dec edi
; If the sum is zero, handle it as a special case
cmp eax,
jne converttostring
mov byte edi
jmp printresult
converttostring:
; Convert integer in EAX to a decimal ASCII string
convertloop:
mov edx,
mov ebx,
div ebx ; EAX EAX EDX EAX
add dl ; Convert remainder to ASCII
mov edi dl ; Store ASCII character
dec edi ; Move backwards in result
test eax, eax ; Check if EAX is zero
jnz convertloop ; Continue if not zero
; Set EDI to the start of the converted number
inc edi
printresult:
; Print "Sum is: followed by the converted number
; First, print the "Sum is: prefix
mov eax, ; syswrite
mov ebx, ; file descriptor stdout
mov ecx, output ; pointer to message
mov edx, ; message length
int x ; call kernel
; Calculate length of the result number string
mov eax, result ; End of buffer
sub eax, edi ; Length of converted number
mov edx, eax ; Move length to EDX for syswrite
; Now, print the result number string
mov eax, ; syswrite
mov ebx, ; file descriptor stdout
mov ecx, edi ; pointer to converted number
int x ; call kernel
; Exit the program
mov eax, ; sysexit
xor ebx, ebx ; exit code
int x ; call kernel
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
