[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] inline assembler

From: Joerg Wunsch
Subject: Re: [avr-gcc-list] inline assembler
Date: Thu, 11 Oct 2001 12:37:34 +0200 (MET DST)

Torsten Hahn <address@hidden> wrote:

> Hi,
> i have a c-file like this:
> #define myreg r16
> .
> .
> .
> int main(void)
> {
> ...
> asm ("
>       LDI myreg, 0xff
> ");
> }
> But this does not work ! How can i tell avr-gcc, to use the #define(ed) 
> statements also in the inlined assembler sections ?

The problem is not the inline assembler statement, but actually that
(from the C point of view), you're trying to replace something that's
inside a string literal.  The C preprocessor doesn't apply macro
expansion there.  You can easily verify this by simply passing the
file just through the preprocessor only.  For one example, you can do
this by running "avr-gcc -E <filename>.c".

When running this with your file, you get:

% avr-gcc -E foo.c
# 3 "foo.c"

int main(void)
foo.c:10:6: warning: multi-line string literals are deprecated
asm ("
        LDI myreg, 0xff
# 10 "foo.c"


As you can see, it doesn't work (but you knew that already :), and you
get a warning.  The warning says that you should not run string
literals across several source lines, since this isn't compliant with
the C standard.  Instead, write a single "..." per line, which is
terminated by a \n.  Then, to allow for macro expansion, you have to
terminate the string right before the macro.  Also, the macro should
expand into a string literal itself.  This way, you'd get:

#define myreg "r16"


int main(void)
asm ("\n"
        "LDI " myreg ", 0xff\n"

That way, you exploit a feature of ANSI C where multiple adjacent
string literals are being concatenated, regardless of whether there's
white space (or even comments) between them.  Thus, writing

      "foo" /* this is a comment */ "bar"

will effectively yield the C string literal "foobar".  Note however
that you can't see /this/ already in the output of the C preprocessor,
since that feature is a feature of the language itself and thus
implemented in the compiler stage.  You can, however, see the result
in the generated assembler output (avr-gcc -S).

J"org Wunsch                                           Unix support engineer
address@hidden        http://www.interface-systems.de/~j/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]