Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MACROs

Status
Not open for further replies.

eng1

New Member
Programming langugae: assembly
Processor: PIC16


I wrote a macro at the beginning of a program, so that I can use it in the 'main' routine. Does anyone know how (where) the assembler allocates space in the program memory for a macro?

Basically the structure of my program is as follows

Code:
   ; Variable definitions here
   

   Foo macro x,y
      ;....
   endm

   org  0x000
   goto  main

   org  0x004
   ; ISR

main:
   Foo A,B
   ; remaining code here

   end
Thanks
 
Last edited:
eng1 said:
Programming langugae: assembly
Processor: PIC16


I wrote a macro at the beginning of a program, so that I can use it in the 'main' routine. Does anyone know how (where) the assembler allocates space in the program memory for a macro?


Thanks


If that language is anything like C, then the macro itself never gets allocated, per se. But space for its code gets allocated once every time it's called.

In C, a preprocessor goes over the code before handing it to the compiler, and the preprocessor is what handles the macros. Once a macro has been defined, the preprocessor replaces all instances where the macro is called with the code from the macro definition. The resulting text (with other changes too) is passed to the compiler.

I hope that made sense.


Torben
 
Same in assembler, the macro code is inserted everywhere it's used in the program - so depending what it does, how long it is, and if it's used a lot, it may make more sense to use a subroutine instead (which is only stored once).
 
Short answer, cumulatively at each location of instance.

Assuming the use of an IDE like MPLAB, you may also view the exploded view of the code from the Program memory window. Here you see how that macro is placed throughout the code and every opcode. Its very easy to accidentally do a computed jump into a macro instead of over it because your eyes see a single line or you miscalculate the actual number of instructions residing inside the macro. So I either replace long ones with a subroutine or make a dedicated macro include file having the length of each macro burned into my long term memory.

My favorites and only as to not taint the original language:
LOADF_F
MOVF_F
BANK1
BANK2
BANK3
BANK4
All 2 cycles long. :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top