Shop OBEX P1 Docs P2 Docs Learn Events
assigning a value to a variable or constant? — Parallax Forums

assigning a value to a variable or constant?

wperkowperko Posts: 66
edited 2013-07-19 09:11 in BASIC Stamp
Hi,

ibyte = 768

Why does the result of;

DEBUG CR, "ibyte = ", DEC ibyte, CR

give ibyte = 108 ???

What is going wrong here?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-07-19 06:12
    Assuming that you've defined ibyte as a byte, it can only hold values from 0 to 255 (since it's a byte - 8-bits). All constants and intermediate values (like in a calculation) in PBasic are words (16-bits) and the extra bits get thrown away when stored in a byte (or nibble or bit variable). I don't know why it shows as 108. That doesn't make sense. 768 is the same as $300. When you store that in a byte, you throw away the 3 and get $00 which is zero. There must be something else going on in the code you're not showing. That's why it's generally misleading when you only show small code fragments.
  • wperkowperko Posts: 66
    edited 2013-07-19 07:06
    Hi,

    ibyte108.jpg


    ' ByteParse4.bs2
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' Initialze
    '
    ' Variables
    '
    ibyte VAR Byte ' byte to be converted
    hbyte VAR Byte ' bytes to represent Hundreds digit
    mbyte VAR Byte ' bytes to represent Tens digit
    lbyte VAR Byte ' bytes to represent Ones digit
    Remainder VAR Byte
    ' Initialize ibyte
    ibyte = 876 ' initial byte to be converted to 3 values
    DEBUG CR, CR, "ibyte = ", DEC ibyte, CR, CR

    hbyte = ibyte DIG 2
    mbyte = ibyte DIG 1
    lbyte = ibyte DIG 0

    DEBUG CR, "hbyte = ", DEC hbyte, CR
    DEBUG CR, "mbyte = ", DEC mbyte, CR
    DEBUG CR, "lbyte = ", DEC lbyte, CR, CR, CR

    END
    1024 x 675 - 68K
  • PublisonPublison Posts: 12,366
    edited 2013-07-19 07:25
    wperko wrote: »
    Hi,

    ibyte108.jpg


    ' ByteParse4.bs2
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' Initialze
    '
    ' Variables
    '
    ibyte VAR Byte ' byte to be converted
    hbyte VAR Byte ' bytes to represent Hundreds digit
    mbyte VAR Byte ' bytes to represent Tens digit
    lbyte VAR Byte ' bytes to represent Ones digit
    Remainder VAR Byte
    ' Initialize ibyte
    ibyte = 876 ' initial byte to be converted to 3 values
    DEBUG CR, CR, "ibyte = ", DEC ibyte, CR, CR

    hbyte = ibyte DIG 2
    mbyte = ibyte DIG 1
    lbyte = ibyte DIG 0

    DEBUG CR, "hbyte = ", DEC hbyte, CR
    DEBUG CR, "mbyte = ", DEC mbyte, CR
    DEBUG CR, "lbyte = ", DEC lbyte, CR, CR, CR

    END

    As Mike said, if you are declaring ibyte as a BYTE, it can only represent a number up to 255.

    Try:

    ibyte VAR Word

    and see the magic happen.
  • SapphireSapphire Posts: 496
    edited 2013-07-19 09:11
    You set ibyte = 876 in your code, not 768. And 876 in hex is $36C, which if stored in a byte is $6C or 108. In any case, if you want to store a 3-digit number up to 999, you will need to use a word variable. The result of the DIG operator, however, could be stored in a nibble variable, since the value will only be 0 to 9. Variable space is limited, so use only what you need for the range of values you are going to store.
Sign In or Register to comment.