Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How come my macro need to allocate space?

Status
Not open for further replies.

16F676

Member level 2
Member level 2
Joined
Aug 14, 2006
Messages
44
Helped
5
Reputation
10
Reaction score
2
Trophy points
1,288
Activity points
1,536
Hi friends,
I have a doubt on macros. I created a new asm file(delay.asm) as macro and included it's name in the main file(#include delay.asm). then I cut and pasted some portion of my asm(delay routine) to the new macro file. now I assambled the file with mplab and it returned with an error- overwriting address bla bla..
I had to allocate some blank address (ORG 0X0B00) in the bigining of the macro file to overcome the issue and it's successfully assembled. But I found some macro files from net has no addres alocation and it can be assambled with out error. How come my macro need to allocate space? is there any sloution to remove the address alocation (ORG 0Xxxxx) from the bigining of a macro file?
Thanks in advance.

NB: I am using assembly language.
 

MACROs

I'm assuming you're talking about using MPLAB.

It doesn't sound like you used a macro. It sounds like you have some code in delay.asm (like a subroutine). Then you included delay.asm at the beginning of the main file (assembler assumes ORG 0x0000) and followed with a later ORG 0x0000 command. Then the assembler will overwrite the code. If you intend to use a macro then macros in MPLAB follow the syntax:

name macro arg0, arg1, ...
code
endm

The code is not assembled unless the macro is used as:

code
...
name arg0, arg1, ...
...
code

Please read your help files for more details and examples.

If you want to separate your subroutines into another file, I usually write it this way in the main file:

'Constants and defines
'Macro definitions
'variable CBLOCK statements

org 0x0000
goto INTIALIZE
org 0x0004
'Interrupt routine
retfie

#include "delay.asm"
...

INITIALIZE
...
MAIN
...
goto $


-Jonathan
 

    16F676

    Points: 2
    Helpful Answer Positive Rating
Re: MACROs

Well, Thank you very much for the instant replay.
I am a bit confused with it now. I really understood that writing a portion of code for convinience is called as macro. but now I have two doubts.
You are right jonw. I need to make long code to portions and put it in diffrent files.
I also did the main file as you said.


Code:
main file:
Pre processor directives
#include XXXXXX.asm
#include XXXXXX.asm
CBlock
ENDC
ORG 0X0000
code  (form 0X000 to 0X0300)

END

and the assistant file

SUbroutine

WHen I assamble in this way, it returns an error of overwriting stuff
and when I add 0X0400 in the bigining of the assistand file, it successfully assembling. but I found some files on net with out using the ORG in the assistant files. can you pls explain me why?

and my another doubt is , actually what is a macro, and what is it's use?
thanks in advance
 

Re: MACROs

16F676,

I think your first question can be rephrased as can I use and included file without having an org command inside it. The answer is yes. Perhaps an example is in order. For instance, say you wanted routines to wait 4, 5, or 6 cycles (like a delay). But you wanted to use in many applications and include it when you want it. So, you write "delay.inc" (or "delay.asm") which reads as follows:


Code:
;Amount of delay includes the call in the main code
Delay6
nop
Delay5
nop
Delay4
return

To use that routine, you can write "main.asm" as follows:

Code:
;Compiler directives, definitions, includes, etc.

org 0x0000    ;Put compiled code at the reset location
goto MAIN
org 0x0004    ;Interrupt vector, put interrupt code here
retfie

;can't call Delay6, Delay5, Delay6 yet
#include "delay.inc"
;can call Delay6, Delay5, Delay6 here

MAIN
call Delay6   ;delay 6 cycles including this statement
call Delay5   ;delay 5 more cycles, 11 total
call Delay4   ;delay 4 more cycles, 15 total
goto $

;End of program, no more code or includes beyond this point
END

The assembler is pretty dumb, but powerful. It will assemble the code from top to bottom starting in main. When it gets to #include "delay.inc", it will assemble all the code in delay.inc (as if it is pasted into the main file at that location) before it assembles the rest. Technically, #include "delay.inc" can be anywhere between the end of the interrupt routine and the keyword END, but the assembler is not aware of the code until it has been assembled and won't allow you to call delay6 until after the #include "delay.inc" line.

Now, for macros. Macros are useful for substituting strings with code where it doesn't make sense to use a subroutine. Macros can take parameters much like subroutines, but the parameters have no type the assembler simply replaces the parameter name defined with the macro with the actual parameter name given with the call (this is exactly like the #define in c). So, to continue our example it doesn't make sense to call a DELAY1, DELAY2, or DELAY3 because a call and a return takes 4 cycles. But you don't want to remember a sequence of commands that will do the same thing, you want more readable code. So you define some macros in "delay.inc":

Code:
DELAY1 macro
nop
endm

DELAY2 macro
goto $ + 1   ;goto the next line, two cycles
endm

DELAY3 macro
DELAY2   
DELAY1
endm

and in "main.asm" you change it to:

Code:
;All that other stuff
;can't call Delay6, Delay5, Delay6, or use DELAY3, DELAY2, DELAY1 yet
#include "delay.inc"
;can call Delay6, Delay5, Delay6, and use DELAY3, DELAY2, DELAY1 here

MAIN
call Delay6   ;delay 6 cycles including this statement
call Delay5   ;delay 5 more cycles, 11 total
call Delay4   ;delay 4 more cycles, 15 total
DELAY3       ;delay 3 more cycles
DELAY2       ;delay 2 more cycles
DELAY1       ;delay 1 more cycle
goto $

;End of program, no more code or includes beyond this point
END

When you look at the assembled code DELAY3 will be replaced by

Code:
goto $ + 1
nop

Note that you can nest (or even recursively use) macros -- very handy.

-Jonathan
 

    16F676

    Points: 2
    Helpful Answer Positive Rating
Re: MACROs

Thanks Jonathan,
It's a valuable information for me and I hope it will be helpful for those want to study macros. Do you have any other examples of using macro ? for me, I need to study it's usage deeply. pls post if you have macro example files in assembly.
Thank you once again.
I have successfully assembled substitute .inc/asm files. the problem was the position of #include directive.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top