Electronics > Microcontrollers

Question on use of #define versus EQU in PIC ASM language



I am busy getting familiar with the whole asm code structure for programming the pic PIC 12F675 in my PIC KIT 1 demo board. I am a first time programmer. Forgive me if I dont use the exact words to describe the SW function/variable names etc. as the terminology is all new. I will put a question mark in brackets (?) after a word  if I am not sure that I used the correct terminology

In a piece of code that I am analysing, I see they make use of this statement:

#define D0_ON  B'00010000'

From what I understand, the variable (?) D0_ON is assigned the 8 bit word whose binary value is 00010000. Then in the code they move this D0_ON value to the output port GPIO as shown below:

movlw D0_ON
movwf GPIO

In ASM coding, I understand that they also use the EQU function For example

D0_ON    EQU    10h ;*** i.e. the binary value 00010000 in hex ***

movlw D0_ON
movwf GPIO

My understanding is that the  ‘EQU’ instruction simply means something equals something else. Therefore, is there any difference in the above two examples or is there a distinct rule when one must use the #define in certain instances instead of the EQU?


Both of your examples will produce the same code.

But there's a technical difference:
"#define" defines a MACRO, which causes a text substitution when you use it.  (This is sort-of copied from the C language.  Most assemblers (including MPASM) have another type of macro common to assemblers that works differently.)
"equ" defines a label-style SYMBOL, which gets assigned a (numeric, I think) value, and you can use it where you would have used that value.

So since D0_ON is just a number, your examples behave the same.
But you could have said:

--- Code: ---#define D0_ON GPIO,4
   bsif D0_ON  ;; turn on just the specified bit

--- End code ---
while D0_ON equ GPIO,4 would have caused an error.

A major reason to use the #define form is that the same set of definitions can be used for C programs as well as assembler.

A major reason to use the "equ" form is that the symbol will show up in cross reference listings and debuggers.

Thanks for your kind assistance

So much clearer now....the journey of a 1000 miles starts with the first step....I am taking small learning steps and with your reply its opened up a new avenue of understanding

Also I think that EQU dates back to the early dark ages of Microchip assembler (circa 1980), then C programming was not an option. Use #define and that is at least ANSI C compatible :)


[0] Message Index

There was an error while thanking
Go to full version