Picaxe 20M and gosub

cachomachine

Senior Member
Hi am getting the following error message

Gosub test ;#16
^
Syntax error on line 17 at/before position 11

Error: Only 16 gosub commands allowed!


when I run a syntax check on the following program (Editor 6.0.9.1)

#Picaxe 20M
main: Gosub test '#1
Gosub test '#2
Gosub test '#3
Gosub test '#4
Gosub test '#5
Gosub test '#6
Gosub test '#7
Gosub test '#8
Gosub test '#9
Gosub test '#10
Gosub test '#11
Gosub test '#12
Gosub test '#13
Gosub test '#14
Gosub test '#15
Gosub test '#16
Goto main

test:Return

What is the problem?
 

Technical

Technical Support
Staff member
As the error msg states for the very old obsolete 20M part:

Error: Only 16 gosub commands allowed!

One of these is actually reserved for the interrupt sub procedure, so in reality only 15 different 'gosub commands' can be typed.
For the later part (20M2) the program can contain up to 255 gosubs instead.
 

Buzby

Senior Member
Well, you learn something every day !.

I knew of the '16 gosub' limit, but I interpreted that as meaning no more than 16 distinct subroutines, not a limit of 16 calls.

Which prompts me to ask why ?.

I could understand a design limit of defining 16 unique subroutines, ( maybe the call targets are compiled into 4 bit vectored addresses ), but what limits the number of calls that can be in a program ?

Maybe the return addresses pushed onto the stack are vectored as well, is that what it is ?.

Cheers,

Buzby
 

hippy

Technical Support
Staff member
I could understand a design limit of defining 16 unique subroutines, ( maybe the call targets are compiled into 4 bit vectored addresses ), but what limits the number of calls that can be in a program ?

Maybe the return addresses pushed onto the stack are vectored as well, is that what it is ?
Exactly that. Because the PICAXE uses variable bit size tokens it's not really 256 bytes of memory, it's 2048 bits. If the full return address were pushed that would need a 11 bit x 4 deep stack, which is 44 bits, 6 bytes, and probably 8 to make firmware efficient.

By pushing the 'vector' / number of the GOSUB, that only requires 4 bits x 4 deep, 16 bits, 2 bytes, which is quite a saving in RAM when it comes to very constrained devices. Those 4 bits are then translated into jumps to where the return would go. That uses more PICAXE code space for the jump table but saves on RAM use.
 

westaust55

Moderator
Also recall that at least with earlier version of the PE (early PE5 even?) that in options there was a setting for later PICAXE parts to select whether 16 or 255 GOSUB limit applied which could save some program space if the lower number was selected.
 
Top