Overview

Forth is a structured, stack-based programming language developed by Charles Moore. Forth provides two stacks, the data stack and the return stack. Instructions in Forth manipulate the values on the stacks.

Redcode Forth is a threaded implementation of Forth for the MARS virtual machine. The subset provides 36 Forth words in 85 Redcode instructions. Programs are stored as a list of subroutine calls.

Instruction set

The following instructions are implemented in Redcode Forth:

Mathematics

Forth Macro Description - MINUS remove TOS and subtract TOS from 2OS . implemented as NEGATE + + PLUS remove TOS and add TOS to 2OS * TIMES remove TOS and multiply 2OS by TOS U/ UDIV remove TOS and divide 2OS by TOS UMOD UMOD remove TOS and 2OS . divide 2OS by TOS , place remainder on stack 1+ ONEPLUS add 1 to TOS ABS ABS replace TOS with the absolute value of TOS NEGATE NEGATE replace TOS with - TOS = EQUALS remove TOS . if TOS = 2OS replace 2OS with true. else false 0= ZEQUALS if TOS = 0 replace TOS with true. else replace with false U< ULESS remove TOS . if 2OS < TOS replace 2O with true. else false

Flow Control

Forth Macro Description BEGIN BEGIN place current IP on the return stack UNTIL UNTIL remove TOS . if TOS = 0 then jump to TORS , else remove TORS EXECUTE EXECUTE remove TOS , place IP on the return stack and jump to TOS EXIT EXIT remove and jump to TORS DO DO remove TOS and 2OS . place IP , TOS and 2OS on the return stack I I place the inner loop index on the stack J J place the outer loop index on the stack LOOP LOOP add 1 to 2ORS . if <> TORS go to 3ORS , else drop TORS , 2ORS and 3ORS

Output

Forth Macro Description U. UDOT remove TOS and display as an unsigned base 10 number SPACE SPACE display a space CR CR display a newline character EMIT EMIT remove and display TOS

Stack Manipulation

Forth Macro Description DROP DROP remove TOS DUP DUP place a copy of TOS on the stack OVER OVER place a copy of 2OS on the stack. implemented as 1 PICK PICK PICK replace TOS with a copy of the TOS th stack item SWAP SWAP exchange TOS and 2OS . implemented as 1 ROLL ROT ROT rotate TOS , 2OS and 3OS , moving 3OS to TOS . implemented as 2 ROLL ROLL ROLL remove TOS and rotate TOS stack items, moving the deepest to TOS DEPTH DEPTH place the size of the stack on the stack ?DUP QDUP if TOS <> 0, place a copy of TOS on the stack

Miscellaneous

Forth Macro Description LIT LIT place a copy of the next value in the program on the stack LITP LITP adjust the next value to be relative to IP then place on stack R@ RFETCH place a copy of TORS on the stack R> RFROM remove TORS and place on the stack >R TOR remove TOS and place on the return stack

Example Programs

Fibonacci Sequence

The Fibonacci Sequence is a series of numbers studied by Leonardo of Pisa, also known as Fibonacci. Each number in the sequence is the sum of the previous two. The following Redcode Forth program displays the first 20 numbers:

main lit 0 lit 1 lit 20 lit 0 do dup udot cr swap over plus loop end

The first line of the program contains the label main to show Redcode Forth where the program begins. The second line contains two instructions. lit 0 places 0 on the stack, then lit 1 places 1 on the stack. These are the first two numbers in the Fibonacci Sequence:

0 1

Next, lit 20 lit 0 places 20, 0 on the stack:

0 1 20 0

do removes the top two elements from the stack to use as the parameters for a do ... loop. The loop index is set to 0 and the loop limit is set to 20:

0 1

dup places a copy of the top stack element on the stack. udot removes the top element and displays it, then cr displays a newline.

Next, swap exchanges the top two stack elements:

1 0

Then over places a copy of the second element onto the stack:

1 0 1

plus add the top element to the second element, then removes the top element:

1 1

Finally, loop adds 1 to the loop index. If the loop index is less than the loop limit, the program jumps back to the matching do . The initial loop index is 0 and the limit is 20, so the do ... loop will be repeated 20 times.

Implemention in Redcode