Confused by MIN

cpedw

Senior Member
I want a byte variable to follow the sequence 1,2,4,8,16,32,64,128,1,2 ... so I wrote the code
Code:
symbol temp = b2   

temp = 1
DO
'Operations   
    temp = temp*2 min 1
LOOP
but that took temp from 128 to 0 not 1.
Replacing
temp = temp*2 min 1
by
temp = temp*2
temp = temp min 1
gets it working OK.

Does the first version fail me because it's working with a word when it applies the MIN operation?

Derek
 

roho

Member
Does the first version fail me because it's working with a word when it applies the MIN operation?
I don't know for certain and the user manual isn't specific, but yes, this does appear to be the case. At least, if you re-define the temp register as a word variable then your original code works as intended.
 

matchbox

Senior Member
Try putting the MIN before the math. In some application it works after the math, and in others it needs to be before.
Code:
symbol temp = b2  

temp = 1
DO
'Operations  
    temp = temp min 1 * 2
LOOP
 

cpedw

Senior Member
I don't know for certain and the user manual isn't specific, but yes, this does appear to be the case. At least, if you re-define the temp register as a word variable then your original code works as intended.
I hadn't thought of using a Word but that works. I need the variables though so I'll stick with my scheme.

Putting Min before the Maths goes ... 64,128,0,2,4 ... which is near but not what I want.

@Aries "temp min 1 (= smaller of temp and 1)"
Surely that's the Larger of temp and 1?

Derek
 

hippy

Technical Support
Staff member
temp = temp * 2 And $FF Min 1

Should do the job. That's basically forcing the truncation on the 16-bit calculated value before that value is written to a byte variable and gets truncated.
 

Aries

New Member
@Aries "temp min 1 (= smaller of temp and 1)"
Surely that's the Larger of temp and 1?
Sorry - slip of the mind. My point was that with left-to-right arithmetic, the MIN is done first, followed by the multiplication, so you will never get 1 as a result
 
Top