section .text global _start ;must be declared for linker (ld) _start: mov eax,3
ID: 3700054 • Letter: S
Question
section .text
global _start ;must be declared for linker (ld)
_start:
mov eax,3 ;number bytes to be summed
mov ebx,0 ;EBX will store the sum
mov ecx, x ;ECX will point to the current element to be summed
top: add ebx, [ecx]
add ecx,1 ;move pointer to next element
dec eax ;decrement counter
jnz top ;if counter not 0, then loop again
done:
add ebx, '0'
mov [sum], ebx ;done, store result in "sum"
display:
mov edx,1 ;message length
mov ecx, sum ;message to write
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
global x
x:
db 2
db 4
db 3
sum:
db 0
Click here to compile and execute assembly code.
Change the program to display F as a sum of the array.
Explanation / Answer
section .data
arr dd 1,2,3,4 ;given array
len equ $-arr ;length of array
sum dd 0 ;variable to store sum
disp_int db "%d",0,10 ;for display using printf
section .text
extern printf ;for getting printf function
global main ;declaring scope of main
main:
xor edx,edx ;to make it zero
mov ebx,4 ;to get length by dividing eax
mov eax,len ;stroing length in eax
div ebx
mov ebx,0 ;sum will be stored here
mov ecx,arr ;address of array
top:
add ebx, [ecx] ;Value in the array is added to ebx
add ecx,4 ;move pointer to next element
dec eax ;decrement counter
jnz top ;if counter not 0, then loop again
mov [sum], ebx ;done, store result in "sum"
display:
push dword[sum] ;Value in sum is pushed onto stack
push disp_int
call printf ;Printf is call
add esp,8 ;Stack gets emptied
ret
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.