- Experience Levels
- Backgrounds
- Goals
- Use Forth to understand low level computer concepts.
- Focus will be Arduino, but the concepts apply to all computers and languages.
- Intentionally focusing on small / old comuters to reduce conceptual load
- There will be A few lab experiments.
Let's start that now.
- Chuck Moore
- First Forth projects
- Rise of popularity in the early 80's
- Characteristics (procedural, concatenative, untyped)
- Forth today
- Firmware
- Resource constrained applications
- Firmware
- And occasionally Firmware
- Even the tiniest devices can run Forth- easily ported to new systems.
- Very resource efficient (minimal memory requirements)
- "Low Floor, High Ceiling"
- Neat: Jones Forth
- It's fun!
- Less tooling and setup than C
- The only language a developer can completely understand quickly.
- Makes you a better programmer
- Influences your understanding of other languages.
- Low level concepts are easier to understand.
- If you can dodge a wrench, you can dodge a ball.
- No Garbage Collector
- No Floating Point Numbers (sometimes)
- No Type System or Type Checker (everything is an integer)
- Compiled and interpreted!
http://www.microchip.com/wwwproducts/en/ATmega328P
- Processor
- Memory
- Input lines
- Output lines
- Other lines (power, reset, clock, etc.)
http://cs-alb-pc3.massey.ac.nz/notes/59304/l15.html
A computer must "remember" the data it is working with.
Memory is like a spreadsheet for integers. Memory has many addresses that hold values.
Address | Value (decimal) |
---|---|
0 | 24 |
1 | 13 |
2 | 27 |
3 | 255 |
4 | 37 |
5 | 0 |
6 | 42 |
7 | 86 |
Kind | Size |
---|---|
Flash | 32 kB |
RAM | 2 kB |
ROM | 1 kB |
Computers require symbolic thinking.
Instead of... | Let's say... |
---|---|
A | 65 |
65 | 1011 |
1011 | high, low, high, high |
high, low, high, high | A |
At a low level, Computers only understand numbers!
All concepts must be symbolized as numbers or sequences of numbers to be useful.
Performs calculations and actions using contents of memory.
Example: Read address, write address, execute address.
We will come back to this in a moment.
Convert voltage fluctuations to numbers which are usually stored in memory.
That's it. Keyboards, monitors, WiFi cards all work like this.
https://sites.google.com/site/lofturj/cwreceive
What's going on inside the chip?
- Computer senses an incoming signal
- Computer stores signal value in a memory address
- Computer processes the value, often storing the result in a new address
- Computer writes to certain special addresses, that cause signals to be sent back out (output)
Memory operations in the old days: https://www.youtube.com/watch?v=vAhp_LzvSWk
- Now that we understand the hardware, let's focus on human computer interaction.
- Any questions on what we've covered so far?
- We will cover machine language and assembly, but only to the extent needed for Forth.
If you are a programmer, but have never worked in a language with manual memory management (C, C++, Forth, Rust, Fortran, etc.), some of these concepts will be new to you.
Remember when we said that computers can only operate on numeric symbols?
The processor follows this sequence, over and over:
- Fetch: Get a value from a memory address.
- Decode: Determine what the number symbolizes (Read, Write, Jump, etc.)
- Execute: Perform the operation. Ex: "Add contents of cell 1 and cell 2".
- Repeat: This cycle is repeated continually. Some architectures feature a HALT instruction.
The lowest level computer language is Machine Language.
Not (easily) human readable. Can be directly placed into memory without any changes.
Address | Instruction | Description |
---|---|---|
0 | 00000001 | Turn pin 1 (connected to LED bulb) on |
1 | 00000010 | sleep for 2 seconds |
2 | 00000001 | Turn pin 1 off |
3 | 00000010 | If pin2 is off, skip next line |
4 | 00000000 | Halt |
5 | 01010101 | Jump to address 0 |
Real world example: https://www.computerhope.com/jargon/p/punccard.htm
Credit: https://chortle.ccsu.edu/java5/Notes/chap04/ch04_4.html
One up from machine language is assembly language.
Like machine language, it has a one-to-one correspondence to the computer's instruction codes.
Unlike machine language, it uses pneumonic based (human readable) codes to represent each instruction.
An assembler is required to translate a pneumonic code to machine language.
NEXT: See an arcade game written in assembly.
- Someone wrote a snake arcade game using 6502 assembly.
- Move the snake via
W, A, S, D
keys - Be sure to see the "notes" section.
- With the exception of firmware developers, this is what everyone uses today (C, Java, etc.)
- Still need to exist as machine language, but do so without a 1-to-1 correspondence to machine language.
- Can use concepts that are easier for humans to understand.
- Can use words instead of numbers.
- Don't always need to think in terms of memory addresses and data lines.
set myUrl to "http://google.com"
display dialog myUrl
- Numbers can represent real data (values) or a symbol representing an instruction.
- The real world interacts with software by sending signals to input lines, causing changes to values in memory.
- Software interacts with the real world by sending signals to output lines.
We now have enough understanding of the hardware to move forward.
Any questions about hardware before we continue?
- Terminal
- Text editor
- Check that all the expected words are there
- Install a "whoops" marker
- Talk about lecture/lab format
Let's do some math
- Actually called the "data stack", since it is not the only stack.
- You never need parenthesis.
- Copy/pasting code between locations is easier.
- The Stack is not an abstraction on most architectures. It exists on "bare metal".
- It's easy to go backwards or "backtrack" your work.
NEXT: Let's do math in Forth
- Use the enter key to "push" a new number on the stack. Use spaces as a delimiter.
1 2 3 <enter>
- Use the period character to "pop" the top off the stack and view it. (
.
)4 5 6 . . .
prints6 5 4
- Perform math via
+
,-
,*
, and/
(integer division only)- 2 + 2 is
2 2 + .
in forth
- 2 + 2 is
- First in, first out.
- The primary data structure in Forth.
- Interactive programming and debugging
- Easier setup because you don't need complicated "tool chains" or cross compilers.
- Everything resides on the host.
- Most systems are case insensitive (not ours).
In forth, functions and methods are called "words"-
reusable sequences of code. Let's make our Hello, world!
code more DRY*
- The space between
."
andHello
is significant!
\ Backslash is a comment in Forth
\ Defines a new "dictionary entry" of `hello`.
: hello
\ Print to serial line
." Hello, world!"
;
See a list of words by using the words
word.
You can enter a search term after words
. Ex: words "
returns ."
- Dictionary: A linked list of word definitions.
- Markers: Set a "bookmark" in the dictionary when you need to reqind.
- All characters except space and carriage return are legal in Forth word names.
- You will see words with symbols in them frequently. Eg:
.
,!
,@
.
SCENARIO: You find yourself adding the number 7 to other numbers very often.
: add7 7 + ;
12 add7 \ Pushes `19` on stack.
- Sometimes, the stack is not in the order you want.
- Some words can "rearrange" the stack:
- NEXT: All the stack re-arranging words
word | effect | hint |
---|---|---|
SWAP |
A B -> B A | |
ROT |
A B C -> B C A | (rotate) |
OVER |
A B -> A B A | |
TUCK |
A B -> B A B | |
DUP |
A -> A A |
Let's try these now.
=
,<
,>
,<>
- 0 is
true
- Nonzero (usually -1) is
false
Forth has flow control, like every other language. What is flow control, really?
NEXT: Example
: 7?
7 = IF
." IT IS 7!"
ELSE
." NOPE!"
THEN ;
LAB: What do 7 7?
and 8 7?
print?
TODO Age calculator app
- Without documentation, things get hard to read.
- Luckily, we have standards for this stuff.
- Words can be redefined
- Words must be defined before they can be used
- FUN FACT: You could theoretically create words for numbers like
7
, but don't do that.
begin <CODE_TO_EXECUTE> <TEST_CONDITION> until
: second 1000 * ms ;
: speak ( n -- n ) dup 1 second . ;
: decrement 1 - ;
: less_than_zero? 0 < ;
: blastoff! ." Blastoff!" ;
: countdown ( n -- )
begin
speak
decrement
dup \ Copy value for next loop
less_than_zero?
until ;
: launch ( n --- ) countdown blastoff! ;
There are other ways to loop:
RECURSE
, BEGIN
, WHILE
, AGAIN
, UNTIL
, REPEAT
TODO
- "big" Forths
- Execution tokens
- Vectored execution
- Compile vs. interpret
- inner vs. outter interpreter
- Defining words
- Tasks
- Vocabularies
- Dictionary structure
- recursion
- Stephen's Book
- Pictured Numeric Input
- LAB: "Hello, {{ name }}"
- LAB: "It's over {{ 9000 }}"
- LAB: Create a new marker
- LAB: Create a constant
- LAB: Create a variable
- LAB: Read a variable
- LAB: Create a
FOO+!
word - LAB: Create a
FOO@
word - LAB: Can/Can't drink beer
- LAB: 3 2 1 blastoff
- LAB: Blink
- LAB: Push to blink
- LAB: PWM write
- LAB PWM read
- LAB: Fire alarm