Forth: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
(→‎sample: xsqrt)
m (→‎sample: g typo)
Line 48: Line 48:
256 x !
256 x !
variable g
variable g
1 x !  
1 g !  
: r x @ g @ / ;
: r x @ g @ / ;
: a g @ + 2/ g ! ;
: a g @ + 2/ g ! ;

Revision as of 18:41, 12 October 2017

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 g ! 
: 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/