Calculate and Display Sum
This assembly code calculates the sum of three numbers and displays the result using system calls. The code takes advantage of x86 assembly language and Linux system calls for input and output operations.
source code
section .text
   global _start   ; Declare entry point for linker (ld)
   
_start:	
   
   mov  eax,3      ; Number of bytes to be summed (counter)
   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 the current element to the sum
   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'  ; Convert sum to ASCII character
   mov  [sum], ebx ; 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  0Code Explanation:
The provided assembly code performs the following steps:
Setup and Loop:
Registers
eax,ebx, andecxare used to manage operations and data.The loop starts with the
_startlabel and iterates over thexarray's elements to calculate the sum.
Sum Calculation:
Inside the loop, each element of the
xarray is added to theebxregister (used as a sum accumulator).The
addanddecinstructions are used to perform these operations, with the loop condition checked usingjnz(jump if not zero).
ASCII Conversion:
After the loop, the sum in the
ebxregister is converted into its ASCII character representation by adding the ASCII value of '0'.
Displaying the Result:
The
sys_writesystem call (usingint 0x80) is used to display the result on the screen.The
sys_exitsystem call is used to terminate the program.
Data Storage:
The
xarray contains the input numbers.The
sumvariable stores the ASCII representation of the calculated sum.
Example:
Let's consider the values in the x array: 2, 4, and 3.
The code will calculate the sum: 2 + 4 + 3 = 9.
The sum 9 will be converted to its ASCII character representation ('9') and displayed on the screen.
When the code is assembled and executed in a suitable x86 assembly environment, the output will be:
9Usage:
To run the code:
Assemble the code using an x86 assembly assembler, for example:
nasm -f elf file.asm -o file.oLink the object file to create an executable:
ld -m elf_i386 -s -o file file.oRun the executable:
./file
Please ensure that you have an appropriate x86 assembly environment and that you're running this code on a Linux-based system to observe the expected output.
Feel free to replace "file" in the usage commands with a suitable name for your executable.
Last updated
Was this helpful?