What is the advantage of a MACRO over a CALL ROUTINE?

Status
Not open for further replies.

avinsinanan

New Member
Hi,
I was wondering the advantage of a MARCO over a CALL ROUTINE when using ASSEMBLY LANGUAGE for the PIC16F877.

Yours Respectfully
Avin Sinanan
 
avinsinanan said:
Hi,
I was wondering the advantage of a MARCO over a CALL ROUTINE when using ASSEMBLY LANGUAGE for the PIC16F877.

A Macro inserts the complete code everytime you use it, a subroutine is only inserted once and called when required.

Macro's tend to be small (a couple of lines?), so the call and return from a subroutine would take as much room as inserting the code everytime.

For an advantage, you don't have the call and return delays, which makes it run very slightly faster (not often a problem), and it also doesn't use any stack space.

Macro's are really a programmer function, and subroutines are a machine function.
 
A calls "calls" a subroutine. The subroutine is is stored in program memory only once and can be called many times from the main program. This saves memory.

A macro is expanded inline. This means if you write a 10 line macro, for example, and you use the macro in your main code the complete 10 lines will be copied to the place where you use it.

So if you use the macro 10 times you will actually use 100 lines of code.
 
The advantage is that there is no call and return overhead, so a speed advantage. The disadvantage is that it tends to use more code space. This is an enduring dilemma for the programmer, speed versus size.
 
Macro's tend to be small (a couple of lines?), so the call and return from a subroutine would take as much room as inserting the code everytime.
I think that's a bit confusing, Nigel, you make it sound like there is a return associated with each call...
 

There IS a return associated with each call! - it might be the same one?, but it still takes the same amount of time :lol:

But I agree it was a bit confusing, the return only takes one memory location, no matter how many times you call the routine.
 
A significant difference not mentioned is that macros can be parameterised, so that each macro expansion need not generate the same code sequence each time, whereas a function is coded once and that's it. Macro parameterisation is really one of the neat features, lending macro usage not just to inlined code, but to code sequences that are, for example, essentially the same in purpose, but that require to be coded differently in different situations. Examples might be setting PCL and PCLATH appropriately prior to a table lookup, based on the value of a label value.

A further possible advantage of macro's is to be frugal on stack usage. If call nesting in an app may reach the hardware stack depth limit, but memory is plentiful, pushing some routines into macros may be beneficial.

About return insns, whilst call/returns have to match, if the last instruction of a subroutine before the return is a call, the trick of tail call elimination (a GOTO to the subroutine rather than a CALL) can be used to save a return instruction and a couple of cycles.

Nick
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…