mail@pastecode.io avatar
a year ago
1.8 kB
section .data
    prompt1 db 'Enter the first number : ',0 ;25
    prompt2 db ' Enter the second number : ',0 ;27
    prompt3 db ' Sum is -> ',0 ;11
    num1   dd 0   ; First number
    num2   dd 0   ; Second number
    result dd 0   ; Result of the addition

section .text
    global _start

    ;prompt for first number
    mov eax,4 ;write
    mov ebx,1 ;stdout
    mov ecx,prompt1 ; starts printing the character train
    mov edx,25 ;length of the train
    int 0x80

    ; Read the first number
    mov eax, 3     ; System call number for read
    mov ebx, 0     ; File descriptor (stdin)
    mov ecx, num1  ; Memory location to store the input
    mov edx, 4     ; Number of bytes to read
    int 0x80       ; Call the kernel to read input

    mov eax,[num1]
    sub eax,48
    mov [num1],eax

    ;prompt for second number
    mov eax,4
    mov ebx,1
    mov ecx,prompt2
    mov edx,27
    int 0x80
    ; Read the second number
    mov eax, 3
    mov ebx, 0
    mov ecx, num2
    mov edx, 4
    int 0x80
    mov eax,[num2]
    sub eax,48
    mov [num2],eax

    ; Add the two numbers
    mov eax, [num1]
    add eax, [num2]
    mov [result], eax

    ;prompt for result
    mov eax,4
    mov ebx,1
    mov ecx,prompt3
    mov edx,11
    int 0x80
    mov eax,[result]
    add eax,48
    mov [result],eax
    ; Display the result
    mov eax, 4     ; System call number for write
    mov ebx, 1     ; File descriptor (stdout)
    mov ecx, result ; Memory location of the result
    mov edx, 1     ; Number of bytes to write
    int 0x80       ; Call the kernel to write output

    ; Exit the program
    mov eax, 1     ; System call number for exit
    xor ebx, ebx   ; Exit code 0
    int 0x80       ; Call the kernel to exit