How does the picaxe store numbers?

Brietech

Senior Member
I was wondering, how does the picaxe store numbers internally? For the "pause" command, for instance:

Pause 5 = adds 1 byte to code
Pause 16 = adds 2 bytes to code
pause 255 = adds 2 bytes of code
pause 256 = adds 3 bytes of code
pause 65000 = adds 3 bytes to code

I was thinking it stores everything as 4-bit nybbles, but that doesn't seem to add up. hmm...any thoughts?
 

inglewoodpete

Senior Member
Lord PICAXE moves in strange ways. The code inside a PICAXE interprets the tokenised code that the Programming Editor creates and downloads. The code consists of quantities of bits per command and 'flows' from byte to byte.

Try compiling a single command and observe the number of bytes used. Then repeat the same command and recompile etc etc. You will see that not every command needs the same number of new bytes.
 

Brietech

Senior Member
that seems fairly space inefficient and thus unlikely. I would imagine it's just some sort of non-standard encoding scheme (like 5-6 bits per command, maybe? there aren't that many different ones!), where commands are just part of a byte, so nothing is properly aligned along byte-boundaries like we would expect.
 

hippy

Technical Support
Staff member
Correct, nothing is byte aligned ( apart from EEPROM ), everything is bit aligned and different types of token have different widths, ie 5-bits per command - count them for an 08, there are 32 :)

Number tokens are craftily encoded to use the minimum size for most commonly used numbers, with a 2-bit overhead to say how many bits the number uses ...<code><pre><font size=2 face='Courier'> Number Size Total Token Size

0..1 1 bit + 2 3
2..15 4 bits + 2 6
16..255 8 bits + 2 10
256..65535 16 bits + 2 18 </font></pre></code> Hence, using the smallest numbers gives more compact code.
 

hippy

Technical Support
Staff member
Note that ( on a 28X ) the single line programs &quot;let b0=0&quot; and &quot;Let b0=15&quot; both claim to be &quot;5 byte&quot; programs.

That's because going from 3 bits to 6 bits hasn't pushed the end of the LET command over a byte boundary.<code><pre><font size=2 face='Courier'> Let Var=0 Let Var=15

??????LL ??????LL
LLLVVVVV LLLVVVVV
NNn----- NNnnnn-- </font></pre></code> Add an END statement to those programs and the first remains at &quot;5 bytes&quot; but the second goes up to &quot;6 bytes&quot;.<code><pre><font size=2 face='Courier'> Let Var=0 Let Var=15
End End

??????LL ??????LL
LLLVVVVV LLLVVVVV
NNnEEEEE NNnnnnEE
EEE----- </font></pre></code> As can be seen, it all depends where the LET command starts as to what does and does not change reported program size.

Edited by - hippy on 06/06/2007 15:03:16
 
Top