Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

 
5.6 .macro .endm .left .right .argc

Macro declaration:
   .macro name [expression]

    ; macro code

   .endm
where 'name' is name of macro and expression is optional number of arguments that macro can use. Most simple macro does not have arguments:
  .macro IncW

       addlw 1

  .endm
Calling macro syntax is same like calling functions from C:
  start:

     ; some code

     IncW() ; call IncW macro
 

 

To specify arguments, number of arguments goes after macro name. To access specific argument we must write #'num' where 'num' is one digit number (from 0 to 9):

  .macro IncF 1

      incf #0, 1 ; #0 is first argument

  .endm
  start:
     movlw 0x10
     movwf 0x20
     IncF(0x20)

Macro can have maximum of 10 arguments (named #0 .. #9). When we call macro, any parameters are expressions, so we can write:

   a = 10

   IncF(123-a)  ; call IncF(113)
If we want to pass argument without calculating expression we must enclose it with < >:
   a = 10

   IncF(<a>)
is expanded to:
   incf a, 1
not to:
   incf 10, 1


If we want to declare macro without knowing number of arguments we must put 0 for argument number. To use this params we must use some directives:

  .left
  .right
  .argc

First parameter is on position #0, after .left all params are rotated to left, so after .left,  #0 have value of second parameter. .right do same, just in other direction.

To find how many arguments is passed to macro .argc contain number of arguments:

  .macro Sumation 0

     movf #0, 0

     .rep (.argc - 1)

        .left

         addwf #0, 0

     .endrep

  .endm
This macro just sum all params in working register:
   Sumation(0x20, 0x21, 0x22)
is expanded to:
  movf  0x20, 0

  addwf 0x21, 0

  addwf 0x22, 0  ; W = 0x20 + 0x21 + 0x22
If we call macro Sumation without params it will be expanded (too much times) because (.argc-1) will be negative, to correct this:
  .macro Sumation 0

     .if .argc > 0

      movf #0, 0

      .rep .argc - 1

        .left

        addwf #0, 0

      .endrep

      .endif

        .error Provide one
or more params

      .else

  .endm
Again, 10 arguments is maximum!

Recurrence macro call is legal and mas will not issue any message about this. It is YOURS responsibility to check infinity macro recurrence:

  .macro Rec

     Rec()

  .endm
will crash! But:
  .macro Rec 1

    .if .isndef _Rec

       _Rec = #0

    .else

       _Rec = _Rec - 1

    .endif

    .if _Rec != 0

       Rec(a)

    .endif

  .endm
will countdown recursively 'n' times:
  start:

     Rec(10)
[PREV][INDEX][NEXT]