Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Conklin E.K.Forth programmer's handbook.2000.pdf
Скачиваний:
321
Добавлен:
23.08.2013
Размер:
2.04 Mб
Скачать

 

 

 

Forth Programmer’s Handbook

TUCK

 

( x1 x2 — x2 x1 x2 )

Core Ext

 

 

 

Place a copy of the top stack item below the second stack item.

 

 

 

 

2.1.3 Memory Stack Operations

 

 

 

 

This category of operations allows you to reference memory by using

 

 

 

addresses that are on the stack.

 

 

 

 

 

 

Glossary

 

 

 

!

 

 

( x a-addr — )

Core

 

 

 

Store x at the cell at a-addr, removing both from the stack. “store”

+!

 

 

( n a-addr — )

Core

 

 

 

Add n to the contents of the cell at a-addr and store the result in the cell at a-addr,

 

 

 

removing both from the stack. “plus-store”

 

2!

 

 

( x1 x2 a-addr — )

Core

 

 

 

Store the cell pair x1 x2 into the two cells beginning at a-addr, removing three

 

 

 

cells from the stack. “two-store”

 

2@

 

 

( a-addr — x1 x2 )

Core

 

 

 

Push the cell pair x1 x2 at a-addr onto the top of the stack. “two-fetch”

@

 

 

( a-addr — x )

Core

 

 

 

Replace a-addr with the contents of the cell at a-addr. “fetch”

 

C!

 

( b c-addr — )

Core

 

 

 

Store the low-order byte of the second stack item at c-addr, removing both from

 

 

 

the stack. “C-store”

 

C+!

 

( b c-addr — )

common usage

 

 

 

Add the low-order byte of the second stack item to the byte at c-addr, removing

 

 

 

both from the stack. “C-plus store”

 

C@

 

( c-addr — b )

Core

 

 

 

Replace c-addr with the contents of the byte at c-addr. The byte fetched is

 

 

 

stored in the low-order byte of the top stack item, with the remaining bits

 

 

 

cleared to zero. “C-fetch”

 

Forth Fundamentals 35

Forth Programmer’s Handbook

2.1.4 Return Stack Manipulation Operations

The return stack is so named because it is used by the Forth virtual machine (VM) to keep track of where Forth words will return when they have finished executing. When a high-level Forth word invokes a previously defined Forth word, the address of the next word to be executed is pushed onto the return stack; it will be popped off the return stack when the called word is finished, so execution can resume where it left off.

The return stack is a convenient place to keep frequently used values (by using the words >R, R@, and R>), but it must be cleared before an executing word reaches the end of the current definition, or the virtual machine will return to the “address” on the return stack. This behavior can be useful; for example, on many systems, the definition:

: VECTOR ( xt -- ) >R ;

will act like the word EXECUTE, but will only execute : definitions. VECTOR works by pushing a word’s execution token onto the return stack. Therefore, when the end of the definition pops the return stack into the VM’s register I (or its implementation-dependent equivalent), the VM will begin to execute the word whose address was on the stack for VECTOR. This works on all systems in which xts are actual return addresses.

If you use the return stack for temporary storage, you must be aware that this is also a system resource, and obey the following restrictions:

!Your program must not access values on the return stack (using R@, R>, 2R@, or 2R>) that it did not place there using >R or 2>R.

!When inside a DO loop, your program must not access values that were placed on the return stack before the loop was entered.

!All values placed on the return stack within a DO loop must be removed before

I, J, LOOP, +LOOP, UNLOOP, or LEAVE is executed.

!All values placed on the return stack within a definition must be removed before the end of the definition or before EXIT is executed.

The glossary below documents operations that involve both the return stack and the data stack.

36 Forth Fundamentals

Forth Programmer’s Handbook

Glossary

 

 

 

2>R

( x1 x2 — ) ( R: — x1 x2 )

Core Ext

 

 

Pop the top two cells from the data stack and push them onto the return stack.

 

 

“two-to-R”

 

2R>

( — x1 x2 ) ( R: x1 x2 — )

Core Ext

 

 

Pop the top two cells from the return stack and push them onto the data stack.

 

 

2R> is the inverse of 2>R. “two-R-from”

 

2R@

( — x1 x2 ) ( R: x1 x2 — x1 x2 )

Core Ext

 

 

Push a copy of the top two return stack cells onto the data stack. “two-R-fetch”

>R

( x — ) ( R: — x )

Core

 

 

Remove the item on top of the data stack and put it onto the return stack. “to-R”

R>

( — x ) ( R: x — )

Core

 

 

Remove the item on the top of the return stack and put it onto the data stack.

 

 

“R-from”

 

R@

( — x ) ( R: x — x )

Core

 

 

Place a copy of the item on top of the return stack onto the data stack. “R-fetch”

 

 

Counting LOOPs (DO), Section 2.5.2

 

References

 

 

 

EXECUTE, Section 2.5.6

 

2.1.5 Programmer Conveniences

The words in this section are intended as programming aids. They may be used interpretively at the keyboard, or inside definitions—except for ' (tick); the equivalent of ' inside a definition is the word [']. Because compiling new definitions in Forth is so quick, you are encouraged to create, test, and debug definitions to aid in developing an application.

Forth Fundamentals 37

Forth Programmer’s Handbook

Glossary

 

 

 

' <name>

( — xt )

Core

 

 

Search dictionary for name and leave its execution token on the stack. Abort if

 

 

name cannot be found. “tick”

 

.S

( — )

Tools

 

 

Display the contents of the data stack using the current base. Stack contents

 

 

remain unchanged. “dot-S”

 

?

 

( a-addr — )

Tools

 

 

Fetch the contents of the given address and display the result according to the

 

 

current conversion radix. “question”

 

 

 

Equivalent to the phrase: @ .

 

DUMP

( addr +n — )

Tools

 

 

Display the contents of a memory region of length +n starting at addr:

 

<addr> <+n> DUMP

Output is formatted with the address on the left and up to eight values on a line. The output conversion radix is the current value of BASE (on some systems this word always outputs in hex). Two cells are removed from the stack.

ENVIRONMENT?

( c-addr u — false | i*x true )

Core

 

 

This word is used to inquire about the values of system parameters and the

 

 

existence of options. See Section 3.2 for a full description. “environment-query”

WORDS

 

( — )

Tools

 

 

List all the definition names in the first word list of the search order.

 

 

 

['], Section 4.1.3

 

 

References

 

 

 

 

Environmental interrogation, Section 3.2

 

 

 

Search orders, Section 4.6.1

 

38 Forth Fundamentals

Соседние файлы в предмете Электротехника