Out of variables

RobertN

Member
I've run out of Word variables and have only one Byte variable left on a 08M.
To stay within the 6 word variables I'd like to do something like below, simulate gives "Syntax Error".

if w6 = w4 + w5 then let w6 = 0 endif

Whats wrong? Suggestions appreciated.
 

inglewoodpete

Senior Member
You can't do calculations within an 'If' statement. You'll have to do something like
Code:
w7 = w4 + w5
If w6 = w7 Then
   w6=0
EndIf
Short of variables? You have two options:
* Re-use one that does not contain a value that needs to be preverved at that point in the program
* Poke a register into RAM and then Peek it back after you have completed the reuse of the variable:

Code:
Symbol ram_locn = $50    'or some other free RAM location

Poke ram_locn, WORD w7   'preserve w7 value
w7 = w4 + w5
If w6 = w7 Then
   w6=0
EndIf
Peek ram_locn, WORD w7   'restore w7 value
 

westaust55

Moderator
Further to the information provided by inglewoodpete, have a look at PICAXE manual 2 (currently V6.9) at page 13 under the heading Variables – storage to understand the available non variable memory locations available to you for temporary storage of values.

You might also wish to look at the PICAXE variable map that I created some time ago.
http://www.picaxeforum.co.uk/showthread.php?p=79188 (Post 14)
 
Last edited:

Jeremy Leach

Senior Member
As a desperate measure you can probably save two bytes on InglewoodPete's suggestion by using:
Code:
w6 = w4 + w5 - w6 Max 1 * w6
It's a bit confusing but explaining it in stages (remember, maths expressions are evaluated left to right):

w4 + w5 - w6 This will be 0 if w6 = w4 + w5, and non-zero otherwise.
The Max 1 limits the non-zero result to 1, So if w6 = w4 + w5 this gives 0 otherwise gives 1.
The * w6 makes it give the inteneded overall result: if w6 = w4 + w5 Then w6 = 0 else w6 = w6.
 
Last edited:

RobertN

Member
Thanks for the ideas, will have to give them a try.
A separate one bit variables, a 1 or 0, are being stored in b1, b2, and b3. To make another Word variable available, is it possible to store the 3 separate bit variables in one byte variable?
 

MPep

Senior Member
It is possible to store bit variables into a byte variable. You will need to bit-mask them in. There have been posts about bit-masking.
 
Last edited:

MPep

Senior Member
Good to know. Always good to read about people having a problem then answering it themselves.:D
 
Top