8. Variables and constants

8.1. A sure place: the variable
There are data that may sometimes be needed (the bottom of the stack, the title of the dictionary, our own counters), but not always. We can not keep them in the vermin, because we would be embarrassed if we had to avoid these even in the pad, which sometimes changed the title. There are variables in FORTH (also), which are word words like words of arithmetic or cycle organization.

 The FORTH variables work by putting an address on the stack; you can store the 16-bit data at this address, the variable being created to preserve it.

The FORTH interpreter itself uses variables, such as so-called. System variables.

8.1. Examples of system variables

8.2.0. S0
The word S0, which we learned in 7.3 , is a system variable .

8.2.1. BASE
A BASE contains the base number of the numeric system used when scanning and scanning. So far this was always 10. If we want to work in the 2-digit system, then the value of this variable is set to 2:

 2 BASE (makes the variable address on the stack) ! (which we made with this operation 2)

Let's try and see what life is in the binary system:

1 1 +.

For example, the 9th digit does not take the "stomach" of the interpreter, since we are in a two-digit system where it makes no sense. Surprisingly, however, the experimental reader finds that numbers 2, 3, which are also "meaningless", work. This is because the first few numbers are in the dictionary; these are often used. It speeds up the work of the interpreter by "finding them", without having to convert them.
The most commonly used two numbers are 10 and 16; these are set to FORTH basic words. Their operation is clear from their source text:

: DECIMAL 10 BASE! ;

: HEX 16 BASE! ;

How do I know at what point is my conversion count? It's easy to say, without thinking, you just need to get the base number out of the variable and give it up:

BASE @.

We get 10 responses What do we know about this? That the base number of the numeric system in the given system is represented by a 1 and a 0, that is, it is equal to the base number of the numeric system. We will go further if we write a smaller number:

BASE @ 1-.

From this answer we know that B, that is, 11 is the smallest one-digit number, we are in the 12th numerical system.
Now set the default number to 5 and define a new one in the 5th numerical system:

5 BASE!
: KIIR 10. ;

What happens if we run the KIIR in the 10-digit system? 5 results are obtained.
There are binary numbers in the dictionary words, with the definition of 0000 0000 0000 0101, this is in the decimal system 5.

8.2.2. OUT
We know that the FORTH parentheses on the screen write each character with EMIT. EMIT increases the OUT content of OUT, so if you reset the OUT then you can always find out how many characters have been displayed since the reset.
For example, imagine that we want to write the same lines, which consist of two (unpredictable) numbers; we want the first number to start at the 3rd and 3th on the screen, starting at position 13. We assume the two numbers are on the vermin. Write the word that says it as above:

 : BALRA-TABULAL    CR 0 OUT!    3 SPACES    . (n1 n2 ---) (reset the OUT variable) (3 spaces) (print the first number) (the OUT content now: how many characters have been written)       (already in this line) 13 OUT @ -    SPACES. ; ( Add 13 to the spelled ) (number of spells, then print) (the second number)

8.3. Creating new variables
Variables can be defined with VARIABLE (n ---). The expected value of the worm is the initial value of the variable. The name of the variable should be given immediately after VARIABLE:

0 VARIABLE SAJAT

With this, the word SAJAT appears in our dictionary (as we can confirm with VLIST at a glance). Function: Changes the address of the variable to the stack.
For example, imagine creating a list on the printer (by switching the handset off). How can I not know, one sure thing: 60 lines should be on one side. A new line in the listing program will always start with the CR word. Define the word CR by rolling 60 cards per line (12 EMITs), and then write down on the tab on how many cards it is.
Since we do not know what is going on with a stack in a list, and it's simpler, counting the rows and tabs in a variable:

0 VARIABLE LINE
0 VARIABLE LAPSZ

Both variables will have to be increased by one. To date, we know that the content of a variable would be somewhat increased by 1:

SORSZ @ (we cover the content)
1+
SORSZ! (the returned value will be returned)

FORTH Basic Word Simplifying Adds to Variables (or Any Memory Template) a

+! (n title ---)

which adds n to the one-word value on the title and stores the sum at the address. The +! Could be written in some way if it were not:

 : +!    SWAP OVER    @     +    SWAP! ; (n title ---) (title n title) (title n old content) (title new content)

To increase the content of the SORSZ:

1 SORSZ +!

When calculating rows, tabs, and more, you need to increase variables by just 1. Perhaps it is worth introducing a special word that uses the 1+ action:

: INC (title ---)
DUP @
1+ SWAP!
;

The following things must be done for the printer program after the display of 60 lines:

 : LAPDOB    12 EMIT    0 SORSZ!    LAPSZ @.    . "tab" CR    LAPSZ INC ; (---) ( countdown) ( resume counting ) (write the number of pages) (increase the number of counters )

We will start writing the script with just a few clicks; this is the right moment to set the LAPSZ start value:

: ELSO-LAPDOB
1 LAPSZ! LAPDOB
;

The listing program will have to start listing by calling ELSO-LAPDOB. The redefined CR:

 : CR    SORSZ INC (increase the count counter)    SORSZ @ 60 = (end of tab?)    IF LAPDOB    ELSE CR    ENDIF ;

8.4. Constants
If you want to preserve a value in a dictionary that we never change, it is simpler to use words that give the value not the title but the value itself to the worm. These are the constants. For example, the words 1, 2, 3 contained in the dictionary and the 1, 2, 3 values on the stack. This is BL, from which we get the code for the space.
Our own constants a

CONSTANT (n ---)

so we can define it. The thing is quite similar to defining variables. For example:

42 CONSTANT CSIL-COD

we defined this constant word called CSIL-KOD. CSIL-KOD 42 is put on the stack.

Summary of Chapter 8

The words learned in Chapter 8:

 VARIABLE (n ---) A word defining a variable. so we use: n VARIABLE xxx. This is how we created a word dictionary called xxx. The word xxx works by putting the variable's address on the stack. At this address, when the variable is created, n is the initial value. CONSTANT (n ---) Constant definition word. so use: n CONSTANT yyyy. This is how we created a dictionary word yyyy. The yyyy works by stacking the constant on the stack: this is the value that was given when the constant was created on the worm.

System Variables:

 BASE ( --- title ) The numbers are off and on. is the base number of the concurrent versions. OUT ( --- title ) The value of this variable is increased by EMIT (each character string). So you can use it to monitor and control the number of characters you are typing.

Other words:

 HEX (---) The BASE value is set to 16. DECIMALIZE (---) The BASE value is set to 10. +! (n title ---) Adds n to the 16-bit content of the address, the result is stored in the address-cn.

Examples

8.1. THE ? (title ---) source text source:

:? @. ;

Prints the 16-bit, signed value on the title. If, for example, A variable, it is

THE ?

line A is written.

8.2. Write a: = (title1 title2 ---) word that copies the 16 bit value on address2 to address1
(ie if A and B are two variables, then with AB: = action A is B value).

: = (title1 title2 ---)
@ SWAP!
;

8.3. Write a .BASE (---) word that writes BASE content decimally on the screen, but does not break it! (Or, more accurately, restoring it after it has been corrupted.)

 : .BASE)    BASE @ DUP    DECIMAL    .    BASE! ; (---) (now we have broken BASE) (but there is another instance of the original) (so we can reset it)

9. Where does the variable change? Getting to know the dictionary

9.1. The word counter
During the FORTH programming the dictionary size is constantly changing. When the word "top" of the dictionary is located, the first free space after the dictionary, where the following dictionary will be used, is interpreted by the interpreter DP (Dictionary Pointer, say: dictator pointer). The value of the DP variable is used a lot, so we have a separate word for reading:

: HERE (--- title)
DP @;

A 7.1. it was a section that the pad was at a constant distance from the top of the dictionary. This phenomenon is explained by the source text of the word PAD:

9.2. What's in the dictionary?
A dictionary element consists of the following parts:

• Name field: name of the word;
• chain: index to the previous word of the dictionary (if no previous word, 0);
• code field: a pointer to the code executed when the word is executed;
• parameter field: other data required to execute the word (eg variables for variables).

The parameter field - wholly arbitrarily abbreviated - is physically at the very end of the word. so after changing its definition, the word markers point exactly behind the new variable. If the value of the word counter is then set to 1, 2, etc. we increase this by multiplying the variables of the variable 1, 2, and so on. bytes. The basic word for increasing the word probe, ALLOT's source text:

: ALLOT (n ---)
DP +! ;

9.3. Long Variables
This hand has the option of defining double or longer variables.

87 VARIABLE DOUBLE

88 HERE!

2 ALLOT

DUPLA is therefore two words, the first word is the initial value of 87, and the second the 88th.
The initial values stored in the parser and the "broadening" of the parter are the following basic words:

:, (n ---)
HERE!
2 ALLOT
;

: C, (c ---)
HERE C!
1 ALLOT
;

The following series could have been written:

87 VARIABLE DUPLA 88,

Now we can define vectors, longer datasets. It is important to know that there are more convenient tools for FORTH - we will meet them in Chapter 15 - here we define a vector to better understand FORTH's operation (such as Chapter 15).
For example, define a 10-element "word" vector. Its parsing will be 20 bytes; the

0 VARIABLE VECTOR

defined by VECTOR 2 bytes of the VECTOR by 18 bytes:

18 ALLOT

The vector is worth something if its elements can be referred to separately. Thus, we write the word IK-ELEM (n --- address) waiting for the index of the element (serial number) of the element and return its address:

 : IK-ELEM    2 *    VECTOR +    2 - ; (n-address) (one element is 2 bytes) (the element with 1 index at the address) (starts with the VECTOR)

Summary of Chapter 9

The words learned in Chapter 9:

 DP ( --- title ) Changing the system. The interpreter keeps the title of the "top" of the dictionary, the first free byte after the dictionary. HERE ( --- title ) Specifies the value of the DP system variable. ALLOT (n ---) DP gives it n. . (n ---) Store n at HERE, in a word, increase the value of the word mark (DP) by 2. C (c ---) Store n at HERE by 1 byte, increasing the value of the word counter (DP) by 1.

Examples

9.1. Write a pencil registration system! The system has three types of pencils:

0 CONSTANT RED
1 CONSTANT KEK
2 CONSTANT ZOLD

Create a 3-element variable in which each pen count can be kept. The pen count counters have an initial value of 0. Next, write the following words:

• CERUZA-BE (n color ---)
Increases the pen number counter with the specified n value. So, for example:
1 ZOLD CERUZA
(which is the same as saying this: 1 2 CERUZA-ON)
, the green pencil count increases by one.
• CERUZA-OFF (n color ---)
If the color pen number for the color is at least n, then n is zero. If you do not, it will give you an error message and leave the count value unchanged.
• CERUZAK (color ---) Displays the pencil count for the color.
A description of the "long variable" for pencil counting:

0 VARIABLE CK 0, 0,

Define a SZLO (color-tag) word that produces the pen-numbering pen for the color from the color code:

 : SZLO    2 *    CK + ; (color address) (color code multiplied by 2) (counters are worded) (first counters address)

From here we have a simple business:

 : CERUZA-BE (n color ---)    SZLO +! ; : CERUZA-OFF (n color ---)    SZLO    OVER OVER @    > IF "There is not enough"       DROP DROP    ELSE       SWAP MINUS SWAP +!    ENDIF ; : CERUZAK (color ---)     SZLO @. ;

9.2. Write a NULL (n ---) word that n sets the byte of 0 with the predefined variable parser. So, for example, the

0 VARIABLE HUSZ 18 NO

command line generates a variable with 20 bytes parsing; of which 2 bytes are VARIABLE, and 18 bytes 0 start bytes are NULL.

: NULL (n ---)
0 DO 0 C, LOOP;

 file: /Techref/language/FORTH/z80fig-Forth1_1g_files/variables.htm, 54KB, , updated: 2018/11/8 21:23, local time: 2024/5/23 08:20, TOP NEW HELP FIND:  44.210.83.132:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! 8. Variables and constants

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

 PICList 2024 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.