----- Original Message ----- From: Ray Gallas To: Sent: Friday, February 15, 2002 8:59 AM Subject: [PIC]: Basic question about macros > This is a beginner question, so I apologise if it seems dumb -- > I've been studying various assembly programs and I've noticed that many > of them invoke an assembler directive, "MACRO". > > Now from what I can see (and perhaps I'm missing something here), they > seem to fill essentially the same role as a subroutine. That is, they're > a predefined set of commands, which at some point in your program, you > would have to, well, *define*. It seems to me that doesn't really save > you much work over just calling a subroutine, which you also write out. > > Obviously, MACROs must have some good reason for existing, otherwise, > why would anyone use them? So my question is, what's the difference > between a MACRO, and just using a subroutine? Why would one choose one > over the other in a given situation? > > Thanks for any clues! You are partly right, but... If you have a small number of instances of use of the subroutine and the body of the subroutine is short then you might find that the setup / call / return / retsult overhead swamps any extra code produced by a macro alternative. Also since a macro produces code with 'inlined' arguments, this produces faster executing code than using a more general subroutine. An example of these two properties comined would be: add16 .macro arg1,arg2 movf arg1+0,w addwf arg2+0 movf arg1+1,w btfsc STATUS,C addlw 1 addwf arg2+1 .endm A subroutine version would look like: add16x movf acc1+0,w addwf acc2+0 movf acc1+1,w btfsc STATUS,C addlw 1 addwf acc2+1 return ;; return overhead to use them you would need to do ;; for the subroutine version movf fred+0,w ;; call setup overhead movwf acc1+0 movf fred+1,w movwf acc1+1 movf bert+0,w ;; call setup overhead (cont.) movwf acc2+0 movf bert+1,w movwf acc2+1 call add16x ;; call overhead movf acc2+0,w ;; result overhead movwf bert+0 movf acc2+1,w movwf bert+1 ;; for the macro version add16 fred,bert ;; which would produce this movf fred+0,w addwf bert+0 movf fred+1,w btfsc STATUS,C addlw 1 addwf bert+1 Another advantage is that sometimes you need several copies of the same subroutine that uses different working storage. In this case you would write your subroutine once as a macro and then just invoke it a few times with different working storage. Consider the above add16x example. This subroutine could not be used in an interrupt routine if it were also being used by the main line code, without taking great care not to disturb the working storage acc1 and acc2. Yet another advantage is that macros (depending on the assembler - xcasm definiatly suports this) can also generate their own local workspace, making them suitable for use as inline libraries reducing module linkage overheads. These are overheads that occure when one function calls another in a seperately compiled or assembled module. Regards Sergio -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu