![]() With ada.text_io use ada.text_io procedure fast_fibo is - We work with biggest natural integers in a 64 bits machine type Big_Int is mod 2 ** 64 - We provide an index type for accessing the fibonacci sequence terms type Index is new Big_Int - fibo is a generic function that needs a modulus type since it will return - the n'th term of the fibonacci sequence modulus this type (use Big_Int to get the - expected behaviour in this particular task) generic type ring_element is mod with function "*" ( a, b : ring_element ) return ring_element is function fibo ( n : Index ) return ring_element function fibo ( n : Index ) return ring_element is type matrix is array ( 1. Getfib: input: BX = the desired fibonacci number (in other words, the "n" in "F(n)") DS must point to the segment where the fibonacci table is stored outputs to DX:AX (DX = high word, AX = low word) push ds cmp bx, 41 bounds check ja IndexOutOfBounds shl bx, 1 shl bx, 1 multiply by 4, since this is a table of dwords mov ax, code mov ds, ax mov si, offset fib mov ax, fetch the low word into AX mov dx, 2 fetch the high word into DX pop ds ret IndexOutOfBounds: stc set carry to indicate an error mov ax, 0 FFFFh return FFFF as the error code pop ds ret table of the first 41 fibonacci numbers fib dword 0, 1, 1, 2, 3, 5, 8, 13 dword 21, 34, 55, 89, 144, 233, 377, 610 dword 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657 dword 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269 dword 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986 dword 102334155 8th This keeps it simple for the CPU, which isn't aware of the intended size of each entry of the table.įor the purpose of this example, assume that both this code and the table are in the. Unfortunately, when using lookup tables you have very little choice, since trying to conditionally change the scaling of the index would more than likely take more code than encoding all data as the maximum size regardless of the contents, as was done here. We're wasting a lot of bytes to store very low numbers at the beginning (each takes up 4 bytes regardless of how many digits you see). For one, you end up taking up a lot of space. The elements of the table are ordered by index, so you can simply create a function that takes an offset as the parameter and returnsĪlthough lookup tables are very fast, there are some drawbacks to using them. With old computers it was common to use lookup tables to fetch pre-calculated values that would otherwise take some time to compute. OUTPUTS TO AX push BP push BX push AX mov BX, DI COPY INPUT TO BX xor AX, AX MOV AX,0 test BX, BX SET FLAGS ACCORDING TO BX je LBB0_4 IF BX = 0 RETURN 0 cmp BX, 1 IF BX = 1 RETURN 1 jne LBB0_3 mov AX, 1 ELSE, SET AX = 1 AND RETURN jmp LBB0_4 LBB0_3: lea DI, WORD PTR DI = BX - 1 call fib RETURN FIB(BX-1) mov BP, AX STORE THIS IN BP add BX, -2 mov DI, BX call fib GET FIB(DI - 2) add AX, BP RETURN FIB(DI - 1) + FIB(DI - 2) LBB0_4: add sp, 2 pop BX pop BP ret Using A Lookup Table Signon: db 'First 20 Fibonacci numbers:',cr,lf,'$'įib: WRITTEN IN C WITH X86-64 CLANG 3.3 AND DOWNGRADED TO 16-BIT X86 INPUT: DI = THE NUMBER YOU WISH TO CALC THE FIBONACCI NUMBER FOR. HL holds 16-bit unsigned binary number to print Lhld oldstk we're done - get CP/M's stack back Mloop: push a save our count (putdec will trash) RETURN (14,12),RC=0 restore registers from calling sav L R13,4(0,R13) restore previous savearea pointer ![]() ![]() ![]() * packed dec (PL8) = 15 decimals => max fibo(73) LM R14,R12,12(R13) restore previous savearea pointer STM STM R14,R12,12(R13) save previous contextĬVD R4,PW n convert binary to packed (PL8)ĬVD R3,PW f(n) binary to packed decimal (PL8) SAVEAREA B STM-SAVEAREA(R15) skip savearea The Fibonacci equation is a second-order difference equation which is a particularĪ sequence is a (possibly infinite) set of numbers with a defined order.* integer (31 bits) = 10 decimals -> max fibo(46) In this post we solve the Fibonacci sequence using linear algebra.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |