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 arg1, arg2 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 greater_eq_ ; A >= B
jmp label_ ; unconditional
THIS IS THE SUM.ASM FILE
section .data
array dd 1,2,3,4,5 ; Array of 532-bit integers
len equ 5 ; Array length
output db "Sum is: ",0 ; Output prefix
section .bss
result resb 12 ; Reserve space for the result string (max 10 digits + newline + null terminator)
section .text
global _start ; Entry point for the program
_start:
; Initialize variables
mov ecx, len ; Loop counter
mov eax, 0 ; Sum accumulator
mov esi, 0 ; Array index
sum_loop:
; Add current element to sum
add eax, [array + esi *4] ; 4 bytes per element
; Move to next element
inc esi
; Check loop condition
dec ecx
jnz sum_loop ; 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 +11 ; Start filling result from the end
mov byte [edi],0 ; Null terminator
dec edi
; If the sum is zero, handle it as a special case
cmp eax, 0
jne convert_to_string
mov byte [edi],'0'
jmp print_result
convert_to_string:
; Convert integer in EAX to a decimal ASCII string
.convert_loop:
mov edx, 0
mov ebx, 10
div ebx ; EAX = EAX /10, EDX = EAX %10
add dl,'0' ; 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 .convert_loop ; Continue if not zero
; Set EDI to the start of the converted number
inc edi
print_result:
; Print "Sum is: " followed by the converted number
; First, print the "Sum is: " prefix
mov eax, 4 ; sys_write
mov ebx, 1 ; file descriptor (stdout)
mov ecx, output ; pointer to message
mov edx, 8 ; message length
int 0x80 ; call kernel
; Calculate length of the result number string
mov eax, result +11 ; End of buffer
sub eax, edi ; Length of converted number
mov edx, eax ; Move length to EDX for sys_write
; Now, print the result number string
mov eax, 4 ; sys_write
mov ebx, 1 ; file descriptor (stdout)
mov ecx, edi ; pointer to converted number
int 0x80 ; call kernel
; Exit the program
mov eax, 1 ; sys_exit
xor ebx, ebx ; exit code 0
int 0x80 ; call kernel

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!