Dunfield Development Services Inc.
Customer Support -- Application Note #0010
MICRO-C
Embedding spaces in MCP macros (inline assembly)

Applies to: [Micro-C Compiler
]
Last updated: Sunday May 04, 2003
.
Application Note Index
[ Back ] [ Next ]

PROCEDURE
In an effort to conserve memory, and to provide the maximum macro definition storage space possible, the MCP preprocessor provided with Micro-C will
compress macro definitions by remove all comments and unnecessary whitespace characters from a macro before storing it in memory.
A problem can arise because MCP's idea of what is "unnecessary whitespace" is based on the syntax rules of C. No problem is posed if the macro contains
C source code, however if you wish to write a macro containing inline assembly language statements, you may run into difficulty.
Consider the C fragment: load -5,X
(Could occur in: printf("Load-5=%d, X=%d\n", load -5,X);
MCP (correctly) knows that this can be represented as: load-5,X
However consider the assembly statement: load -5,X and you will see that "load-5,X" causes a problem with most assemblers
because the space between an instruction and it's operands is REQUIRED!
There are two possible solutions to this problem:
The easiest solution would be to use the form of inline assembly language which places the assembly code within quotes: asm " load -5,X";
MCP knows to leave the content of quoted strings alone...
This may not always be a suitable solution, because you may want to use macro arguments (or other #defined names) within the assembly code, and
MCP also refuses to perform macro substitution within quoted strings...
Another solution, which allows you to use the asm { } form of inline assembly language, and which will allow macro substitution within the
assembly code is to make use of the fact that MCP cannot remove all of the whitespace between two valid names... Consider: return a;
MCP cannot change this to: returna; as that would change the meaning to 'C'. In fact, MCP uses the simple
rule that it will not join two valid names together. This we can use to "fool" MCP into leaving a space even where it normally would not.
The trick is to insert a null macro at the point where we want the space, and leave it separated from the name by at least one space. Due to the
way MCP processes nested macros, it will not remove the space:
#define _ /* Use to force embedded space */
#define MACRO asm {\
load _-5,X /* Define macro, retain embedded space */\
}
#undef _ /* No longer needed */ |
|