A89: Re: (C language) Macro Expansion
[Prev][Next][Index][Thread]
A89: Re: (C language) Macro Expansion
Thanks! I knew there had to be a better way to do it.
I'll look into getting that book. I assume it's not just your standard
beginner's introduction, right?
Mark Leverentz
----- Original Message -----
From: Zoltan Kocsi <zoltan@bendor.com.au>
To: <assembly-89@lists.ticalc.org>
Sent: Sunday, August 29, 1999 6:42 PM
Subject: A89: (C language) Macro Expansion
>
> Mark Leverentz writes:
> >
> > I would like to create macros which are assembly statements, but the
trick
> > is that I want to be able to change what the assembly statement is.
> > For instance, I know it is possible to do this:
> >
> > #define MY_MACRO(x) asm("\n" x ": .byte 0\n");
> >
> > MY_MACRO(MyByte)
>
> With gcc you get a parsing error fo that. You should use
>
> MY_MACRO( "MyByte" )
>
> However, you don't want to do that, you want to do
>
> #define MY_MACRO(x) asm("\n" #x ": .byte 0\n");
>
> instead and then you *can* (and, in fact, must) use
>
> MY_MACRO( MyByte )
>
> > But things get more complicated. I would like to be able to pass
numbers as
> > parameters, and have them included in the string. For example:
> >
> > #define DECLARE_POINT(x,y) asm("\nMyRect: .long " x ", " y "\n");
> >
> > This will work only if x and y are literal strings, f.x.
> > DECLARE_POINT("3","4")
> >
> > Is there any way to do this without passing literal strings, f.x.
> > DECLARE_POINT(3,4) ?
>
> Using the above construct will solve your problem. It is an ANSI C
> thing therefore any ANSI C compiler will accept it. That is:
>
> #define DECLARE_POINT(x,y) asm("\nMyRect: .long " #x ", " #y "\n");
>
> and you can then do
>
> DECLARE_POINT(13,an_other_assembly_label+3 );
>
> will generate the expected
>
> asm( "\nMyRect: .long 13 , an_other_assembly_label+3\n" );
>
> and so on.
>
> It is based om 2 things:
>
> ANSI C defines the # operator in macro parameter expansion, which
> causes the parameter substitutrion to be surrounded by double quotes.
>
> ANSI C also declares that two character strings with only whitespace
> between them will be concatenated into one string.
>
> I suggest reading
>
> B. W. Kernighan - D. M. Ritchie
> "The C Programming Language",
> Second Edition.
> ISBN 0-13-110362-8
>
> Regards,
>
> Zoltan
>
>
Follow-Ups:
References: