Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .data
- prompt1 db "Enter A: "
- prompt1_len equ $ - prompt1
- prompt2 db "Enter B: "
- prompt2_len equ $ - prompt2
- prompt3 db "Enter C: "
- prompt3_len equ $ - prompt3
- result_msg db "Result (A+B-C): "
- result_msg_len equ $ - result_msg
- newline db 10
- STDIN_FILENO equ 0
- STDOUT_FILENO equ 1
- STDERR_FILENO equ 2
- section .bss
- num1 resb 2 ; Space for digit A and newline
- num2 resb 2 ; Space for digit B and newline
- num3 resb 2 ; Space for digit C and newline
- result resb 2 ; Space for result and newline
- section .text
- global _start
- _start:
- ; Print "Enter A: "
- mov rax, 1 ; sys_write
- mov rdi, 1 ; stdout
- mov rsi, prompt1
- mov rdx, prompt1_len
- syscall
- ; Read A
- mov rax, 0 ; sys_read
- mov rdi, 0 ; stdin
- mov rsi, num1
- mov rdx, 2 ; read 2 bytes (digit + newline)
- syscall
- ; Print "Enter B: "
- mov rax, 1
- mov rdi, 1
- mov rsi, prompt2
- mov rdx, prompt2_len
- syscall
- ; Read B
- mov rax, 0
- mov rdi, 0
- mov rsi, num2
- mov rdx, 2
- syscall
- ; Print "Enter C: "
- mov rax, 1
- mov rdi, 1
- mov rsi, prompt3
- mov rdx, prompt3_len
- syscall
- ; Read C
- mov rax, 0
- mov rdi, 0
- mov rsi, num3
- mov rdx, 2
- syscall
- ; A+B-C
- mov al, [num1]
- sub al, '0' ; ASCII -> number
- mov bl, [num2]
- sub bl, '0' ; ASCII -> number
- add al, bl ; A + B
- mov bl, [num3] ; Get third digit
- sub bl, '0' ; ASCII -> number
- sub al, bl ; (A + B) - C
- add al, '0' ; back to ASCII
- ; Store result
- mov [result], al
- ; Print result message
- mov rax, 1
- mov rdi, 1
- mov rsi, result_msg
- mov rdx, result_msg_len
- syscall
- ; Print result
- mov rax, 1
- mov rdi, 1
- mov rsi, result
- mov rdx, 1
- syscall
- ; Print newline
- mov rax, 1
- mov rdi, 1
- mov rsi, newline
- mov rdx, 1
- syscall
- ; Exit program
- mov rax, 60 ; sys_exit
- mov rdi, 0 ; exit code 0
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement