> For the complete documentation index, see [llms.txt](https://talaatmagdyx.gitbook.io/assembly-code-snippet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://talaatmagdyx.gitbook.io/assembly-code-snippet/ascii-arithmetic-summation.md).

# 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

```assembly
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://talaatmagdyx.gitbook.io/assembly-code-snippet/ascii-arithmetic-summation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
