Macro Code Examples

MPASM Assembler

Macro Code Examples

The following are examples of macros:

Eight-by-Eight Multiply

subtitle "macro definitions"

page

;

; multiply - is an eight by eight multiply macro,

; executing in program memory, optimized for speed,

; straight line code.

;

; This macro has five parameters as defined here:

; arg1 - first eight bit literal to be multiplied

; arg2 - second eight bit literal to be multiplied

; dest_hi - memory location for high byte of result

; dest_lo - memory location for low byte of result

; temp - memory location for temporary storage

;

; During the execution of this macro, the w register is

; destroyed.

;

; The result of multiply is a 16 bit value stored in the

; two eight bit registers (dest_hi, dest_lo)

;

; This macro is written for the PIC17C42.

;

;

multiply macro arg1, arg2, dest_hi, dest_lo, temp

;

local i = 0 ; Establish a local index

; variable and initialize it.

;

movlw arg1 ; Setup the eight bit

movwf temp ; literal multiplier in the

; memory location temp.

;

movlw arg2 ; Setup the eight bit

; literal multiplicand in the

; w register.

;

clrf dest_hi, F ; Clear both the high and

clrf dest_lo, F ; the low destination

; registers.

;

bcf ALUSTA, C ; Clear the carry bit.

;

while i < 8 ; Use the loop to check all

; eight bits of the

; multiplier (temp).

;

btfsc temp, i ; Test the current

addwf dest_hi, F ; multiplier bit, if temp,I

; then add the multiplicand

; to the high register.

;

rrcf dest_hi, F ; For each pass in the

rrcf dest_lo, F ; loop, right shift each

; destination register using

; the carry bit.

;

i += 1 ; Place this increment in

; column 1 to avoid

; Warning [207].

endw ;

endm ;

The macro declares all of the required arguments. In this case, there are five. The LOCAL directive then establishes a local variable "i" that will be used as an index counter. It is initialized to zero. A number of assembler instructions are then included. When the macro is executed, these instructions will be written in line with the rest of the assembler source code. The macro writes the multiplication code using an algorithm that adds for each bit set in the eight bits of the multiplier and uses right shifts. The WHILE directive is used for this function, continuing the loop until "I" is greater than or equal to eight.

Constant Compare

As another example, if the following macro were written:

include "16cxx.reg"

;

; compare file to constant and jump if file

; >= constant.

;

cfl_jge macro file, con, jump_to

movlw con & 0xff

subwf file, w

btfsc status, carry

goto jump_to

endm

and invoked by:

cfl_jge switch_val, max_switch, switch_on

it would produce:

movlw max_switch & 0xff

subwf switch_val, w

btfsc status, carry

goto switch_on


Microchip Technology Inc.
Microchip's Web Site
Voice: (480) 792-7200
Fax: (480) 899-9210
Microchip's E-mail Address
PreviousNext