Hardware
Firmware
Software
Anywhere
  Home Sensors Software Contact Us 
 
 
 
 
JDForth - Fibonacci Example
History of this example
The fibonacci example was initially proposed by DeSilva on the Parallax Propeller Forum. (Its the second post from the bottom).

The demo presented here gives four variations of the Fibonacci test.

• Fibonacci in Spin - Fib(28) takes 28.69 seconds
• Fibonacci in pure Forth - Fib(28) takes 6.48 seconds
• Fibonacci with a CORE: helper word - Fib(28) takes 2.78 seconds
• Fibonacci written entirely with PASM: - Fib(28) takes 0.86 seconds

On this page you may have a look at:

• The serial terminal display of the test result.
• The JDForth source code (FibTest.JD4) for the Fibonacci test.
• The result of running the Propeller Tool on FibTest.spin.
• The testbench code (FibDemo.spin) used to generate the terminal display.
• The output file (FibTest.spin) generated by JDForth.

Try it for yourself!
Download the spin files. (FibTest.spin and FibDemo.spin)
Terminal display
Screen shot of FibDemo.spin on a Propeller
connected to HyperTerminal.

JDForth source code - FibTest.JD4
Propeller Tool compile of FibTest.spin
This screen shot was taken after compiling FibTest.spin in the Propeller Tool.



Plenty of space left for the rest of your spin objects!
Propeller Tool testbench - FibDemo.spin
JDForth output code - FibTest.spin
CON
' This file was auto-generated by JDForth.
' See: www.JacobsDesign.com.au for more details.

  kPSPSize = 64 'longs
  kASPSize = 16 'longs
  kRSPSize = 48 'longs
  kTIBSize = 16 'bytes

PUB Start(emit, key) : okay
  Stop
  FirstWord := 0
  CodeBase  := @Forth
  PSP0 := @ParStack[-1]
  RSP0 := @AuxStack
  ASP0 := @RetStack
  Base := 10
  PutC := emit
  GetC := key
  TIB  := @TIBSpace
  TIBSize := kTIBSize
  okay := Cog := CogNew(@Kernel, @FirstWord) + 1

PUB Stop
  if Cog
    CogStop(Cog~ - 1)

PUB Push(val)  ' Push a single value on the parameter stack
  repeat while FirstWord
  PSP0 += 4
  long[PSP0] := Val

PUB Pop : val  ' Pop a single value on the parameter stack
  repeat while FirstWord
  val := long[PSP0]
  PSP0 -= 4

PUB Fib1(P1)  ' 2 return values on parameter stack
  PushAll(1, @P1)
  FirstWord := @_Fib1

PUB Fib2(P1)  ' 2 return values on parameter stack
  PushAll(1, @P1)
  FirstWord := @_Fib2

PUB Fib3(P1)  ' 2 return values on parameter stack
  PushAll(1, @P1)
  FirstWord := @_Fib3

PUB FirstWordPtr : Ptr
  Ptr := @FirstWord

PUB WaitForIdle
  repeat while FirstWord

PRI PushAll(ctr, ptr)
  repeat while FirstWord
  LongMove(@ParStack, ptr, ctr)
  PSP0 := @ParStack[ctr-1]

VAR
  long TOS   ' Single element to support TOS in COG memory
  long ParStack[kPSPSize]
  long AuxStack[kASPSize]
  long RetStack[kRSPSize]
  long TIBSpace[(kTIBSize+3)/4]

 ' WARNING: ANY edits below this point will most likely result in 
 '          a non-working program. Items above this point may be 
 '          edited with caution.

VAR
  long Cog         ' The cog identity of this object
  word FirstWord   ' The word to execute
  word CodeBase    ' Memory offset to bytecodes
  word PSP0        ' Parameter Stack Address
  word RSP0        ' Return Stack Address
  word ASP0        ' Algorithm Stack Address
  word Base        ' Number base for .
  word State       ' Compiling or interpreting
  word Handler     ' Used by catch/throw
  word PutC        ' emit places chars into the word at address putc
  word GetC        ' key gets chars from the word at address getc
  word ToIN        ' We're processing this char in TIB
  word NumIN       ' This is the number of chars in TIB
  word TIB         ' Address of Terminal Input Buffer
  word TIBSize     ' Amount of space in TIB

DAT
Forth   word $0000,$000D,$0025,$0026,$0027,$002C,$0031,$0038
        word $003C,$0042,$0044,$0047,$0049,$004B,$0050,$0054
        word $0057,$005C,$005E,$0061,$0064,$0066,$006D,$0072
        word $0078,$007C,$007E,$007F,$0081,$0083,$0085,$008A
        word $008C,$0090,$0095,$009A,$00A6,$0042,$01F1,$002C
        word $004A,$0030,$0024,$000E,$004A,$0030,$0026,$003E
        word $000C,$002C,$001E,$0016,$0040,$0042,$0010,$001E
        word $0036,$0062,$001C,$0034,$0062,$003C,$000C
_Fib1   word $002C,$0010,$0062,$004E,$0002,$000C,$00AD,$002C
        word $008A,$008C,$001C,$008C,$003C,$000C
_Fib2   word $002C,$0010,$008C,$004E,$0002,$000C,$00B5,$002C
        word $00A6,$000C
