Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .data
- result_msg db "GCD is: ", 0
- result_len equ $ - result_msg
- newline db 0xA
- section .bss
- num1 resb 2 ; 1 digit + newline
- num2 resb 2
- section .text
- global _start
- _start:
- ; Read first digit
- mov rax, 0
- mov rdi, 0
- mov rsi, num1
- mov rdx, 2 ; 1 digit + newline
- syscall
- ; Read second digit
- mov rax, 0
- mov rdi, 0
- mov rsi, num2
- mov rdx, 2
- syscall
- ; Convert ASCII to numbers
- movzx rax, byte [num1] ; First number
- sub rax, '0' ; Convert from ASCII
- movzx rbx, byte [num2] ; Second number
- sub rbx, '0' ; Convert from ASCII
- ; Calculate GCD using repeated subtractions
- gcd_loop:
- cmp rax, rbx ; Compare numbers
- je gcd_done ; If equal, we found GCD
- jl smaller ; If RAX < RBX, swap
- sub rax, rbx ; Subtract smaller from larger
- jmp gcd_loop
- smaller:
- sub rbx, rax ; RBX is larger, subtract RAX from it
- jmp gcd_loop
- gcd_done:
- ; Print result message
- push rax ; Save result
- mov rax, 1
- mov rdi, 1
- mov rsi, result_msg
- mov rdx, result_len
- syscall
- pop rax ; Restore result
- ; Convert result to ASCII
- add rax, '0' ; Convert to ASCII
- ; Print result digit
- push rax
- mov rax, 1
- mov rdi, 1
- mov rsi, rsp ; Print from stack
- mov rdx, 1
- syscall
- pop rax
- ; Print newline
- mov rax, 1
- mov rdi, 1
- mov rsi, newline
- mov rdx, 1
- syscall
- ; Exit
- mov rax, 60
- mov rdi, 0
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement