News:

;) This forum is the property of Proton software developers

Main Menu

Proc versus meta macro

Started by John Drew, Jan 09, 2022, 10:45 PM

Previous topic - Next topic

John Drew

I don't understand when to use Meta Macro. Procedures make sense and don't result in duplicated code whereas Meta Macros do.
Shouldn't we always use procedures (or even gosubs)?
Are meta macros only for backward compatibility? Have I completely misunderstood some advantages of meta macros or even how they work?

I thought a meta macro worked by substituting the code into every spot the label appeared - extra code, extra space.

Happy to be shown I'm wrong. My wife tells me I often am.

Confused John :)

top204

#1
A good question John. However, there is no simple answer. :-)

Meta-macros are used in a lot of languages and are useful for giving a meaningful name to something. For example INTCON.7 = 1, will be easier to understand when written as:

$define Global_IntEnable() INTCON.7 = 1

So within the program's flow, the command becomes: Global_IntEnable()

In the C language they are named as #define.

They also have the useful feature of allowing parameters to be sent inline to a small block of code:

$define Global_Ints(pEnDis) INTCON.7 = pEnDis

With the above meta-macro, the program's code will use:

Global_Ints(1) ' Enable global interrupts

Global_Ints(0) ' Disable global interrupts

The same if using multiple commands within a meta-macro, because a name can be given to a set of commands, and they will be laid out in the code inline, instead of a Call/Return. Inline code always operates faster, but can take more flash memory space. However, the 8-bit devices do not have a large call stack, so too many Gosubs or Procedure calls too deep within subroutines can overflow the stack. I know, I know... Such a silly thing to have on the microcontrollers in the 21st century, but that's Microchip for you. :-) The newer 18F devices have a slighty larger stack, but it is still far too small for modern devices, and the, so called, "enhanced" 14-bit core devices have a tiny call stack. The PIC24 and dsPIC33 devices have a true stack in RAM, that can be as large as the RAM available and its size is set by the user, so why haven't they done this with the 18F devices, because they are both using the Harvard architecture?

The compiler does what it can with optimising the Call/Returns, but it can only do so much and too many calls too deeply in many procedures, can cause problems.

Procedures are very powerful for sending values to a, usefully named, subroutine and, sometimes, returning a value from it. It also means the procedure can have a large lump of code inside it that would not be very practical inline, so a Call/Return is ideal. Remember, the name 'procedure'  is just a fancy word for a subroutine that can accept parameters. :-) But a subroutine that will not be added to the devices's flash memory, unless it is called from within the program, so true libraries are able to be created.

Meta-macros are also useful for creating constant values or aliases:

$define cPI 3.1415926

or

$define MyPin PORTA.0

I started using meta-macros as constants and aliases in programs because the IDE highlights their names in the main program, so they are clearer to see. But John's IDE also highlight's Symbols. You may have noticed that I always start a constant's name with a lower case 'c', so it makes it even more clear in the program's flow that the name represents a constant value and not an alias or a block of inline code.

I could go on and on, but I hope you got the idea. i.e. A meta-macro to make small code segments clearer to use and understand, a procedure to make a program clearer and more structured with larger lumps of code, or if a return value is required. However, writing code is a form of artistic talent, so there is no "correct" way of writing it. If it works as it should after it is written, it is a good piece of code, but not always pleasing to the eye. LOL

Frizie

Les, in some examples I see you write:
$define FALSE = 0
$define TRUE  = 1


Is this a better option then
Symbol FALSE = 0
Symbol TRUE  = 1


(I think in this case it doesn't, at most that the IDE highlighter colors the words TRUE and FALSE).
Ohm sweet Ohm | www.picbasic.nl

top204

#3
In an .inc file, I use:

$ifndef False
    $define False 0
$endif
$ifndef True

    $define True 1
$endif


An equals character is not compatible with a $define, because they are created by the preprocessor program and not the compiler.

The above block, will only declare the True and False constants if they have not been pre-declared in another .inc file.

I always find them easier to scan in a program's flow when using Bit flags, instead of 0 or 1, and because True and False highlight as bold, which always makes it easier in my eyes when scanning large programs.

Frizie

(You're right, I placed the equals character by mistake in the $define example hereabove)
Coincidentally, in the time I asked the question you edited the answer above.
The answer about SYMBOL you've given above, so thanks Les.
Ohm sweet Ohm | www.picbasic.nl

John Drew

Thanks Les, I understand fully what you are saying. As you say "It all depends".
This thread will help others who were, like me, a bit confused about the difference and how to make best use of both.
Another step to becoming a better programmer.
Best wishes,
John

top204

#6
You are both very welcome.

I just wish I could find the time, and the means of making a living, to write a few books on the compilers and projects using them. "Positron Experimenters Notebook". Explaining the finer points of the compilers and some good projects showing what can be done with them. With a professional look and feel to the books, unlike 99% of the "Arduino" books out there that are copy and paste masterpieces, with, sometimes, utterly dreadful layouts and meaningless nonsense in them. Without having to understand the gibberish that is C, and the "true" gibberish that is C++, when written incorrectly, or convolutedly (if that is an actual word). :-)

If any publishers read this, I am listening! I have written a few books and articles in the past that were deemed "excellent". :-)

Frizie

Maybe another option is to post a good tutorial on Youtube.
In order to come out well, this must of course be tackled properly and I agree, that takes time and energy.
Explaining like this guy does (concerns bootloader in an Arduino) is pretty clearly done:



Something like this has to be done for Positron (but then with the highlighting of the many advantages of the Positron compiler), clearly visualized and of course good clear English spoken.
Unfortunately my english is bad and I do a lot with the help of a translator.
Ohm sweet Ohm | www.picbasic.nl

top204

#8
Thanks Frizie, but I do not consider myself very photogenic and I do not have much of an ego, so being on youtube is a no..no for me personally.

I also consider it as "here today, gone tomorrow", because in 10 years time, will it still be there? And a book is something very special to me, because I collect them and I have many of the older electronics books and computer books from the 1960s onwards. Before all of this "retro" and "vintage" computer/electronics bubble nonsense started, I used to find them at car boot sales for 10 new pence onwards, but now, they are sold by people who have no regard for what they are, only for what they are worth, so I can no longer collect what I love, because greed has taken over the country, in fact the world, and I cannot always afford them. :-( My favourites are the Babani books from my childhood. I used to go in the local bookshop and go through all of them before finding the one I could afford with my saved up pocket money, or dad would save up and buy one for both of us to read, so they are very special to me.

Anyway... Ramble over, but being on youtube and trying to look smart and ego tripping is just not me I'm afraid. Some people do it excellently, but I know I wouldn't. I also have a bit of a Geordie accent, so that always makes people sound thick. I know you will not fully understand what I meant by that statement, but the Brits among us will. LOL

Dompie

frizzie,
You know how to explain things well because a lot of Dutch people learned to program with Proton many years ago, yes I also did my very first PIC Proton steps based on your lessons on www.picbasic.nl.

Johan