Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .data
- newline db 10
- section .bss
- n resb 2
- ans resb 8 ; increased buffer size for larger numbers
- section .text
- global _start
- _start:
- ; read n
- mov rax, 0
- mov rdi, 0
- mov rsi, n
- mov rdx, 2
- syscall
- ; convert ASCII input to number
- movzx rax, byte [n]
- sub rax, '0'
- ; init first 2 elements
- mov rbx, 0 ; f0
- mov rcx, 1 ; f1
- ; store counter
- mov r8, rax
- mov rax, 1 ; i = 1
- fib_loop:
- cmp rax, r8
- jge out_fib_loop
- mov rdx, rbx
- add rdx, rcx
- mov rbx, rcx
- mov rcx, rdx
- inc rax
- jmp fib_loop
- out_fib_loop:
- ; Convert number in rcx to ASCII string
- mov rax, rcx ; number to convert is in rcx
- mov rbx, 10 ; divisor
- mov rdi, ans ; where to store the string
- add rdi, 7 ; point to end of buffer
- mov byte [rdi], 0 ; null terminator
- ; Convert to string (reverse order)
- convert_loop:
- dec rdi
- xor rdx, rdx ; clear rdx for division
- div rbx ; divide rax by 10
- add dl, '0' ; convert remainder to ASCII
- mov [rdi], dl ; store digit
- test rax, rax ; check if quotient is 0
- jnz convert_loop
- ; Calculate string length
- mov rdx, ans
- add rdx, 7 ; point to end of buffer
- sub rdx, rdi ; rdx = length of string
- ; Print the result
- mov rax, 1
- mov rsi, rdi ; point to start of number
- mov rdi, 1
- syscall
- ; Print newline
- mov rax, 1
- mov rdi, 1
- mov rsi, newline
- mov rdx, 1
- syscall
- ; Exit
- mov rax, 60
- xor rdi, rdi
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement