IF THEN commands

jims

Senior Member
These two commands are very helpful when programming a Picaxe chip with PE6.
If .... Then GOTO....
and
If....Then GOSUB....
Many times I find it would be helpful if there was a,
IF....Then MACRO.... (to perform a macro).
Is this something that could be added to a future update to the editor??
Would any of you other FORUM members have a use for this??
Thank you JimS
 

inglewoodpete

Senior Member
IF....Then MACRO.... (to perform a macro).
Is this something that could be added to a future update to the editor??
Would any of you other FORUM members have a use for this??
Thank you JimS
What you are asking for is just a change in syntax.

The way IF/THEN/GOTO/GOSUB are used is more a matter of programming style. For me, I rarely use single-line IF/THEN statements but prefer to use the following style:
Code:
[color=Blue]If [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
   GoTo [/color][color=Black]AAAAA[/color]
[color=Blue]EndIf
If [/color][color=Purple]b2 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
   GoSub [/color][color=Black]BBBBB[/color]
[color=Blue]EndIf
If [/color][color=Purple]B2 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
   [/color][color=Black]GoDoIt[/color][color=Blue]([/color][color=Black]CC, DD[/color][color=Blue])[/color][COLOR="#008000"]     'That's a macro[/COLOR][color=Blue]
EndIf[/color]
If the truth be told, I rarely use the GoTo statement as I dislike the poor programme structure that it promotes but that's another subject.
 
Last edited:

westaust55

Moderator
If I understand the Macro use correctly based on the PE6 briefing notes: http://www.picaxe.com/docs/pe6.pdf
and that they are inserted/substituted into the relevant locations in the code at download time, then that involves likely insertion of multiple lines of code into the IF…THEN command.
In this case you would need
Code:
IF . . . . THEN
     <Macro>
ENDIF
 

eggdweather

Senior Member
So PE6 already provides the requested capability, from the examples given the answer is a yes?

I suppose it's something like conditional compile statements like IF UK_VARIANT THEN do_this ELSE do_that and the Macros insert the code accordingly.
 

hippy

Ex-Staff (retired)
Part of the problem would be, what should the following produce -

Code:
#Macro MyMacro()
  Goto main
  SerTxd( "Hello World" )
#EndMacro

main:
  If b0 = 1 Then MyMacro
  Goto Main
Should that be -

Code:
main:
  If b0 = 1 Then Goto main
  SerTxd( "Hello World" )
  Goto Main
Or -

Code:
main:
  If b0 = 1 Then
    Goto main
    SerTxd( "Hello World" )
  End If
  Goto Main
Whichever it is someone is going to say that's wrong or have expected the opposite. Macro expansion forces a new line at its beginning so -

Code:
If <condition> Then <macro>
does work, but the macro expansion creates a multi-line IF statement which has to be terminated with ENDIF -

Code:
If <condition> Then <macro>
End If
Which would be better written -

Code:
If <condition> Then
  <macro>
End If
 

jims

Senior Member
Thanks to you all for your responses. I would just like to have the ability to use a
IF....Then MACRO....... statement to perform the Macro. I would assume (always dangerous to assume) that if the PE6 architects were "cut loose" to do this, they would figure out the expansion, etc and come up with a good solution. JimS
 

hippy

Ex-Staff (retired)
I would just like to have the ability to use a IF....Then MACRO....... statement to perform the Macro.
Actually you can with just a small amount of hoop-jumping ...

Code:
#Macro MAYBE
  SerTxd( "Yippee!" )
  SerTxd( CR, LF    )
  End If
#EndMacro

Do
  If b0 = 0 Then MAYBE
  Pause 1000
Loop
The "If b0=0 Then MAYBE" creates an implicit multi-line IF so has to be ended with END IF but there is no reason not to include that in the #MACRO itself.
 

stan74

Senior Member
How many subroutines can you use in for and do loops. eg
gosub sub1
sub1:
gosub sub2
return
sub2:
return
etc
Is it chip dependent?
 

hippy

Ex-Staff (retired)
These days, for M2 and X2 devices, you can have 255 GOSUB Commands in a program.

You can nest GOSUB calls to subroutines 8 deep. That becomes 7 if interrupts are enabled as an interrupt makes an invisible GOSUB call to the Interrupt routine.

Code:
Main: Gosub s1 : SerTxd( "Back home" ) : End
s1:   Gosub s2 : Return
s2:   Gosub s3 : Return
s3:   Gosub s4 : Return
s4:   Gosub s5 : Return
s5:   Gosub s6 : Return
s6:   Gosub s7 : Return
s7:   Gosub s8 : Return
s8:   SerTxd( "Made it!" ) : Return
 

Pongo

Senior Member
I confess I had never heard of a picaxe macro until this thread. What is the advantage over using a subroutine?
 

stan74

Senior Member
These days, for M2 and X2 devices, you can have 255 GOSUB Commands in a program.

You can nest GOSUB calls to subroutines 8 deep. That becomes 7 if interrupts are enabled as an interrupt makes an invisible GOSUB call to the Interrupt routine.

Code:
Main: Gosub s1 : SerTxd( "Back home" ) : End
s1:   Gosub s2 : Return
s2:   Gosub s3 : Return
s3:   Gosub s4 : Return
s4:   Gosub s5 : Return
s5:   Gosub s6 : Return
s6:   Gosub s7 : Return
s7:   Gosub s8 : Return
s8:   SerTxd( "Made it!" ) : Return
and not a goto anywhere ;)
 

