|
|
|
|
|
||
|
Macro declaration: .macro name [expression] ; macro code .endmwhere '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 .endmCalling 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 .endmstart: 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, 1not to: incf 10, 1
.left
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 .endmThis 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 + 0x22If 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 .endmAgain, 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() .endmwill crash! But: .macro Rec 1 .if .isndef _Rec _Rec = #0 .else _Rec = _Rec - 1 .endif .if _Rec != 0 Rec(a) .endif .endmwill countdown recursively 'n' times: start: Rec(10) |
||
|
|