Shop OBEX P1 Docs P2 Docs Learn Events
Generated Code — Parallax Forums

Generated Code

Ken StraussKen Strauss Posts: 10
edited 2009-03-09 18:00 in General Discussion
I've noticed that runtime code is sometimes generated even when everything could be evaluated at compile time. For example:

finishAt VAR Byte
minValue CON 5
finishAt = minValue + 1

Generates runtime code to evaluate (minValue +1). I can code this as:

finishAt VAR Byte
minValue CON 5
finValue CON minValue +1
finishAt = finValue

and avoid the runtime addition but this makes the source code longer and often obscures the intent. Are there any plans to improve SX/B by evaluating constant expressions at compile time? If not, are there other "gotchas" that I can avoid to reduce my code size?

Comments

  • BeanBean Posts: 8,129
    edited 2009-03-09 17:08
    Ken,
    · I'm not sure what you are looking at, but the compiler does genererate the same code as a direct assignment of a constant.
    · See the hex code that is generated.

        86  0018  0C06        MOV finishAt,#minValue + 1     ;  finishAt = minValue + 1
            0019  002D
        87                  
        88  001A  0C06        MOV finishAt,#6                ;  finishAt = 6
            001B  002D
    


    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...

    ·
  • Ken StraussKen Strauss Posts: 10
    edited 2009-03-09 18:00
    It works when using byte variables but not with word values. The following:

    DEVICE SX28, OSCXT2, TURBO, STACKX, OPTIONX
    FREQ 8_000_000

    minValue CON 5
    minValue4 CON minValue -4
    finishAt VAR Word


    Program Start

    Start:
    finishAt = minValue -4

    finishAt = minValue4


    Compiles to:
    MOV finishAt_LSB,#minValue & 255 ;finishAt = minValue -4
    MOV finishAt_MSB,#minValue >> 8
    SUB finishAt_LSB,#4 & 255
    SUBB finishAt_MSB,/C
    SUB finishAt_MSB,#4 >> 8


    MOV finishAt_LSB,#minValue4 & 255 ;finishAt = minValue4
    MOV finishAt_MSB,#minValue4 >> 8
Sign In or Register to comment.