Discussion:
16 bits array cells being erased
(too old to reply)
jd
2005-01-21 17:09:26 UTC
Permalink
Hi,

I am running MacForth 5.2 on a Mac and have very strange things
happening with my arrays. When i create an array and enter a value in
the first cell as follows:
create MyArray 12 allot
221 MyArray !

if i view the content of the first cell the value is right:
MyArray @ .
221

However if i now enter a value in the second cell of the 16 bit array:
230 MyArray 2 + !

and check the content of the first cell again, i obtain 0 : my first
cell has been erased!!

Do i do anything wrong? Can anyone help.
Thanks in advance,
jd
ward mcfarland
2005-01-22 11:33:18 UTC
Permalink
Post by jd
Hi,
I am running MacForth 5.2 on a Mac and have very strange things
happening with my arrays. When i create an array and enter a value in
create MyArray 12 allot
221 MyArray !
221 == hex 000000DD

This stores DD in MyArray + 3 (3 = byte offset)
Post by jd
221
230 MyArray 2 + !
This stores hex 000000E6 in
MyArray + 2 (2 = byte offset)

+DUMP can be your friend here to see what is being stored where.


For 4 bytes per array element, you need to do

create MyArray 12 4 * allot ( to make an array of 12 longs)
or
create MyArray 12 CELLS allot

and access as:

230 MyArray 2 4 * + !
or
230 MyArray 2 cells + !



If you want a simpler style, use MacForth's 1ARRAY word as follows:

12 4 1ARRAY MyArray

Then

221 0 Myarray !
230 1 MyArray !

0 MyArray @ .
221

1 MyArray @ .
230



Now, if you wanted only 1 BYTE per element originally, then you should
have used C@ and C! to access the elements.

-- ward
ward mcfarland
2005-01-23 12:52:44 UTC
Permalink
oops. read the body of your message, and not the title.

Roelf gave you the correct solution for 16-bit access.

<W@ may be better suited than W@, depending on your data, as it
sign-extends the fetched data.

Roelf Toxopeus
2005-01-22 23:03:43 UTC
Permalink
Post by jd
Hi,
I am running MacForth 5.2 on a Mac and have very strange things
happening with my arrays. When i create an array and enter a value in
create MyArray 12 allot
221 MyArray !
221
230 MyArray 2 + !
and check the content of the first cell again, i obtain 0 : my first
cell has been erased!!
Do i do anything wrong? Can anyone help.
Thanks in advance,
jd
a cell in MacForth is 32 bits

use w@ and w! for 16 bit storage and retrieval as in your case above.
221 Myarray w!
230 MyArray 2 + w!

use ! and @ for 32 bit stuff
221 Myarray !
230 MyArray 4 + !

(just in case: use c@ and c! for 8 bit, sorry characters)
Continue reading on narkive:
Loading...