134 lines
3.0 KiB
NASM
134 lines
3.0 KiB
NASM
; Fibonacci number generator using AMD64 assembly with Windows syscalls
|
|
; Uses lookup table for O(1) access
|
|
; Author: Assistant
|
|
|
|
.686p
|
|
.xmm
|
|
.model flat, C
|
|
|
|
include kernel32.inc
|
|
include user32.inc
|
|
|
|
includelib kernel32.lib
|
|
includelib user32.lib
|
|
|
|
.data
|
|
; Precomputed Fibonacci numbers (first 50)
|
|
fib_table DWORD 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
|
|
fib_table DWORD 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
|
|
fib_table DWORD 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040
|
|
fib_table DWORD 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155
|
|
fib_table DWORD 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903
|
|
|
|
; Error messages
|
|
msg_error DWORD 'Error: Index out of range', 0
|
|
msg_success DWORD 'Fibonacci number: ', 0
|
|
|
|
; Console handles
|
|
hStdOut HANDLE ?
|
|
|
|
.code
|
|
|
|
; Function to get Fibonacci number at index n (0-based)
|
|
; Input: EAX = index
|
|
; Output: EAX = Fibonacci number
|
|
get_fibonacci PROC
|
|
; Validate input
|
|
cmp eax, 0
|
|
jl invalid_input
|
|
|
|
; Check if index is within range (max 50)
|
|
cmp eax, 49
|
|
jg invalid_input
|
|
|
|
; Calculate address in table
|
|
mov ebx, eax
|
|
shl ebx, 2 ; Multiply by 4 (DWORD size)
|
|
|
|
; Load Fibonacci number from table
|
|
mov eax, fib_table[ebx]
|
|
ret
|
|
|
|
invalid_input:
|
|
xor eax, eax ; Return 0 for invalid input
|
|
ret
|
|
get_fibonacci ENDP
|
|
|
|
; Function to print a number to console
|
|
print_number PROC
|
|
; Input: EAX = number to print
|
|
push eax
|
|
push ebx
|
|
push ecx
|
|
push edx
|
|
|
|
; Convert number to string and print
|
|
mov ebx, 10
|
|
mov ecx, 0 ; Digit count
|
|
|
|
; Special case for zero
|
|
cmp eax, 0
|
|
jne convert_loop
|
|
mov ecx, 1
|
|
jmp print_digits
|
|
|
|
convert_loop:
|
|
cmp eax, 0
|
|
je print_digits
|
|
|
|
xor edx, edx ; Clear high bits
|
|
div ebx ; Divide by 10
|
|
push edx ; Push remainder (digit)
|
|
inc ecx ; Increment digit count
|
|
jmp convert_loop
|
|
|
|
print_digits:
|
|
cmp ecx, 0
|
|
je done_printing
|
|
|
|
pop eax ; Get digit back
|
|
add eax, '0' ; Convert to ASCII
|
|
push eax
|
|
|
|
; Write character to console
|
|
mov edx, esp
|
|
push 1 ; Number of characters
|
|
push edx ; Address of character
|
|
push hStdOut ; Handle
|
|
call WriteConsoleA
|
|
|
|
add esp, 12 ; Clean up stack
|
|
|
|
dec ecx
|
|
jmp print_digits
|
|
|
|
done_printing:
|
|
pop edx
|
|
pop ecx
|
|
pop ebx
|
|
pop eax
|
|
ret
|
|
print_number ENDP
|
|
|
|
; Main program entry point
|
|
main PROC
|
|
; Get console handle
|
|
push -11 ; STD_OUTPUT_HANDLE
|
|
call GetStdHandle
|
|
mov hStdOut, eax
|
|
|
|
; Test Fibonacci numbers
|
|
mov eax, 10 ; Get 11th Fibonacci number (0-indexed)
|
|
call get_fibonacci
|
|
|
|
; Print result
|
|
push eax
|
|
call print_number
|
|
add esp, 4
|
|
|
|
; Exit program
|
|
push 0
|
|
call ExitProcess
|
|
main ENDP
|
|
|
|
END main |