Forth

From Noisebridge
Revision as of 18:40, 12 October 2017 by Ⅳᵗʰ (talk | contribs) (→‎sample: xsqrt)
Jump to navigation Jump to search

sample

2 + 2

2
2
+
.

result 4


9 % 4

9
4
/mod
.s

result 1 2


square word

: sq dup * ;
2 sq
.s

result 4


hello world

: hw ." hello world" ;
hw

result hello world


integer square root

-1
variable x
256 x !
variable g
1 x ! 
: r x @ g @ / ;
: a g @ + 2/ g ! ;
: t r a g @ .s ;
: c over over = . ;
t c
...
t c

result

t c <2> -1 128 0  ok
t c <3> -1 128 65 0  ok
t c <4> -1 128 65 34 0  ok
t c <5> -1 128 65 34 20 0  ok
t c <6> -1 128 65 34 20 16 0  ok
t c <7> -1 128 65 34 20 16 16 -1  ok

links

http://amforth.sourceforge.net/

AmForth, Floating point and ATmega implementation:

Pygmy Forth: http://pygmy.utoh.org/riscy/