Memory Words, Bytes and Bits

gerrymcc

New Member
This is a very naive question, but I can not find an answer which I fully understand and can feel confident about, when writing my first piece of code. This is despite Searching through the Forum. It concerns bits, bytes, words and variables.

A word consists of two 8bit bytes. The manual calls these b1 (upper, most significant 8 bits) and b0 (lower, least significant 8 bits), of word w0. Looking through several programs, bytes are referred to as b?, and are used as counters and for general control and conditional stepping. A very often used one, is b0. The manual for the 28X1 I am using for my first programming effort says it has 31 individual bits, called bit?, which can be set and read.

My question: If I set or reset bit1 (the second of the available 32 individual bits), does this change the value of the second least significant bit of byte called b0, or the second least significant bit of the lower byte of word w0?

As part of trying to understand the memory structure, I am also puzzled by the entry for the 28X1 on Page 9 of Section 2 of the manual. Referring to the 28X1, it says it has words w0-13 (28 8-bit bytes), bytes b0-27 (28 bytes), bits0-31 (4 bytes), giving 60 bytes in total. But, the first column says it has 28 bytes.

Is this part of the same question above, or am I fundamentally missing something?
A references to an answer about this in past Forum answers would be welcome as a less effort answer.

Thank you.
 

BCJKiwi

Senior Member
Umm.. yes well..,

The three different statements are correct,
b0 thru b28
w0 thru w13
bits 0 thru 31

These all overlay each other all starting at the same place;

bit0 is the first bit of b0
b0 is the first byte of w0

From the manual;
"However for larger numbers two byte variables can be combined to create a word
variable, which is capable of storing integer numbers between 0 and 65335.
These word variables are labeled w0, w1, w2 etc... and are constructed as follows:
w0 = b1 : b0
w1 = b3 : b2
w2 = b5 : b4
w3 = b7 : b6
etc..."

and,
" Bit variables are part of the lower value byte variables e.g.
b0 = bit7: bit6: bit5: bit4: bit3: bit2: bit1: bit0
b1 = bit15: bit14: bit13: bit12: bit11: bit10: bit9: bit8
etc..."

So if you change bit5, you will change b0 AND you will change w0.
Hope this helps.
 

gerrymcc

New Member
Thank you.

So, if I want to use bits as a "long term" marker within a program, then I have to avoid using b0 to b3 (if I should ever have to need to use all of them- which seems very unlikely), which implies that I should only start using bytes and words above b4 and w4.

This also implies that even b4 and w4, and above, should be used within very tight loops, so that their values are very local, and can be safely over written with new values later in the program, once their immediate use has been finished with.
 

hippy

Ex-Staff (retired)
@ gerrymcc : Yes, use bit0-bit31, then don't use b0 through b3 and w0 plus w1 and you will be okay.
 

MartinM57

Moderator
Yep, that sounds a good strategy if you want to use each bit as a long term global variable

Part of the art of designing a non-trivial PICAXE program is your memory usage/access strategy

So you could use:
- b0 to b31 for global long term bit storage
- b4 to b9 for global long term byte storage
- b10 and above for local short term byte storage
- POKE and PEEK commands to real memory addresses (see manual) for long term multi-byte storage
 

Dippy

Moderator
or, if you're not updating your 'marker' bit too often you could use EEPROM. Then your bit won't waste a byte. I like POKEing though.
 
Top