# Lab 1: Forth Basics

Remember:

• Be careful when performing operations that you don't "consume" too much off the stack - use DUP, OVER and TUCK to help you avoid this
• Make use of the fact that this is an interpreted environment - try commands out before defining them as a word
• The command to print what's on the stack (without consuming anything) is just .S
• Make large problems simpler by defining auxiliary words to help you - try and keep word definitions short.

### Running Forth

The version of Forth we'll use is GForth 0.4.0 - the executable is called gforth, and it's in a directory called: /package/jpower/se209/bin     So put this in your path, and you're ready to go...

For example, if you're using the c-shell you might type:

```
set path = ( \$path /package/jpower/se209/bin )
gforth
```
You should then see:
```GForth 0.4.0, Copyright (C) 1998 Free Software Foundation, Inc.
GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
```
Before you start the exercises below, try some simple maths! Remember that typing a number causes it to be pushed onto the stack; typing an operator (like +,-,*,/ or MOD) causes it to be applied to the topmost two elements in the stack. Type in some of the other commands (from the lecture notes) just to make sure you have the hang of them...

Remember: To print the stack, just type .S

### Some simple words

Define Forth words to perform the following:
1. max3 ( n1 n2 n3 -- n )
Gets the maximum of the topmost three stack elements.

2. is-even ( n -- flag )
True if the number on top is even, false otherwise (use MOD with 2).

3. mk-odd ( n -- n )
Rounds number on top of stack up to the nearest odd number (i.e. add 1 if the number is even, otherwise do nothing).

4. quadratic ( n1 n2 n3 n4 -- n )
Works out the formula ax2 + bx + c, assuming that the four numbers on top of the stack represent a, b, c and x (with x topmost).

### Words with loops

Define Forth words to perfom the following:
• sumbetween ( n1 n2 -- n )
Works out the sum of all numbers between the topmost two numbers on the stack (so if 3 and 8 were on the stack, the answer would be 4+5+6+7).

• fibonacci ( n -- n )
Work out the fibonacci of the number on top of the stack where:
• fibonacci(0) = fibonacci(1) = 1
• fibonacci(n) = fibonacci(n-1) + fibonacci(n-2), for n > 1
Use a loop here, keeping the values for fibonacci(n-1) and fibonacci(n-2) on the stack during the loop.

• print-stars ( n1 n2 -- )
Given n1 and n2 on top of the stack, print n1 spaces followed by n2 stars. There's a Forth word called EMIT that assumes the top of the stack is an ASCII value, and prints the corresponding character - ASCII for space is 32, for a star is 42.

• print-triangle ( n -- )
Given some number n, print a triangle of stars using all the odd numbers up to n; for example, if n was 5, print:
```  *
***
*****
```
This really only works if n is odd (or rounded up to an odd number...). You might want to define an auxiliary function that, given the number n and the current row number, works out how many spaces and stars to print.

 James Power, Dept. of Computer Science Last revised: 15 February 2000