0 VARIABLE LL ALLOT
Author Message
0 VARIABLE LL ALLOT

Hi All!

Will you teach me how can I fill array in such case like:

0 1 2 3 4 5 6 ....

Will you send me a sample code

Mon, 26 Nov 2001 03:00:00 GMT
0 VARIABLE LL ALLOT

Quote:

> Will you teach me how can I fill array in such case like:

> 0 1 2 3 4 5 6 ....

Suppose you want to fill an array of 10 cells:

10 CONSTANT #CELLS  CREATE MY-ARRAY #CELLS CELLS ALLOT

: FILL-ARRAY
#CELLS 0 DO  I  MY-ARRAY I CELLS +  !  LOOP ;

However, if you are working with 10 chars:

10 CONSTANT #CHARS  CREATE MY-ARRAY #CHARS CHARS ALLOT

: FILL-ARRAY
#CHARS 0 DO  I  MY-ARRAY I CHARS +  C!  LOOP ;

You can get the same results with CREATE DOES>:

: ARRAY                   \ Defines an array 'n' cells long
CREATE ( n -- )        CELLS ALLOT
DOES>  ( n -- a-addr ) SWAP CELLS + ;

: CARRAY                  \ Defines an array 'n' chars long
CREATE ( n -- )        CHARS ALLOT
DOES>  ( n -- c-addr ) SWAP CHARS + ;

10 CONSTANT #ELEMENTS
#ELEMENTS ARRAY ARRAY-1  #ELEMENTS CARRAY ARRAY-2

: FILL-1   #ELEMENTS 0 DO  I DUP ARRAY-1 !   LOOP ;
: FILL-2   #ELEMENTS 0 DO  I DUP ARRAY-2 C!  LOOP ;

Hope this helps.

--------------------
Federico de Ceballos

Mon, 26 Nov 2001 03:00:00 GMT
0 VARIABLE LL ALLOT

Federico de Ceballos ??? a ????

Quote:

>> Will you teach me how can I fill array in such case like:

>> 0 1 2 3 4 5 6 ....

>Suppose you want to fill an array of 10 cells:

>10 CONSTANT #CELLS  CREATE MY-ARRAY #CELLS CELLS ALLOT

>: FILL-ARRAY
>  #CELLS 0 DO  I  MY-ARRAY I CELLS +  !  LOOP ;

>However, if you are working with 10 chars:

>10 CONSTANT #CHARS  CREATE MY-ARRAY #CHARS CHARS ALLOT

>: FILL-ARRAY
>  #CHARS 0 DO  I  MY-ARRAY I CHARS +  C!  LOOP ;

>You can get the same results with CREATE DOES>:

>: ARRAY                   \ Defines an array 'n' cells long
>  CREATE ( n -- )        CELLS ALLOT
>  DOES>  ( n -- a-addr ) SWAP CELLS + ;

>: CARRAY                  \ Defines an array 'n' chars long
>  CREATE ( n -- )        CHARS ALLOT
>  DOES>  ( n -- c-addr ) SWAP CHARS + ;

>10 CONSTANT #ELEMENTS
>#ELEMENTS ARRAY ARRAY-1  #ELEMENTS CARRAY ARRAY-2

>: FILL-1   #ELEMENTS 0 DO  I DUP ARRAY-1 !   LOOP ;
>: FILL-2   #ELEMENTS 0 DO  I DUP ARRAY-2 C!  LOOP ;

>Hope this helps.

>--------------------
>Federico de Ceballos

_________________________________________________
=================================================

Looks nice, but I have some question now:

1) How can I test these operations;

2) Will you tecah me about words CELLS and CREATE

Mon, 26 Nov 2001 03:00:00 GMT
0 VARIABLE LL ALLOT

Quote:

> Federico de Ceballos ??? a ????
> [code]

This also works:

: 0123ARRAY CREATE ( u -- ) 0 ?DO I , LOOP ;

12 0123ARRAY Alexander's

Quote:

> 1) How can I test these operations;

: ?S ( a u -- ) 0 ?DO DUP ? CELL+ LOOP DROP  ;

Alexander's 12 ?S

Or:

Alexander's 12 CELLS DUMP

Quote:
> 2) Will you teach me about words CELLS and CREATE

See: http://www.forth.org/dpans/dpans.html

Or:  http://www.albany.net/~hello/simple.htm

--
Leo Wong

http://www.albany.net/~hello/
The Forth Ring: http://zForth.com/

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Mon, 26 Nov 2001 03:00:00 GMT
0 VARIABLE LL ALLOT

Quote:
"Alexander Ryaboukha"  writes:

[ previous deleted ]

Quote:
> Looks nice, but I have some question now:

> 1) How can I test these operations;

> 2) Will you tecah me about words CELLS and CREATE

The dpANS-6 document is available in several formats, including HTML, from

http://www.forth.org/fig.html

or from

http://www.taygeta.com

The dpANS-6 document is the best way to study the definitions of words
like CELLS or CREATE...DOES> .

I recommend downloading the Forth Primer from various places where it
is stashed, including the above and

http://www.users.zetnet.co.uk/aborigine/forth.htm

It includes F-PC in some late version plus Jack Brown's lessons (also
C.H. Ting's lessons), which they were kind enough to make available.
These are designed to run with F-PC.

You might want to read the Forth handbook from Forth, Inc. I regret to
say that there are no easily available introductory Forth texts on book
store shelves right now. But Tim Hendtlass's book, "Real Time Forth" is
available in electronic form from some of the above Web sites. I recommend
it since it was his class notes for a Forth course.

This is the best I can do for you right now.

--
Julian V. Noble

"Elegance is for tailors!"    -- Ludwig Boltzmann

Mon, 26 Nov 2001 03:00:00 GMT
0 VARIABLE LL ALLOT

Quote:

>"Alexander Ryaboukha"  writes:

> [ previous deleted ]

>> Looks nice, but I have some question now:

>> 1) How can I test these operations;

>> 2) Will you tecah me about words CELLS and CREATE

>The dpANS-6 document is available in several formats, including HTML, from

> http://www.forth.org/fig.html

>or from

> http://www.taygeta.com

>The dpANS-6 document is the best way to study the definitions of words
>like CELLS or CREATE...DOES> .

A language standard is not designed as an introductory document, but
a technical specification for those implementing a Forth system or
intending to write significant, hopefully portable applications.  In many
respects its language is formal and not "friendly."  However, it certainly
is the authoratative source, and would clear up mistakes like

0 VARIABLE LL  ALLOT

for example (VARIABLE hasn't taken an argument since 1983, so the
0 would be an argument to ALLOT, making it a no-op).

Quote:
>I recommend downloading the Forth Primer from various places where it
>is stashed, including the above and

> http://www.users.zetnet.co.uk/aborigine/forth.htm

>It includes F-PC in some late version plus Jack Brown's lessons (also
>C.H. Ting's lessons), which they were kind enough to make available.
>These are designed to run with F-PC.

>You might want to read the Forth handbook from Forth, Inc. I regret to
>say that there are no easily available introductory Forth texts on book
>store shelves right now. But Tim Hendtlass's book, "Real Time Forth" is
>available in electronic form from some of the above Web sites. I recommend
>it since it was his class notes for a Forth course.

The Forth Programmer's Handbook was designed as an introduction
to Forth for programmers experienced in other languages and generally
knowledgable about computers and programming issues.

It's available from our web site or www.forth.org (also Amazon).

Cheers,
Elizabeth

Mon, 26 Nov 2001 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages