CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

"expecting an opcode mnemonic" in macro definition

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Felix Althaus



Joined: 09 Sep 2003
Posts: 67
Location: Winterthur, Switzerland

View user's profile Send private message

"expecting an opcode mnemonic" in macro definition
PostPosted: Sat Nov 17, 2001 8:15 am     Reply with quote

Hello

Why does the following macro generate an error (expecting an opcode mnemonic)?

#define test(x); \
#if(0)\
#asm \
movlw 0x00 \
movlw 0x00 \
movlw 0x00 \
#endasm \
#endif \

If the if() is true (=1) everything works ok.

Thanks
Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 1170
Tomi
Guest







Re: "expecting an opcode mnemonic" in macro defini
PostPosted: Sat Nov 17, 2001 8:37 am     Reply with quote

What do you mean with that #if ?
#if means: "evaluate the expression. If it is TRUE, insert the lines til #endif. If it is FALSE, IGNORE the lines til #endif".
So your code read by the compiler:
#define test(x); //Huuuuuuuhhhhhhhh?

Maybe you want to use "if" instead of "#if" ?
#define test(x) \
if(!x) \
#asm \
movlw 0x00 \
movlw 0x00 \
movlw 0x00 \
#endasm \
else \
#asm \
movlw 0xFF \
movlw 0xFF \
movlw 0xFF \
#endasm \
The list:
0000 00341 .................... test(i);
0068 08AA 00342 MOVF 2A,F
0069 1D03 00343 BTFSS 03,2
006A 286D 00344 GOTO 06D
006B 3000 00345 MOVLW 00
006C 286E 00346 GOTO 06E
006D 30FF 00347 MOVLW FF

:=Hello
:=
:=Why does the following macro generate an error (expecting an opcode mnemonic)?
:=
:=#define test(x); \
:=#if(0)\
:=#asm \
:=movlw 0x00 \
:=movlw 0x00 \
:=movlw 0x00 \
:=#endasm \
:=#endif \
:=
:=If the if() is true (=1) everything works ok.
:=
:=Thanks
:=Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 1172
Felix Althaus



Joined: 09 Sep 2003
Posts: 67
Location: Winterthur, Switzerland

View user's profile Send private message

Re: "expecting an opcode mnemonic" in macro defini
PostPosted: Sat Nov 17, 2001 8:50 am     Reply with quote

:=What do you mean with that #if ?

I thought I can simulate (for debugging of the macro) any expression with #if(1) = TRUE (=> insert code) and #if(0) = FALSE (=> do not insert it), so I don't have to worry about the expression. But the error occurs anyway if I use a complex expression or only a 1 or 0.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1174
Tomi
Guest







Re: "expecting an opcode mnemonic" in macro defini
PostPosted: Sat Nov 17, 2001 9:27 am     Reply with quote

If you are in a #define directive you MUST give two parameters.
So you have two choices:
If you want to use #define all times, use #else:
#if(0) // define for NULL
#define test(x) \
#asm \
movlw 0x00 \
movlw 0x00 \
movlw 0x00 \
#endasm \
#else // define for NOT NULL
#define test(x) \
#asm \
movlw 0xFF \
movlw 0xFF \
movlw 0xFF \
#endasm \
#endif

Or you can skip the entire #define:
#if(0) \
#define test(x) \
#asm \
movlw 0x00 \
movlw 0x00 \
movlw 0x00 \
#endasm \
#endif
But in this case you don't have the test() macro (definition is skipped).

:=:=What do you mean with that #if ?
:=
:=I thought I can simulate (for debugging of the macro) any expression with #if(1) = TRUE (=> insert code) and #if(0) = FALSE (=> do not insert it), so I don't have to worry about the expression. But the error occurs anyway if I use a complex expression or only a 1 or 0.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1177
Felix Althaus



Joined: 09 Sep 2003
Posts: 67
Location: Winterthur, Switzerland

View user's profile Send private message

Re: "expecting an opcode mnemonic" in macro defini
PostPosted: Sat Nov 17, 2001 9:51 am     Reply with quote

Ok - let me begin at the beginning:

I want to write a macro, which produces a few lines of assembly code. One of these lines is a movlw instruction. The literal is calculated from the macro parameters (but there is no problem).

A few other assembly lines only will be included if the parameters meet any condition (for this I use the #if(expr) statement). My problem is now, that if the expression is true, everything works ok, but if the expression is FALSE an error (expecting opcode...) occurs.

again my code:

#define test(x); \
// any code that works ok.
#if(0)\ //only for debugging, simulates a false expression
#asm \
movlw 0x00 \ // only to simulate any code
movlw 0x00 \
movlw 0x00 \
#endasm \
#endif \

My question again: Why does this (i.e expression = FALSE) generate an error?
I thought, if this expression is false, the entire #asm section wouldn't be written.

Thanks
Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 1178
Tomi
Guest







Re: "expecting an opcode mnemonic" in macro defini
PostPosted: Sat Nov 17, 2001 10:14 am     Reply with quote

Hallo Felix,
Maybe I can help you only when I can see your entire macro.
Macros are simple but they have a strong syntax.
Usually you must use a "head compiler" to read the text simply as the compiler does. It is a simple "Lego".
for example, if I write:
#define test(x) \
Restart_wdt(); \
#if(0) \
#asm \
movlw 0x00 \
movlw 0x00 \
movlw 0x00 \
#endasm \
#endif \
It gives a "missing opcode" error. The line:
#define test(x) Restart_wdt();
is terminated with a ";" what is illegal. Correct:
#define test(x) \
Restart_wdt() \
#if(0) ;\ // this is NOT an empty instruction but a terminating
#asm \
movlw 0x00 \
movlw 0x00 \
movlw 0x00 \
#endasm \
#endif \

So if the expression is continued after the common instruction(s) it is necessary to close that instr. with a ; (if it is a C instr.).
___________________________
This message was ported from CCS's old forum
Original Post ID: 1179
Felix Althaus



Joined: 09 Sep 2003
Posts: 67
Location: Winterthur, Switzerland

View user's profile Send private message

Thanks
PostPosted: Sat Nov 17, 2001 10:40 am     Reply with quote

Hello

I tested the following code and it works perfectly. Thanks!
But a have still a question:

#define test(x); \
#asm \
movlw 0x00 \
#endasm \
#if(0); \
movlw 0x00 \
movlw 0x00 \
#endasm \
#asm \
movlw 0x00 \
#endif \

If I "head-compiled" it right, this code should produce:
movlw 0x00;
But why is the ; necessary in assembly?

Thanks
Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 1180
Tomi
Guest







Re: Thanks
PostPosted: Sat Nov 17, 2001 11:28 am     Reply with quote

Hello,
the answer is so difficult...
First of all, there is a confusion about using #asm #endasm pairs. Legally you must use them as BEGIN - END pairs.
About the Q: 0 is never TRUE so the line is theoretically "movlw 0x00" (; is missing). Let's talk about the soul of the compilers: they know a compiler directive as a NEW LINE starts with "#". But on the other hand the #define directive must fit only one line. The "\" is not a trick: it is for a better readibility but your macro is an exact one-line expression after the pre-processing. So what about a #if in a #define directive? In the expression:
#define mymacro #if(0) ........ #endif
the #if(0) is not at the beginning of the line. Fortunately you are lucky and CCS C not always senses the difference between the <CR-LF> line-terminator and the ";" command delimiter (as my favorite professor said: "This is not a bug. This is a new feature."). Shortly: in your example the ";" is a "directive delimiter" ("hardly line break") not defined in standard C.

Once more, it would be better to use the standard C and write this:
#if(0)
#define test(x); \
#asm \
movlw 0x00 \
#endasm
#else
#define test(x); \
#asm \
movlw 0x00 \
movlw 0x00 \
movlw 0x00 \
movlw 0x00 \
#endasm
#endif

Pay attention on the "\" chars: use them only if it is necessary (not in all lines).


___________________________
This message was ported from CCS's old forum
Original Post ID: 1181
Felix Althaus



Joined: 09 Sep 2003
Posts: 67
Location: Winterthur, Switzerland

View user's profile Send private message

Re: Thanks
PostPosted: Sat Nov 17, 2001 12:01 pm     Reply with quote

Thanks

You use
#if(0)
#define test(x);

But I have a problem with it: In the if expression I use the macro parameters and so it doesn' seem to work.

mfg

Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 1182
Tomi
Guest







Re: Thanks
PostPosted: Sat Nov 17, 2001 12:35 pm     Reply with quote

Hajaj,
Maybe now I understand what you mean. But the input parameter is either a RAM variable or a pre-defined constant (like _DEBUG)?
Be careful with #if: it is evaluated on compile time, so you could not enter a variable or reference for it. You must use a constant expression, e.g.:
#define VERSION 0x0300
#define MAINVERSION 0x03
#define SUBVERSION 0x01
#if (VERSION >= 0x0300)
#if (VERSION >= (MAINVERSION << 8) | SUBVERSION)
etc.

If your input parameter "x" is a variable it is exists only at run-time but not in compile time.

:=Thanks
:=
:=You use
:=#if(0)
:=#define test(x);
:=
:=But I have a problem with it: In the if expression I use the macro parameters and so it doesn' seem to work.
:=
:=mfg
:=
:=Felix
___________________________
This message was ported from CCS's old forum
Original Post ID: 1183
Felix Althaus



Joined: 09 Sep 2003
Posts: 67
Location: Winterthur, Switzerland

View user's profile Send private message

Re: Thanks
PostPosted: Sat Nov 17, 2001 1:05 pm     Reply with quote

:=Maybe now I understand what you mean. But the input parameter is either a RAM variable or a pre-defined constant (like _DEBUG)?

The parameters are predefined constants (from a *.h file).
___________________________
This message was ported from CCS's old forum
Original Post ID: 1184
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group