I just had to see how well hand-written code compares to his results.
Just the thought of writing a 6502 version makes my head hurt, so I'll leave it as an exercise for the superstar programmers here to do it...
As expected, the 6809 does very well:
Code: Select all
0100 00021 org $100
00022
0100 00023 fib
0100 1083 0002 [5] 00024 cmpd #2
0104 25 16 (011C) [3] 00025 blo Return
00026
0106 34 06 [7] 00027 pshs D ; Save original n
00028
0108 C3 FFFF [4] 00029 addd #-1 ; Do fib(n - 1)
010B BD 0100 [8] 00030 jsr fib
00031
010E 34 06 [7] 00032 pshs D ; Save fib(n - 1)
00033
0110 EC 62 [6] 00034 ldd 2,S ; Retrieve original n
00035
0112 C3 FFFE [4] 00036 addd #-2 ; Do fib(n - 2)
0115 BD 0100 [8] 00037 jsr fib
00038
0118 E3 E4 [6] 00039 addd ,S ; Add them together
00040
011A 32 64 [5] 00041 leas 4,S ; Clean the stack
00042
011C 00043 Return
011C 39 [5] 00044 rts
00045
011D 00046 Test
011D CC 0017 [3] 00047 ldd #23
0120 BD 0100 [8] 00048 jsr fib
00049
011D 00050 end Test
3,755,751 cycles
The 8080 does not fair too poorly. This is one of the few times the XTHL instruction (swap HL contents with the word at the top of the stack) is useful.
Code: Select all
0100 00021 org 100h
00022
0100 00023 fib:
0100 7C [5] 00024 mov A,H
0101 FE 00 [7] 00025 cpi 0
0103 C2 010A [10] 00026 jnz 2f
00027
0106 7D [5] 00028 mov A,L
0107 FE 02 [7] 00029 cpi 2
0109 D8 [5/11] 00030 rc
00031
010A 00032 2:
010A E5 [11] 00033 push H ; Save original n
00034
010B 11 FFFF [10] 00035 lxi D,-1 ; Do fib(n - 1)
010E 19 [10] 00036 dad D
010F CD 0100 [17] 00037 call fib
00038
0112 E3 [18] 00039 xthl ; Save fib(n - 1) and retrieve original n
00040
0113 11 FFFE [10] 00041 lxi D,-2 ; Do fib(n - 2)
0116 19 [10] 00042 dad D
0117 CD 0100 [17] 00043 call fib
00044
011A D1 [10] 00045 pop D ; Add them together
011B 19 [10] 00046 dad D
00047
011C C9 [10] 00048 ret
00049
011D 00050 Test:
011D 21 0017 [10] 00051 lxi H,23
0120 CD 0100 [17] 00052 call fib
00053
011D 00054 end Test
10,061,711 cycles
I had to see just how "inferior" the 6800 is...somewhat similar to the 8080:
Code: Select all
0100 00021 org $100
00022
0100 00023 fib
0100 4D [2] 00024 tsta
0101 26 04 (0107) [4] 00025 bne 2f
00026
0103 C1 02 [2] 00027 cmpb #2
0105 25 20 (0127) [4] 00028 blo Return
00029
0107 00030 2
0107 37 [4] 00031 pshb ; Save original n
0108 36 [4] 00032 psha
00033
0109 C0 01 [2] 00034 subb #1 ; Do fib(n - 1)
010B 82 00 [2] 00035 sbca #0
010D BD 0100 [9] 00036 jsr fib
00037
0110 37 [4] 00038 pshb ; Save fib(n - 1)
0111 36 [4] 00039 psha
00040
0112 30 [4] 00041 tsx ; Address the stack
00042
0113 E6 03 [5] 00043 ldab 3,X ; Retrieve original n
0115 A6 02 [5] 00044 ldaa 2,X
00045
0117 C0 02 [2] 00046 subb #2 ; Do fib(n - 2)
0119 82 00 [2] 00047 sbca #0
011B BD 0100 [9] 00048 jsr fib
00049
011E 30 [4] 00050 tsx ; Address the stack
00051
011F EB 01 [5] 00052 addb 1,X ; Add them together
0121 A9 00 [5] 00053 adca ,X
00054
0123 31 [4] 00055 ins ; Clean the stack
0124 31 [4] 00056 ins
0125 31 [4] 00057 ins
0126 31 [4] 00058 ins
00059
0127 00060 Return
0127 39 [5] 00061 rts
00062
0128 00063 Test
0128 4F [2] 00064 clra
0129 C6 17 [2] 00065 ldab #23
012B BD 0100 [9] 00066 jsr fib
00067
0128 00068 end Test
5,564,070 cycles