Author Topic: Question on use of #define versus EQU in PIC ASM language  (Read 7541 times)

0 Members and 1 Guest are viewing this topic.

Offline EENG

  • Contributor
  • Posts: 21
Question on use of #define versus EQU in PIC ASM language
« on: January 22, 2012, 06:19:44 am »
Hallo

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?





 
 

Online westfw

  • Super Contributor
  • ***
  • Posts: 3124
  • Country: us
Re: Question on use of #define versus EQU in PIC ASM language
« Reply #1 on: January 22, 2012, 06:54:23 am »
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: [Select]
#define D0_ON GPIO,4
   bsif D0_ON  ;; turn on just the specified bit
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.

 

Offline EENG

  • Contributor
  • Posts: 21
Re: Question on use of #define versus EQU in PIC ASM language
« Reply #2 on: January 22, 2012, 08:47:18 am »
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
 

Offline Graham1904

  • Contributor
  • Posts: 7
Re: Question on use of #define versus EQU in PIC ASM language
« Reply #3 on: January 22, 2012, 10:09:52 am »
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 :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf