This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Literalizing the '#' character in #define

The C51 pre-processor treats the "#" character as the "Stringize" operator. How do I override this operator when desired?

For example:

#define X mov A,#10

Expands to:
mov A,10

How can I force a literal expansion of the "#" character such that the above macor expands to:
mov A,#10

Thanks.

  • Perhaps you could use the trigraph for the octothorpe? (??=) Or the digraph? ($:) Or the backslash escape sequence? (\x23 or \043)

  • I had already tried the trigraph form and verified that it does not work, but stopped there.

  • hi,

    this is yet another cause to use assembler language in separate .asm file.
    Have you the reason to combine with both C and ASM routines in one file really?

    Regards,
    Oleg

  • Do you not have a real name?

    "The C51 pre-processor treats the "#" character as the "Stringize" operator. How do I override this operator when desired?"

    Are you saying that the C51 preprocessor is not working in accordance with the standard ANSI rules?

  • Sorry for the "Studio Tech" moniker -- that is the default name on this machine.

    The advice for using a separate ".asm" file (i.e., the A51 "macro" directive instead of "#define") is certainly a workable solution. We do have occasion to generate in-line assembler within C programs and find that a couple of simple #define statements within the ".h" header file is a bit easier to maintain when extensive assembler code is not needed.

    Regardless, it would still be useful to know if it possible to generate a "#" from within a #define macro when using the Ax51 assembler. Regarding ANSI compliance, the A51 User Manual does offer the following two caveats on page 158:
    NOTES
    The Ax51 macro assembler does not accept C escape sequences like "\n", "\r" or
    "\x0d". You need to replace these characters with hex values.

    Unlike the Cx51 compiler, multiple strings are not concatenated to a single
    string by the Ax51 macro assembler. Therefore you need to separate multiple
    items with a comma when using the Ax51 macro assembler.


    I just wish I knew what either of these restrictions meant in terms of actual code construction! As far as I can tell, the C-macro preprocessor within A51 does not expand escape sequences or "hex values" at all.

  • hi,

    Regardless, it would still be useful to know if it possible to generate a "#" from within a #define macro when using the Ax51 assembler.

    If you use the assembler and needs with such definitions then I suggest you to use macro. For example:

    XCOM	MACRO
    	mov A,#10
    	ENDM
    
    Now you may use macro XCOM anywhere in assembly file. And no #define needs.

    If you use C-language and need define some assembly commands which will be used somewhere then I may suggest next way:
    - define it as C-macro:
    #define XCOM(x) MOV A,x
    //... somewhere in program:
    #pragma ASM
    XCOM(#10)
    #pragma ENDASM

    Regards,
    Oleg

  • Thank you Oleg for your useful work-arounds. I have reverted to the 'macro' directive for the subject piece of code, although we will need to maintain separate versions for different assemblers and compilers, which is what we were trying to avoid.