Implementing a roll-over

John Chris

Senior Member
Hi,

Is there a way to use bitwise logical functions such as OR, AND, etc or other PICAXE supported functions to implement a roll-over for a given set of numbers without using an IF statement. For instance the set of integers 1-12 describes the months in a year. Can we devise a simple combination of functions to take as an input ,the present month, and provide as an output, the month before? e.g. 1 --> 12, and for all other elements in the set n --> n-1. The catch is that conditional statements are not allowed. I am looking for an alternative to the code shown below.

IF input = 1 THEN
output = 12
ELSE
output = input - 1
ENDIF

Thanks,

Chris
________
VAPORIZER
 
Last edited:

MartinM57

Moderator
I'm sure the logic gurus are sharpening their pencils as I type - but can I ask what the real issue is and why you are looking for an alternative...program size...or are you just having another go at, or advancing, what you looked for in http://www.picaxeforum.co.uk/showthread.php?t=13834 ?

EDIT: I guess what I really mean is that are IF statements allowed if they use less bytes than what you show?..or are they strictly forbidden?
 
Last edited:

John Chris

Senior Member
Martin, my reason for asking is primarliy the prior. I have been working on a program (for the 28X2) that has resulted in unexplained results on more than one occaision when I would make little changes / add new features. I found that I could often revert the program back to a well-behaved state if I simply removed an IF THEN ENDIF statement. I can only assume that I had reached some limit for nested IF's or GOSUB's etc.

With respect to the later point, the subroutine 'SubtractDateTimes' that is presented in the post that you mention, is complex, consuming a least a couple GOSUB levels, IF statements, DO WHILE, FOR NEXT, etc. So yes, If I can find a way to simplify the subroutine, I will be in a better spot.

I understand, as you say, that alternative solutions may present a cost in terms of memory. Some loss in memory is tolerable provided there is a gain associated with decreased complexity.

This raises an interesting point though. When limits associated with GOSUB, IF, FOR, etc. have been reached, is there any warning sign / error messages ? Perhaps my problems are associated with other things and I have not reached this limits. One could simply answer this question by creating a simple program to intentionally violate some of these limits and check for error messages. Many on this forum, however, likely know the answer.

I very much appreciate your response,

Chris
________
Chevrolet corvair specifications
 
Last edited:

hippy

Ex-Staff (retired)
When limits associated with GOSUB, IF, FOR, etc. have been reached, is there any warning sign / error messages ?
When the maximum number of GOSUB and so on is exceeded there will be compiler errors given but no errors for when GOSUB call depth is exceeded at run time. It's just not practicably possible to accurately assess that at compile time, though simulation can reveal such issues.

Other than user-created program flow errors with GOSUB's and Interrupts, if it gets through the compiler without error, code should execute correctly.

A simple IF statement should not have an impact on code operation so if one causes a problem and removing it / replacing with alternative code solves a problem it would be interesting to see that code.
 

westaust55

Moderator
Untested (PE not installed here) but these should (?) work:

To go back one month:
use hippys code


month = month – 1 MAX 12 ; – this is just variant on hippy's this attempt does not work :eek:

To go forward 1 month
month = month + 1 // 13 MIN 1​
 
Last edited:

hippy

Ex-Staff (retired)
@ westaust55 : Won't work because 'month' needs to be 1 .. 12, so 1 - 1 Max 12 = 0
 

Jeremy Leach

Senior Member
Here's just a little interesting thought ...

Have 11 bytes in EEPROM starting at address 1:
Code:
Address    Data
1             2
2             3
3             4
4             5
5             6
6             7
7             8
8             9
9             10
10           11
11           1
 
'Initialise
Symbol Month = b0
Month = 1
Then just call Read Month,Month to get the next value.
 
Top