section .data
prompt db "Enter a number: ", 0
resultMsg db "The sum is: ", 0
section .bss
num1 resb 1
num2 resb 1
result resb 1
section .text
global _start
_start:
; Print "Enter a number:"
mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor for stdout
mov ecx, prompt ; address of the prompt string
mov edx, 16 ; length of the string
int 0x80 ; invoke system call
; Read first number from user
mov eax, 3 ; syscall number for sys_read
mov ebx, 0 ; file descriptor for stdin
mov ecx, num1 ; address to store input
mov edx, 1 ; read 1 byte
int 0x80 ; invoke system call
; Convert the first ASCII input to integer
mov al, [num1] ; move the character from input to AL
sub al, '0' ; convert ASCII to integer
mov bl, al ; store the first number in bl
; Print "Enter a number:"
mov eax, 4
mov ebx, 1
mov ecx, prompt
mov edx, 16
int 0x80
; Read second number from user
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 1
int 0x80
; Convert the second ASCII input to integer
mov al, [num2]
sub al, '0'
mov cl, al ; store the second number in cl
; Add the numbers
add bl, cl ; bl = bl + cl
; Print "The sum is: "
mov eax, 4
mov ebx, 1
mov ecx, resultMsg
mov edx, 15
int 0x80
; Convert the result to ASCII and print it
add bl, '0' ; convert back to ASCII
mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor for stdout
mov ecx, result ; address of result variable
mov [result], bl ; store the result in memory
mov edx, 1 ; print 1 byte
int 0x80 ; invoke system call
; Exit the program
mov eax, 1 ; syscall number for sys_exit
xor ebx, ebx ; exit code 0
int 0x80
c2VjdGlvbiAuZGF0YQogICAgcHJvbXB0IGRiICJFbnRlciBhIG51bWJlcjogIiwgMAogICAgcmVzdWx0TXNnIGRiICJUaGUgc3VtIGlzOiAiLCAwCgpzZWN0aW9uIC5ic3MKICAgIG51bTEgcmVzYiAxCiAgICBudW0yIHJlc2IgMQogICAgcmVzdWx0IHJlc2IgMQoKc2VjdGlvbiAudGV4dAogICAgZ2xvYmFsIF9zdGFydAoKX3N0YXJ0OgogICAgOyBQcmludCAiRW50ZXIgYSBudW1iZ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