Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .data
- prompt1 db 'Enter first number: ', 0
- prompt1_len equ $ - prompt1
- prompt2 db 'Enter second number: ', 0
- prompt2_len equ $ - prompt2
- result_msg db 'The sum is: ', 0
- result_msg_len equ $ - result_msg
- newline db 0xa
- section .bss
- num1 resb 6 ; Reserve 6 bytes for first number string
- num2 resb 6 ; Reserve 6 bytes for second number string
- sum resb 6 ; Reserve 6 bytes for sum string
- section .text
- global _start
- _start:
- ; Print first prompt
- mov eax, 4
- mov ebx, 1
- mov ecx, prompt1
- mov edx, prompt1_len
- int 0x80
- ; Read first number
- mov eax, 3
- mov ebx, 0
- mov ecx, num1
- mov edx, 6
- int 0x80
- ; Print second prompt
- mov eax, 4
- mov ebx, 1
- mov ecx, prompt2
- mov edx, prompt2_len
- int 0x80
- ; Read second number
- mov eax, 3
- mov ebx, 0
- mov ecx, num2
- mov edx, 6
- int 0x80
- ; Convert string to integer and add
- mov esi, num1
- call atoi ; Convert first number to integer (result in eax)
- mov ebx, eax ; Save first number in ebx
- mov esi, num2
- call atoi ; Convert second number to integer (result in eax)
- add eax, ebx ; Add the numbers
- ; Convert result back to string
- mov esi, sum
- call itoa
- ; Print result message
- mov eax, 4
- mov ebx, 1
- mov ecx, result_msg
- mov edx, result_msg_len
- int 0x80
- ; Print sum
- mov eax, 4
- mov ebx, 1
- mov ecx, sum
- mov edx, 6
- int 0x80
- ; Print newline
- mov eax, 4
- mov ebx, 1
- mov ecx, newline
- mov edx, 1
- int 0x80
- ; Exit program
- mov eax, 1
- mov ebx, 0
- int 0x80
- ; Function to convert ASCII string to integer
- atoi:
- xor eax, eax ; Zero out eax
- xor ebx, ebx ; Zero out ebx
- .next_digit:
- mov bl, [esi] ; Get next character
- cmp bl, 0xa ; Check for newline
- je .done
- cmp bl, 0x20 ; Check for space
- je .done
- sub bl, '0' ; Convert ASCII to number
- imul eax, 10 ; Multiply current number by 10
- add eax, ebx ; Add new digit
- inc esi ; Move to next character
- jmp .next_digit
- .done:
- ret
- ; Function to convert integer to ASCII string
- itoa:
- push esi ; Save esi
- add esi, 5 ; Point to end of buffer
- mov byte [esi], 0xa ; Add newline at end
- mov ebx, 10 ; Divisor
- .next_digit:
- dec esi ; Move back one position
- xor edx, edx ; Clear edx for division
- div ebx ; Divide eax by 10
- add dl, '0' ; Convert remainder to ASCII
- mov [esi], dl ; Store ASCII digit
- test eax, eax ; Check if quotient is zero
- jnz .next_digit ; If not zero, continue
- pop esi ; Restore esi
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement