6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu May 16, 2024 7:24 pm

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Forth-83 VARIABLE
PostPosted: Sat Jul 06, 2019 10:03 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 861
In the definition of VARIABLE , The Forth-83 Standard states that when a VARIABLE is created, two bytes are alloted in its parameter field and that an application is responsible for initializing the contents of a variable which it creates. Does anyone know if this is a prohibition against a Forth-83 system initializing variables to zero at the moment of creation?


Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Sun Jul 07, 2019 3:52 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8437
Location: Southern California
I haven't paid much attention, because I theorize that a variable initialized to zero is just about as likely to produce wrong results as any other number, if the programmer forgets to have his program initialize it before reading it.

In "Starting Forth" however, Brodie defines VARIABLE as:
Code:
: VARIABLE     CREATE   0 ,   ;


In the case of VARIABLE, my entry to 6502 Forth was with a metacompiler I bought for the automated test equipment project at work, and the metacompiler, supposedly Forth-83, would not let me do things like add an offset to the address of the variable's data space. The error message was that the target's RAM (and therefore its variable space) was not accessible to the metacompiler! Of course not! I know that! All I want is the address! (Yeah, I yelled at the monitor.) IIRC, it wouldn't let me redefine VARIABLE either, so I made a word VAR which was just the following (shown with CONSTANT too):
Code:
: CONSTANT   CREATE  ,    const runtime ;

: VAR ( n -- )                               \ n is how many bytes you
   THERE SWAP ALLOT CONSTANT ;  IMMEDIATE    \ want in the variable.

\ Use VAR so the same code will work with both the metacompiler and
\ compilation on the target itself.  A typical syntax example would be
\    WSIZE VAR FLLEN
\ to get a variable named FLLEN with WSIZE bytes allotted to it.

(THERE keeps track of where you are in RAM usage, while HERE keeps track of where you are in ROM usage. Program material compiled by the metacompiler goes in ROM, so obviously a variable's code space and data space have to be separate. In the final system, HERE and THERE become aliases, and anything compiled on, and by, the target 6502 computer itself goes into RAM, since the target can't write to its own ROM anyway.)

I don't remember if the IMMEDIATE had something to do with another metacompiler problem.

Requiring the number of bytes breaks with the standard, but it always bugged me that if you want for example six bytes, Forth 83 required saying,
Code:
VARIABLE  FOOBAR   4  ALLOT

You could say "6 ALLOT" to match the array size you want, but then you're wasting two bytes.

When I wrote my '816 (actually '802) Forth assembly-language source (not using a metacompiler), I went back to the more normal VARIABLE name and usage, for others' sake. It commas in a 0; but if you follow it up by ALLOTting more space, that space does not get initialized. I think it's best to never assume anything is automatically initialized to what you want.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Sun Jul 07, 2019 4:23 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1930
Location: Sacramento, CA, USA
I am more troubled by the portability issues of stating "two bytes" than I am on the issue of initialization.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Sun Jul 07, 2019 4:27 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8437
Location: Southern California
barrym95838 wrote:
I am more troubled by the portability issues of stating "two bytes" than I am on the issue of initialization.

A typical usage of my VAR word would be
Code:
WSIZE  VAR  FOOBAR
where WSIZE is of course a pretty standard constant that puts the number of bytes, address units, whatever, on the stack (and in my case, a 2).

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Sun Jul 07, 2019 5:11 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 861
GARTHWILSON wrote:
I haven't paid much attention, because I theorize that a variable initialized to zero is just about as likely to produce wrong results as any other number, if the programmer forgets to have his program initialize it before reading it.

I agree. That's not why Fleet Forth initializes variables to zero. I did it to save a few bytes. Fleet Forth's vocabularies have the address of the latest word's link field in the first cell, the address of the parent vocabulary in the second cell, and use the third cell as part of the VOC-LINK chain.
A new vocabulary will have a zero in its first cell ( because it is empty ), so VOCABULARY is defined like this:
Code:
HEX
: VARIABLE  ( -- )
   CREATE 0 , ;
: VOCABULARY  ( -- )
   VARIABLE                // LATEST WORD'S LFA
   CURRENT @ ,             // PARENT VOCABULARY
   HERE VOC-LINK DUP @ , ! // VOC-LINK CHAIN
   DOES>
      CONTEXT ! ;

Note: the // ( double slash) is a workaround, I first saw in Blazin' Forth, for the C64 not having \ ( backslash)
barrym95838 wrote:
I am more troubled by the portability issues of stating "two bytes" than I am on the issue of initialization.

A good reason to define variable as
Code:
: VARIABLE  ( -- )
   CREATE 0 , ;

since that is independent of cell width.


Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Fri Jan 17, 2020 6:18 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8437
Location: Southern California
I see my word VAR above appears to be the same as Forth, Inc.'s BUFFER: . Elizabeth Rather's book "Forth Application Techniques" says on page 63 that BUFFER: is not a standard word, but it's available in most Forth, Inc. systems.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Fri Jan 17, 2020 8:38 pm 
Offline

Joined: Sun May 13, 2018 5:49 pm
Posts: 248
JimBoyd wrote:
In the definition of VARIABLE , The Forth-83 Standard states that when a VARIABLE is created, two bytes are alloted in its parameter field and that an application is responsible for initializing the contents of a variable which it creates. Does anyone know if this is a prohibition against a Forth-83 system initializing variables to zero at the moment of creation?

I think this is a "for portability of software written in Forth, don't expect the underlying Forth to initialize anything for you - if you want the variable to start at zero then your program should initialize it" kind of thing. In that case, the programmer writing the Forth may initialize variables to anything (or even not bother and just advance the compiler pointer).

By ANS94, they at least changed it to "reserve one cell", but it still says: "A program is responsible for initializing the contents of the reserved cell." I think that wording is slightly clearer on the intent.

Tali Forth 2 variables are initialized to zero when they are created.


Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Sat Dec 11, 2021 2:45 am 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 861
GARTHWILSON wrote:
Requiring the number of bytes breaks with the standard, but it always bugged me that if you want for example six bytes, Forth 83 required saying,
Code:
VARIABLE  FOOBAR   4  ALLOT

You could say "6 ALLOT" to match the array size you want, but then you're wasting two bytes.


What about:
Code:
CREATE FOOBAR  6 ALLOT



Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Sat Dec 11, 2021 4:11 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8437
Location: Southern California
JimBoyd wrote:

What about:
Code:
CREATE FOOBAR  6 ALLOT


I don't remember for sure, but this might have been one of the problems with the metacompiler I started with, where if I subsequently referenced FOOBAR, it would give error messages saying that the target's memory was not available at metacompile time. So dumb. I'd yell at the monitor, "I know that, dummy! I just want the address!" But I would develop code interactively on the target, then when it was working, copy it into the source code for the metacompiler so I could add it to what I had in EPROM; and I wrote the VAR word using CONSTANT (which the metacompiler had no trouble with) so I wouldn't have to modify everything to go from one form to the other.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Forth-83 VARIABLE
PostPosted: Sun Dec 12, 2021 8:38 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 861

That must have been a really bad metacompiler. No doubt, the worst was that it was a black box.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: