Shop OBEX P1 Docs P2 Docs Learn Events
Modulus operator or __REMAINDER Aliases in SX/B v1.51.01 — Parallax Forums

Modulus operator or __REMAINDER Aliases in SX/B v1.51.01

Mike CookMike Cook Posts: 829
edited 2006-09-20 02:15 in General Discussion
I'm having a little trouble with the Modulus operator or more specifically the __REMAINDER Aliases in SX/B v1.51.01
From the HELP file under Operators section: Modulus it states:
Note: The remainder of a division (modulus) is available immediately after the division or modulus operation. This code: 
 
dig10 = value / 10
dig01 = __REMAINDER
 
... uses half the instruction space of: 
 
dig10 = value / 10
dig01 = value // 10
 
The only requirement for using __REMAINDER (or __WREMAINDER for word values) is that the assignment to another variable must be the first instruction following the division or modulus operation. "


In my code I'm doing the following:
tmp1 = Year / 4      ' Step 1a. - get a quarter of the year
tmp2 = __REMAINDER   ' Setp 1b. - save the remainder


If Year = 00 then I would expect:

tmp1 = 0
tmp2 = 0

But I actually get:

tmp1 = 0
tmp2 = 255

However If I change the code to:
tmp1 = Year / 4    ' Step 1a. - get a quarter of the year
tmp2 = Year // 4   ' Setp 1b. - save the remainder


I get what I expect:

tmp1 = 0
tmp2 = 0

Could this be a bug with the __REMAINDER? Or am I not understanding this correctly?

Thanks

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike

Comments

  • BeanBean Posts: 8,129
    edited 2006-09-19 23:41
    Mike,
    Is YEAR a byte or a WORD ?
    The problem is most likely that SX/B will optimize the "/ 4" into shifts instead of doing long division.
    But is should still set the remainder properly like you want.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.
    ·
  • Mike CookMike Cook Posts: 829
    edited 2006-09-19 23:43
    Year is defined as a byte

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
  • BeanBean Posts: 8,129
    edited 2006-09-19 23:49
    Mike,
    · It should work. Do you have any other code between these two lines ?
    tmp1 = Year / 4      ' Step 1a. - get a quarter of the year
    tmp2 = __REMAINDER   ' Setp 1b. - save the remainder
    
    
    

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.
    ·
  • Mike CookMike Cook Posts: 829
    edited 2006-09-19 23:53
    No other code between the two statements.

    Year is a byte but it is defined this way:
    Time            VAR     BYTE(7)
    Hour            VAR     Time(0)
    Minute          VAR     Time(1)
    Second          VAR     Time(2)
    Day             VAR     Time(3)
    Month           VAR     Time(4)
    Year            VAR     Time(5)
    Valid           VAR     Time(6)
    

    Should that make a difference?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
  • BeanBean Posts: 8,129
    edited 2006-09-19 23:57
    Mike,
    It shouldn't matter. Can you post the code, or send it to me in a PM ?

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.
    ·
  • Mike CookMike Cook Posts: 829
    edited 2006-09-19 23:59
    Code is attached see SUB: GET_DAY

    Thanks!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
  • BeanBean Posts: 8,129
    edited 2006-09-20 00:22
    Mike,
    · I just put at the start label:

    Start:
      Year = 0
      BREAK
      WATCH tmp1
      WATCH tmp2
      GET_DAY
    


    And when I single step through it, it sets tmp1 to zero and tmp2 to zero (using __REMAINDER)·???

    Can you try single stepping it and see what you get ?

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.
    ·
  • Mike CookMike Cook Posts: 829
    edited 2006-09-20 00:52

    OK, I've got a 'SXKey' but right now I'm using the 'BLITZ', I have not taken the time to fully learn the full capabilities of the 'SXKey'

    What I did do is define

    Year = 0

    in the START: label and then uncomment:

    tmp2 = __REMAINDER

    and commented out:

    tmp2 = Year // 4

    in the GET_DAY SUB

    This worked. Now to back out the Year = 0. Commented this line out, saved, recompiled and it STILL works, Now I can't duplicate the problem I was having in my first post.

    Boy I need a BEER, Oh well problem solved for now.

    Thanks for the quick replies, Ken needs to give you a raise or a case for Propeller Chips!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
  • Mike CookMike Cook Posts: 829
    edited 2006-09-20 01:31
    Ok, further investigation.... It appears that I got bit by the:

    PROGRAM Start

    PROGRAM Start NOSTARTUP

    feature. (I was using PROGRAM Start NOSTARTUP to save a few bytes of code space, thought I was handling everything properly in my code.)

    If I don't set the Year or Leap variable to ZERO AND compile with PROGRAM Start, power cycle the Board for 30 seconds, it works as expected (no errors)

    However if I set:

    PROGRAM Start NOSTARTUP

    DON'T define Year or Leap, compile, power cycle the board for 30 seconds, then the error shows up.

    I might have expected he first run through of the SUB's to produce bogus results, but would have assumed that the next loop through would fix it's self.

    Moral of the story: If you don't know assembly (or what your doing) always use: PROGRAM Start

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
  • BeanBean Posts: 8,129
    edited 2006-09-20 02:11
    Ahhh,
    The old un-initialized variable problem. I spend days (literally) fighting the read-modify-write problem on the color video board. So you got off easy.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.
    ·
  • Mike CookMike Cook Posts: 829
    edited 2006-09-20 02:15
    Well this trip through "Alice and Wonderland" only took 3 hours! smilewinkgrin.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
Sign In or Register to comment.