Picaxe Compiler Questions

RNovember

Well-known member
Hello,
I have been doing some experimenting, and I noticed something interesting.
These two pieces of code do the exact same thing.

Code:
for b0 = 1 to 5
    toggle B.1
    wait 1
next b0
Code:
b0 = 0
do
    let b0 = b0 + 1
    toggle B.1
    wait 1
    if b0 = 5 then
        end
    endif
loop
But when I compile them, the first is 15 bites, and the second is 21 bites. I was just wondering why.
 

RNovember

Well-known member
I found a similar case.
Code:
b0 = 0
main:
    let b0 = b0 + 1
    
    if b0 > 3 then
        let b0 = 1
    endif

    select case b0
        case 1
            high B.1
        case 2
            low B.1
        case 3
            goto flash
    end select

    wait 1
    goto main

flash:
    for b1 = 1 to 8
        toggle B.1
        pause 200
    next b1
    goto main
Code:
b0 = 0
main:
    let b0 = b0 + 1
    if b0 > 3 then
        let b0 = 1
    endif
    if b0 = 1 then
        high B.1
    endif
    if b0 = 2 then
        low B.1
    endif
    if b0 = 3 then
        goto flash
    endif
    wait 1
    goto main

flash:
    for b1 = 1 to 8
        toggle B.1
        pause 200
    next b1
    goto main
The first one compiles to 64 bites, and the second to 52. Does anyone know why?
 

lbenson

Senior Member
Because it's not a compiler, it's an interpreter (and especially not an optimizing compiler). Basically, if you have more things which the interpreter considers tokens in one program, it will be longer than a program which does the exact same thing with fewer tokens.

The size (one byte or two) of a token may also depend on the length of the program and the placement of statements within the program. You may reorder the statements in a program without changing the function and find the interpreter has created either a smaller or larger tokenized program to download.
 

RNovember

Well-known member
So is the actual program that gets downloaded to the chip actually longer or shorter depending on the code length, if they do the same thing?
 

hippy

Technical Support
Staff member
For the FOR-NEXT case it's because those commands can be highly optimised in the object code so need less bytes than the long-hand version.

In fact most differences in code size between long-hand and actual come down to the long-hand not being exactly what the compiler code generates. That is the case for the second SELECT-CASE example.

The long-hand IF-IF-IF sequence doesn't include a 'GOTO done' within each IF-ENDIF which the SELECT-CASE generates which makes it longer.

In the example you have those aren't necessary but it saves executing addition IF's and avoids the problem of 'falling through' when you have something like this which prints a byte value in a fixed three digit field...
Code:
Select Case b0
  Case <10  : SerTxd ( "00" )
  Case <100 : SerTxd( "0" )
End Select
SerTxd( #b0 )
If you convert that as you did, you would end up with "0001" and not what was hoped for.

With the PICAXE having a lot more memory these days it's rare to really have to worry about code size. The best rule is to write the code so it's short and simple to understand, debug and modify.
 
Top