Adding Two Numbers in GNU Assembler

This example demonstrates how to add two numbers using GNU Assembler.

        ; Example program to add two numbers using GNU Assembler
        ; Input: a, b (memory locations)
        ; Output: sum at memory location c
        
        .section .text 
        .align 4
        mov     $0, %eax      ; Initialize sum to 0
        mov     $0, %ebx      ; Initialize a to 0
        mov     $0, %edi      ; Initialize b to 0
        
        .loop:
        cmp     %ebx, $0      ; Check if a is zero
        jeq     .zero         ; If a is zero, break loop
        mov     %ebx, %ecx    ; Copy a to ecx
        mov     $0, %edx      ; Initialize b to 0
        add     %ecx, %edx    ; Add a to b
        add     %edx, %eax    ; Add sum to a
        jmp     .loop
        
        .zero:
        mov     %eax, %ebx    ; Move sum to a
        mov     $0, %eax      ; Set a to 0
        
        .exit:
        mov     %ebx, %eax    ; Move a to ebx
        mov     $1, %al       ; Set return value to 1
        stosb                        ; Store byte at address 0x30
        xor     %al, %al       ; Clear AL
        ret
    
        .section .data
        .glob   _start
        _start:
        call    .exit
        mov     $1, %rax      ; Set exit code to 1
        mov     %rax, %rdi    ; Pass to sys_exit
        mov     $0, %r8d      ; Set RAX to 0
        syscall
        

Note: This example uses GNU Assembler syntax and assumes specific memory addresses for variables.

        ; Sample usage:
        ; mov $0x0000, %ebx
        ; mov $0x0001, %edi
        ; call .exit
        ; mov %ebx, %eax
        ; mov $1, %al
        ; stosb
        ; xor %al, %al
        ; ret
        

Key Concepts: