please dont rip this site

7. Getting to know the memory

7.1. Byte Operations
The computer memory is a sequence of bytes numbered and numbered by them. The words of the FORTH memory manager make these bytes freely readable and overwritable, regardless of whether they are in the middle of the interpreter code, in the stack, or in a somewhat "gentler" place. The programmer's job is not to get in the wrong places. A good address for skinning, where you can store data temporarily: pad (pronounced ped). The pad means a notepad, a block. The address of the pad is a

PAD (--- title)

word provides.
Caution! The pad is above the dictionary area, at a constant distance from the top of the dictionary (byte from the last word defined). Therefore, when new words are defined or forgotten, the pad position changes. It is best to use the data in the pad in the same word that we put it into. For permanent data retention, the variables are provided, which will be discussed in Chapter 8 .
Words for memory bytes:

C @ (title --- c) specify the contents of the byte stored in the title;
C! (c title ---) ac stores the bytes on the title (the contents of the previous byte are lost).

In the description of the stack effect, the title means a memory address; type is a one-word, unsigned value.
Write a word that waits for characters from the keyboard, right up to ENTER, and after ENTER, rewrite the resulting string. The backspace character (whose code is 8) responds by deleting the last scanned character. We keep the characters in the pad while we are writing.

      KEY DUP 13 -
will be the first character)
(vermen is the next character's title)
(it will be false if it was ENTER)
         (here we get if you have not already ENTER)
      DUP 8 =
      IF DROP
        PAD -
        IF 1 ENDIF
        OVER C!
        1 +

we do not want to delete more)
(how much is there?)
(if not)

(store the code)
(the next code one after the other)
(we will have to store it)
         (here comes the effect of ENTER)
         (the address code of the vermen)
      DROP PAD
      DO RC @
(the first character to be entered)

Typing the characters could also be done with the TYPE key word. TYPE, along with other basic words that handle byte series, can be found in the glossary at the end of this chapter. A convenient way to scan character sequences is

EXPECT (max limit ---)

a word that drops the characters scanned from the keyboard from the specified address to the memory. Scanning characters will take up to the first carriageway or the "max" character scans. By the end of the series, binary zeros are stored in the memory. The backspace character deletes the last character in the series.

7.2. Voice and Dual Operations
Memory is not only 8-bit, that is, bytes, but also words and doubles.
The words used are:

@ (cim --- content) the 2 bytes on the title and the following address, that is, the word on the stack;
! (n title ---) stores the n 16 bit value on the address and the next byte (the previous contents of 2 bytes will be lost);
2 @ (title --- d content) it puts a double word on the title and the next 3 bytes.
2! (title d ---) the DWORD is stored on the title and the next 3 bytes (the previous contents of 4 bytes will be lost).

So the

0 PAD 2!

the bytes on the PAD, PAD + 1, PAD + 2 and PAD + 3 addresses are reset.

7.3. Convenient Stack
Management With the words handling memory, you can "go out of the way" to the stack as it is ultimately just a memory space. All you have to do is know what titles the stack is about. The

SP @ (--- title)

refers to the title of the top of the stack (before the execution of SP @), that is, if the stack is not empty, the title of the top element.
So if the stack is not empty then that

SP @ @

series is equivalent to a DUP operation. If we experiment with a bit with the SP8 and SP @, we soon realize that SP @ gives a smaller title, the more elements in the stack.

The stack is "down" in the opposite direction to the memory address.

The address of the stack of the stack, the address given by SP @ in an empty stack, FORTH keeps a permanent address; the latter address is placed on the stack by the word S0. The bottom of the stack is therefore

S0 @

. (See the following figure.)

Now we can write the 1.6. stage , non-standard management stack in words (DEPTH, .STACK, PICK and ROLL). You should read the source texts of those who like to learn from other programs; none of the novelties are provided.

   SP @
   S0 @
   - MINUS
   2 /
(--- stack depth)
(on the top)
(the bottom)
(that's bytes pt include elements of the stack)
(so many items available)

: STACK (---)
   SP @ S0 @ =
      CR. "Stack is empty"
      SP @ S0 @ SWAP (from top to bottom)
      DO CR R @ 5 .R 2 + LOOP

Before we write the words PICK and ROLL, let's wonder: what do we do if someone wants to look too deep in the bottom of the stack? What if someone wants to get the third of the two elements of the stack? Assume that this can only be a programming mistake and protect the programmer from the consequences of his mistake, so stop everything! That's right


weave. Quit interrupts almost everything at all levels and returns the control to the external interpreter, which starts to wait for a new command line. Quit with QUIT is not followed by OK.
The text of PICK can be followed without much explanation.

: PICK (n --- n1)
   DEPTH 1- (with no nullity)
   OVER <
   IF (if the stack has fewer than n elements)
      "PICK error" QUIT
      SP @
      SWAP 2 * (2 * n)
      + @

The stack when PICK is started:

The n ROLL (n now refers to the number on the stack) first copies a n-th top element to the top of the stack with a PICK, which is to be "thrown" and then sums up the "old" n- n-1, n-1 to n-2, and so on. Finally, from n-1, all elements are "slid down", and at the top two copies of the n-eds; the excess top is discarded.

   DUP> R

(copy "one copy")
(copy element n to the roof)
         (Copied in DO ... LOOP cycle)
   SP @
   DUP 2+
   SWAP R> 2 * +
      R 2- @
   -2 + LOOP
(the title of
the original item)
(the title of the original item)

(copy the item to be

(duplicate) (discard the item n. unnecessary) (copy)

What was that about?
Summary of Chapter 7

The words taught in Chapter 7:

@ (title --- n) The 16-bit content of the title.
! (n title ---) Store n by 2 bytes from the title.
C @ (title c ---) It gives the title 8-bit content.
C! (c title ---) Store in the byte on the address ct.
PAD ( --- title )
The address of the memory space for temporary storage. This title changes when changing the size of the dictionary (new words are defined, "forgot", etc.)
QUIT (----)
Interrupt the program run. QUIT causes the interpreting of the interpreter to be interrupted, and the interpreter waits for a new command line.
EXPECT (max title ---)
Scan text from the keyboard to the address. The last character you entered can be deleted with the delete character. Scan to the first carriage return character or to the "max" character (without the deletions) continues. In the memory at the end of the text (one or more) is binary 0.
S0 ( --- title ) It gives the title of the bottom of the stack.
SP @ ( --- title ) The top of the stack (top of the stack if the stack is not empty) (before the SP @ execution). If the stack is empty, this address is the same as S0 @ .

Convenient basics to handle Byte series:

TYPE (title length ---) Prints the title on the title at the specified length.
CMOVE (where to where length ---) Moving byte series from one title to the next. Transmission starts at lower addresses.
FILL (title length character ---) The memory is filled with long characters from the title.
ERASE (title length ---) The memory is loaded from the address by a long 0 bytes.
BLANKS (title length ---) The memory is filled with long spaces from the title.


7.1.a. How to write 2 @ (title-d-content) and 2! (title d ---)?

: 2 @ (title --- d content)
   DUP @ (the contents of the first memory word)
   SWAP 2+ @ (for the second)

: 2! (d-content title)
   > R (we put the address on the vibe)
   R 2 +! (fill the second memory slot first)
   R>! (the first one for the second time)

7.1.b. How to write TYPE (title length ---)?
FIG version:

   -DUP IF
      OVER +
      SWAP DO
        RC @ EMIT
(address length ---)
(if the length is not 0)

(bytes between the two addresses
(if it was 0, the address on the vermen is)

7.1.c. How to write CMOVE (from length to length)?
A FORTH solution:

   0 DO
     OVER C @
     OVER C!
     1 + SWAP 1+ SWAP
(from the length to ---)

(let's get one byte)
(let's put it to the other location)
(both titles will be added)

(the two titles do not have to)

However, in general (like fig-Forth), CMOVE is not in FORTH but in a machine code.

7.1.d. How to write FILL (title length byte ---)?
The FIG solution:

   SWAP> R
   OVER C!
   DUP 1+ R> 1-
(address length byte ---)
(address byte)
(address title + 1 length-1)

7.1.e. How to write the words ERASE and BLANKS ?
FIG version:



7.2. Why is not DEPTH so good?

: DEPTH (--- verity)
   S0 @ (the bottom)
   SP @ (the top)
   - 2 /

The first S0 puts a new element on the stack, SP @ is no longer in the original state. This version is correct:

: DEPTH (--- verity)
   S0 @ (the bottom)
   SP @ (the top)
   - 2 /

7. 3. In many FORTH, there is a word S <> (title1 title2 length --- f) that compares the length length beginning with address1 and address 2 but not in fig-Forth. The returned signal is false, ie 0, if the two characters are the same, positive if the first is greater, negative if the second one. (The two equal lengths are the same if their corresponding characters are the same, and if not, then the comparison of the code of the first non-matching pair of characters gives the relationship between the two.) Write the word S <>.

: S <>
   0 PAD!
(title1 title2 length --- f)
      (this will be the signal we give back)
      (initially assume that the two series)
      (same, if not, change it)    
   0 DO
    OVER C @ OVER C @
    -DUP IF
    1+ SWAP 1+ SWAP
   PAD @

(the difference between the two characters)
(if the two characters are different)
(this will be the answer signal)
(and finish the operation)

(move on with the titles)

(titles are no longer needed)

7.4. If we write a memory area with the words TYPE (title length ---), there are very strange things, since there are all sorts of control characters between them. Write a TYPE2 (title length ---) word that prints the characters between 32 and 126 (punctuation, digit, uppercase, lowercase) and points to the rest.

The program is almost literally the same as TYPE, only the control characters must be replaced by the code of the point (56) before the characters are written:
      RC @
      DUP DUP
      32 <SWAP 126> OR
        DROP 46
(title length ---)

(if control character)

(write the character)

For example, you can try TYPE2 for example:


The answer is: ... AAA

7.5. Write a BOTTOM (--- n) that copies the bottom of the stack to the top of the stack.

   SP @ S0 @ - IF (there is something on the stack)
      S0 @ 2- @
   ELSE. "Stack is empty" QUIT

file: /Techref/language/FORTH/z80fig-Forth1_1g_files/memory.htm, 57KB, , updated: 2018/11/8 21:25, local time: 2024/5/23 09:20,

 ©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!

<A HREF=""> 7. Getting to know the memory </A>

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.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host, 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.

Welcome to!