_Fib3   word $002C,$0010,$00A8,$004E,$0002,$000C

DAT
        long 0  'Make sure the rest is long aligned!
Kernel  word $0019,$5C7C,$0000,$0000,$0000,$0000,$0000,$0000
        word $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        word $0000,$0000,$0000,$0000,$0000,$0000,$0200,$0000
        word $FFFF,$FFFF,$0804,$80FC,$0E04,$083C,$0E03,$A0FC
        word $2604,$54FC,$1001,$A0BC,$1004,$80FC,$0008,$043C
        word $1002,$80FC,$260B,$80BC,$0E13,$E4FC,$0402,$6CBC
        word $0401,$043C,$03F0,$A0BC,$0401,$06BC,$001A,$5C68
        word $0402,$80FC,$0E04,$A0FC,$4203,$54FC,$1001,$A0BC
        word $1002,$80FC,$0008,$04BC,$1002,$80FC,$420B,$80BC
        word $0E21,$E4FC,$0E04,$08BC,$0804,$84FC,$1002,$04BC
        word $0402,$80FC,$1003,$80BC,$1208,$04BC,$0009,$5C3C
        word $0A04,$80FC,$0405,$083C,$0408,$A0BC,$0402,$80FC
        word $0027,$5C7C,$0405,$08BC,$0A04,$84FC,$0027,$5C7C
        word $0804,$80FC,$0E04,$083C,$1002,$80FC,$0000,$5C7C
        word $1007,$A0BC,$0E04,$08BC,$0804,$84FC,$0029,$5C7C
        word $1202,$04BC,$0402,$80FC,$0804,$80FC,$0E04,$083C
        word $0E09,$A0BC,$0044,$5C7C,$6E34,$5CFC,$0E08,$04BC
        word $0E10,$2CFC,$0E10,$38FC,$0027,$5C7C,$1001,$A0FC
        word $004C,$5C7C,$1000,$A0FC,$004C,$5C7C,$100C,$A0BC
        word $0804,$80FC,$0E04,$083C,$0E08,$A0BC,$0027,$5C7C
        word $1207,$A0BC,$0E04,$08BC,$1204,$083C,$0027,$5C7C
        word $0804,$80FC,$0E04,$083C,$0027,$5C7C,$1004,$A0BC
        word $0E02,$2CFC,$1007,$84BC,$0E08,$08BC,$0027,$5C7C
        word $0C04,$84FC,$0027,$5C7C,$0A04,$80FC,$0E05,$083C
        word $0025,$5C7C,$0804,$80FC,$0E04,$083C,$0E05,$08BC
        word $0A04,$84FC,$0027,$5C7C,$1004,$08BC,$0A04,$80FC
        word $1005,$083C,$0A04,$80FC,$0E05,$083C,$0804,$84FC
        word $0025,$5C7C,$E008,$58FC,$1004,$08BC,$0804,$84FC
        word $1007,$043C,$0025,$5C7C,$ED41,$58FC,$EC07,$54BC
        word $1004,$08BC,$0804,$84FC,$0008,$A0BC,$0025,$5C7C
        word $F407,$50BC,$0000,$0000,$0E00,$A0BC,$0027,$5C7C
        word $0E01,$80FC,$0027,$5C7C,$0E01,$84FC,$0E01,$84FC
        word $0027,$5C7C,$0E01,$2CFC,$0027,$5C7C,$0E01,$38FC
        word $0027,$5C7C,$0F01,$58FD,$1004,$08BC,$1007,$80BC
        word $0E08,$A0BC,$0026,$5C7C,$0F09,$58FD,$0086,$5C7C
        word $1004,$08BC,$0E08,$C13C,$0E0C,$70BC,$0026,$5C7C
        word $0E00,$867C,$0E04,$08BC,$0804,$84FC,$0402,$80D4
        word $0027,$5C54,$1002,$04BC,$1010,$2CFC,$1010,$38FC
        word $0408,$80BC,$0027,$5C7C,$1401,$A0FC,$1005,$08BC
        word $1205,$A0BC,$1204,$84FC,$1209,$08BC,$100A,$80BC
        word $1208,$863C,$1005,$083C,$0095,$5C54,$0402,$80FC
        word $0A08,$84FC,$0027,$5C7C,$57E0,$58FD,$1207,$A0BC
        word $1004,$08BC,$0804,$84FC,$0E04,$08BC,$1009,$F03C
        word $0026,$5C7C,$1201,$A0FC,$1207,$C13C,$0031,$5C4C
        word $0E01,$84FC,$0804,$80FC,$0E04,$083C,$0E01,$84FC
        word $0027,$5C7C,$96B8,$5CFD,$0027,$5C7C,$0001,$0000
        word $6E07,$C13D,$00CB,$5C4C,$0E01,$84FC,$0804,$80FC
        word $0E04,$083C,$0E01,$84FC,$0A04,$80FC,$9605,$083D
        word $96B8,$5CFD,$0A04,$80FC,$0E05,$083C,$0E04,$08BC
        word $0804,$84FC,$96B8,$5CFD,$1005,$08BC,$0A04,$84FC
        word $0E08,$80BC,$9605,$08BD,$0A04,$84FC,$0000,$5C7C