Menu

calculation fails.

Help
shiveni
2008-02-08
2014-01-01
  • shiveni

    shiveni - 2008-02-08

    The following  code fails to make PORTA=0xFF.....

    #chip 16F877A, 20
    DIM REM1 as word

    DIR PORTA OUT
    PORTA = 0x00

    STRLCD("T")

    tst:
    goto tst

    SUB STRLCD (DATA1 as string)

    LEN = DATA1(0)
    REM1= ((DATA1(1)-32)*5)+1

    if REM1 > 220 then
    PORTA = 0xFF
    end if

    End Sub

    Where as The following  code works fine and makes PORTA=0xFF.....

    #chip 16F877A, 20
    DIM REM1 as word

    DIR PORTA OUT
    PORTA = 0x00

    STRLCD("S")

    tst:
    goto tst

    SUB STRLCD (DATA1 as string)

    LEN = DATA1(0)
    REM1= ((DATA1(1)-32)*5)+1

    if REM1 > 220 then
    PORTA = 0xFF
    end if

    End Sub

    The calculation and comparision < 256 in (only lower byte) are working fine and >256 (Word) is having problem.

    I have checked the ASM file created. The higher byte of DATA1 is not considered. Is that the limitation?....

     
    • Hugh Considine

      Hugh Considine - 2008-02-10

      This is probably due to an overflow. Try replacing

      REM1= ((DATA1(1)-32)*5)+1

      with

      REM1= ([word](DATA1(1)-32)*5)+1

      Without the [word], GCBASIC sees that everything in the calculation is a byte variable or constant under 256, and so uses the 8 bit multiply routine. Unfortunately, the result overflows and causes an error. Adding [word] makes GCBASIC use the 16 bit multiply operation for the calculation in the brackets, which should solve the problem.

      Older versions of GCBASIC would use 16 bit calculations for anything where the result was being placed in a word variable, so your code would have worked with an older version. However, often using word operations for everything would cause inefficient code in places where byte operations worked perfectly. So, I altered GCBASIC to use byte operations for any calculations where [word] is not to the left of one of the values used in the calculation.

       
    • shiveni

      shiveni - 2008-02-13

      Hi Hugh,

      Its perfect. Working fine.

      I am almost going to finish Nokia 3110 LCD interface code.

      will post soon.

      Regards,
      Shivakumar.R
      dlmkerode.tripod.com

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.