Forth is a structured, imperative, reflective, stack-based computer programming language and programming environment.
Aprocedural, stack-oriented, and reflective programming language withouttype checking, Forth features both interactive execution of commands(making it suitable as a shell for systems that lack a more formaloperating system) and the ability to compile sequences of commands forlater execution. Some Forth implementations (usually early versions orthose written to be extremely portable) compile threaded code, but manyimplementations today generate optimized machine code like otherlanguage compilers.

Although not as popular as other programmingsystems, Forth has enough support to keep several language vendors andcontractors in business. Forth is currently used in boot loaders suchas Open Firmware, space applications, and other embedded systems.

Forth relies heavily on explicit use of a data stack and reverse Polish notation (RPN or postfix notation).

For those who know C, Forth is a bit different than C.

1. In Forth program, arguments are unnamed unlike C, where we use named variables.
2. C handles data through data structure, FORTH does that through stack as well as data structure.
3.Some developers favour FORTH over C because a FORTH system can providea tightly integrated development environment consisting of Assembler,High level language, Real Time Executive, interactive development anddebug. All without any special (expensive) hardware tools for the sameprice as a C compiler.
4. Many people also argue that theirproductivity with FORTH is higher, they are closer to the machine andtheir turnaround times were significantly better than that of C.

Forthwas originally developed for small embedded control mini- andmicro-computers, and it seems to have been implemented on every majorprocessor manufactured. It has been used in a wide variety ofapplications, including spreadsheets, expert systems, multi-userdatabases, and distributed real time control systems.

Forth isbasically a two stack machine, having two built-in stacks, a data stackand a return stack. Data evaluation in Forth is accomplished on theData Stack using Reverse Polish Notation (RPN).
The DataStack is theone used for passing data between Forth words. The Return Stack isanother Forth stack that is primarily for internal system use.

Whena word is invoked, the inner interpreter puts the next address on thereturn stack. When the word is completed the top item on the returnstack is popped into a register called ip (instruction pointer), andexecution continues from there. Most Forths that run on Intel machinesuse the BP register for ip.

Since the return stack is actuallyused only on invocation and completion, , it is available as a kind ofcatch-all in between. One common use for the return stack is to storedo ... loop parameters. Anything added to the return stack during theexecution of a word must be removed before the word ends, and the sameis true for a loop.

Below I am mentioning some of the forth words and their description just for better understanding :

1. '.' prints the number on the top of the data stack. '.S' prints the whole stack.

Wehave a way of documenting the effect of words on the stack with a stackdiagram. A stack diagram is contained in parentheses. In Forth, theparentheses indicate a comment. In the examples that follow, you do notneed to type in the comments. While programming, it is encouraged touse comments and stack diagrams to make the code more readable.

The stack diagram for a word like ' . ' would be:

. ( N -- , print number on top of stack )

2. SWAP swaps the top two elements of the stack.

The stack diagram for SWAP would be:

SWAP ( a b -- b a , swap top two items on stack )

3. The word OVER causes a copy of the second item on the stack to leapfrog over the first. It's stack diagram would be:

OVER ( a b -- a b a , copy second item on stack )

4. drop ( n -- )
Removes n from the stack

5. The stack diagram for ROT is, therefore:

ROT ( a b c -- b c a , ROTate third item to top )
6. If you want to grab any arbitrary item on the stack, use PICK .
PICK makes a copy of the Nth item on the stack. The numbering starts with zero, therefore:

0 PICK is equivalent to DUP
1 PICK is equivalent to OVER

PICK ( ... v3 v2 v1 v0 N -- ... v3 v2 v1 v0 vN )

7. 2SWAP ( a b c d -- c d a b , swap pairs )

8. 2OVER ( a b c d -- a b c d a b , leapfrog pair )

9. 2DUP ( a b -- a b a b , duplicate pair )

10. 2DROP ( a b -- , remove pair )

11. CR ( -- , start new line , carriage return )

12. Here are stack diagrams for some of these words :

< ( a b -- flag , flag is true if A is less than B )

> ( a b -- flag , flag is true if A is greater than B )

= ( a b -- flag , flag is true if A is equal to B )

0= ( a -- flag , true if a equals zero )

OR ( a b -- a||b , perform logical OR of bits in A and B )

AND ( a b -- a&b , perform logical AND of bits in A and B )

NOT ( flag -- opposite-flag , true if false, false if true )

13. >r ( n --)
Store top of parameter stack on return stack. Pronounced "to R."

r> ( -- n)
Reverse of >R. Pronounced "from R."

r@ ( -- n) or r ( -- n)
Copy top of return stack onto parameter stack. Prounounced "R fetch."

14. @ ( adr -- n )
Fetches a number from adr

In SPARC, the initial stage of the boot process requires support from system firmware, typically loaded into a PROM,
such that a bootable device can be addressed and that some minimal I/O functionality exists for reading of a promary bootblock.

For SPARC, the firmware is known as OpenBoot PROM or OBP and is written in forth programming language.

Recently, I am working on one bug, regarding, the computer hangs when we give the command "boot -" in the ok prompt.
On analyzing the problem and going through the file /usr/src/psm/stand/bootblks/common/boot.fth, we can see,

: next-c ( adr len -- adr' len' c )
over c@ >r str++ r>

whichclearly shows, after getting the command 'boot -', it searches for thenext character after the '-' which it does not find. There is no checkperformed to see whether any character is at all present after '-',which in turn throws "boot -" causes Fast Data Access MMU Miss errormessage on the screen.

The solution is simple, add one checkingto see whether any character is present after '-' and if not then topush a '0' on the stack, so that invoking switch statement on thisnext-c, we can handle this behavior.

\ next char or 0 if eol
: next-c ( adr len -- adr' len' c )
dup if over c@ >r str++ r> else 0 then