home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 4 Part 020 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- ┌────────────────────────────────┐
- │ Review of Return Stack Words │
- └────────────────────────────────┘
-
- The Words: >R R> and R@ are used for accessing the return stack.
-
- These words are very dangerous!! Do NOT test or execute them
- interactively. They can only be used within colon definitions.
-
- Note: ( P: inputs -- outputs ) Indicates parameter or data stack,
- ( R: inputs -- outputs ) Indicates return stack.
-
- \ Transfer top data stack item to return stack.
- >R ( P: n -- ) ( R: -- n )
- \ Transfer top return stack item to data stack.
- R> ( P: -- n ) ( R: n -- )
- \ Copy top return stack item to data stack.
- R@ ( P: -- n ) ( R: n n )
-
- ┌─────────────────────┐
- │ Return stack Rules │
- └─────────────────────┘
-
- 1. Each use of >R must be balanced with a corresponding R>.
- 2. Do not use >R R> and R@ within DO ... LOOPs. Loop control
- information is kept on the return stack and could be destroyed.
-
- Here is an application of the Return Stack in the following
- solution to problem 3.30 of Lesson 3.
-
- Write a word that calculates the area of a triangle
- using HERO's formula. A = sqrt[ s(s-a)(s-b)(s-c) ]
- where s is the semi perimeter. s = (a+b+c)/2
-
- \ Solution to problem 3.30
- : AREA ( a b c area )
- 3DUP + + 2/ >R ( a b c )
- R@ 3 ROLL - ( b c s-a )
- R@ 3 ROLL - ( c s-a s-b )
- R@ 3 ROLL - ( s-a s-b s-c )
- * * R> * SQRT
- CR ." Triangle area is " . ;
-
- \ Warning! You cannot factor the R@ 3 ROLL - out of the
- \ above definition. All user access to the return stack must
- \ occur within one word as FORTH uses the return stack to nest
- \ the calling words return address.
-
- Can you give a solution that does not use the return stack?
-
- ┌──────────────────────┐
- │ Memory Operators. │
- └──────────────────────┘
-
- It is time for us to learn somthing about how Forth and F-PC organize
- memory. We will begin our investigation with Forth in Hex mode.
- The following is not intended to be a complete picture of what happens
- when you make a colon definition in F-PC, it is just intended to be a
- start. Every time we make a colon defintion in F-PC information is
- stored in three areas memory areas or memory segments. The largest
- piece, area, chunck or segment of memory that the 8086 can address with
- a 16 bit address is 64K or 65,535 bytes. So in order to address more
- memory the 8086 has segment registers that can point to 64K chunks or
- segments of memory. F-PC takes advantage of these features by keeping
- information about defintions that are made in three separate segments of
- memory. These three segments are called:
-
- The CODE segment, The Header segment, and The List segment.
-
- Every time we make a colon defintion, each of the above three memory
- areas or segments receives the appropriate information about the new
- word definition. Let's concentrate on the CODE segment first. Every new
- word that we make stores information in the code segment. It is called
- the CODE segment because this is the segment that contains all of the
- 8086 machine code for the Forth system.
-
- We can find out the location of the next available memory loacation in
- the code segment by executing the word HERE . HERE returns a pointer
- or the address of the next available free memory location in the code
- segment. We like to work in the HEX mode when we are investigating
- actual memory contents so put your Forth system in hex mode and try some
- of the following exercises.
-
- HEX HERE U. <enter> 74D8 ok
- : MESSAGE ." HELLO" ; <enter> ok
- HERE U. <enter> 74DD ok
- 74DD 74D8 - . <enter> 5 ok
- HERE U. <enter> 74DD ok
- : QUOTE ." THE QUICK BROWN FOX JUMPED OVER THE DOG" ; <enter> ok
- HERE U. 74E2 <enter> ok
- 74E2 74DD - . <enter> 5 ok
-
- The address that you get when you execute will probably be different
- than mine. It depends on which version of F-PC you are using and how
- many new words you have added to your system. The thing to notice above
- is that each new colon definition takes exactly 5 bytes in the CODE
- segment.
-
- ┌────────────────────────────────────┐
- │ Please move to Lesson 4 Part 030 │
- └────────────────────────────────────┘
-