Assembly Code Snippet
  • Assembly Code Snippet
  • Assembly Program: 4-Digit Decimal Number Addition
  • ASCII Division and Display in x86 Assembly
  • ASCII Arithmetic Addition in x86 Assembly
  • BCD Addition and Display in Assembly
  • BCD Arithmetic and Display in Assembly
  • Decimal Multiplication and Display in x86 Assembly
  • Assembly Program: Determining Endianness and Displaying Messages
  • Assembly Program: Determining Even and Odd Numbers
  • Assembly Program: Displaying Messages using Syscalls
  • Assembly Program: Finding and Printing the Largest Word in a Sentence
  • Assembly Program: Finding the Largest of Three Numbers
  • Displaying "Hello World!" in x86 Assembly
  • Increment and Display Digits in Assembly
  • Input and Display User Name in Linux Assembly
  • Input and Display User Number in Linux Assembly
  • Introduction to Linux Assembly Programming with Messages
  • Assembly Program: Demonstrating Loop and Testing
  • Changing and Displaying Names in Assembly
  • Selection Sort from Minimum to Maximum
  • Assembly Program: Sentence Word Deletion
  • Sum and Read Input Numbers:
  • Two-Digit Sum and Square - Assembly Program
  • Calculate and Display Sum
  • Sorting Numerical Elements (BubbleSort)
    • Assembly Program: Sorting Numerical Elements
Powered by GitBook
On this page
  • source code
  • How It Works
  • Example
  • How to Run

Was this helpful?

Edit on GitHub

ASCII Arithmetic Addition in x86 Assembly

This assembly code is designed to showcase a simple arithmetic addition operation on ASCII-encoded digits. The program will take two ASCII digits as input, convert them to their decimal equivalents, perform addition, and then display the result.

source code

section	.text
   global _start    ; Entry point for the program
	
_start:             ; Start of the program
   mov	eax,'2'      ; Load ASCII value of '2' into EAX
   sub     eax, '0'   ; Convert ASCII to decimal
	
   mov 	ebx, '3'    ; Load ASCII value of '3' into EBX
   sub     ebx, '0'   ; Convert ASCII to decimal
	
   add 	eax, ebx    ; Calculate the sum
   add	eax, '0'     ; Convert sum to ASCII
	
   mov 	[sum], eax  ; Store the ASCII sum in memory location 'sum'
   mov	ecx,msg	    ; Load address of message into ECX
   mov	edx, len    ; Load length of message into EDX
   mov	ebx,1	    ; File descriptor (stdout)
   mov	eax,4	    ; System call number (sys_write)
   int	0x80	    ; Call kernel to display message
	
   mov	ecx,sum     ; Load address of ASCII sum into ECX
   mov	edx, 1      ; Set length to 1 (single character)
   mov	ebx,1	    ; File descriptor (stdout)
   mov	eax,4	    ; System call number (sys_write)
   int	0x80	    ; Call kernel to display sum
	
   mov	eax,1	    ; System call number (sys_exit)
   int	0x80	    ; Call kernel to exit program
	
section .data
   msg db "The sum is:", 0xA,0xD ; Message to display
   len equ $ - msg   ; Calculate length of message
   segment .bss
   sum resb 1         ; Storage for the calculated sum

How It Works

  1. The program begins by loading the ASCII values of two digits into registers.

  2. It converts the ASCII values to their corresponding decimal values by subtracting the ASCII value of '0'.

  3. The decimal values are then added together to calculate the sum.

  4. The sum is converted back to an ASCII value by adding the ASCII value of '0'.

  5. The result is displayed along with an appropriate message.

Example

For instance, let's consider the input '2' and '3':

  1. The ASCII value of '2' (50) is converted to the decimal value 2.

  2. The ASCII value of '3' (51) is converted to the decimal value 3.

  3. The decimal values 2 and 3 are added to obtain the sum of 5.

  4. The decimal value 5 is then converted back to its ASCII representation (53).

  5. The program will display: "The sum is: 5".

How to Run

To run this program:

  1. Assemble the code using an assembler like NASM:

    nasm -f elf file.asm -o file.o
  2. Link the object file to create an executable:

    ld -m elf_i386 file.o -o file
  3. Run the executable:

    ./file

Make sure you have NASM and a compatible x86 assembler installed.

PreviousASCII Division and Display in x86 AssemblyNextBCD Addition and Display in Assembly

Last updated 1 year ago

Was this helpful?