stan74

Senior Member
I confess I had never heard of a picaxe macro until this thread. What is the advantage over using a subroutine?
Like what's the difference between a subroutine and a function? It returns a value,which you can do in a subroutine.Must be useful or they wouldn't exist.
 

hippy

Ex-Staff (retired)
I confess I had never heard of a picaxe macro until this thread. What is the advantage over using a subroutine?
The main use is that it can reduce having to do a lot of typing when there are lots of things in common in the code -

Code:
SerTxd( "The number 1 is one" , CR, LF )
SerTxd( "The number 2 is two" , CR, LF )
SerTxd( "The number 3 is three" , CR, LF )
Code:
#Macro Say( n, text )
  SerTxd( "The number ", #n , " is ", text, CR, LF )
#EndMacro

Say( 1 , "one" )
Say( 2 , "two" )
Say( 3 , "three" )
When used instead of a GOSUB it has advantages and disadvantages; it will generate a lot more code ( effectively what would be the GOSUB code is duplicated every time ) but it will execute slightly quicker than a GOSUB.

In most case where it's a choice between GOSUB or MACRO and there is no obvious reason to use a MACRO the best choice is probably a GOSUB.
 

Pongo

Senior Member
When used instead of a GOSUB it has advantages and disadvantages; it will generate a lot more code ( effectively what would be the GOSUB code is duplicated every time ) but it will execute slightly quicker than a GOSUB.

In most case where it's a choice between GOSUB or MACRO and there is no obvious reason to use a MACRO the best choice is probably a GOSUB.
Thanks Hippy for the clear explanation, I think I'll stick with GOSUB :)
 

BESQUEUT

Senior Member
Thanks Hippy for the clear explanation, I think I'll stick with GOSUB :)
I do not agree:
Just try to write code using GOSUB... for the Hippy #14 example, ...
If you have more than one parameter, macro is often a better choice, specially if you have enought code space, and even more if speed is of interest.


More : combined with PUSH and POP, you can CALL a SUB program with parameters...
In that case, I use MACRO AND GOSUB
Differents tools for differents uses...

But it's only my opinion...
 

stan74

Senior Member
I do not agree:
Just try to write code using GOSUB... for the Hippy #14 example, ...
If you have more than one parameter, macro is often a better choice, specially if you have enought code space, and even more if speed is of interest.


More : combined with PUSH and POP, you can CALL a SUB program with parameters...
In that case, I use MACRO AND GOSUB
Differents tools for differents uses...

But it's only my opinion...
Push,pop,call? We have access to the program counter?
 

hippy

Ex-Staff (retired)
The CALL statement does not exist in Picaxe environment
It definitely does; the following will compile, simulate and download whether CALL or GOSUB is used -

Code:
Do
  Call MySub
Loop

MySub:
  SerTxd("MySub")
  return
But, no, it is is not a CALL as some other Basic variant languages which are not PICAXE Basic may have it.

In terms of creating a subroutine calling mechanism which can pass parameters; that does have to be created using MACROS and GOSUB/CALL commands if one wants that. For example -

Code:
#Picaxe 20X2

#Macro PRINT(n)
  b27 = n
  Gosub SubPrintB27
#EndMacro

Do
  PRINT(99)
  PRINT(b0)
  b0 = b0 + 1
Loop

SubPrintB27:
  SerTxd( #b27, CR, LF )
  return
And using PUSH and POP ...

Code:
#Picaxe 20X2

#Macro PRINT(n)
  Push  n
  Gosub SubPrint
#EndMacro

Do
  PRINT(99)
  PRINT(b0)
  b0 = b0 + 1
Loop

SubPrint:
  Pop b27
  SerTxd( #b27, CR, LF )
  return
 
Top