Re: [cc65] Macros in inline assembler

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2012-01-19 17:59:12
On Thu, Jan 19, 2012 at 04:35:26PM +0100, "Andreas Rückert" wrote:
> I did something else and just unrolled the loop by hand
> ========
>         uint8 *datap = data;
>
>         __asm__( "lda %v+3", datap);
>         __asm__( "sta %v", W);
>         __asm__( "lda %v+2", datap);
>         __asm__( "sta %v+1", W);
>         __asm__( "lda %v+1", datap);
>         __asm__( "sta %v+2", W);
>         __asm__( "lda %v", datap);
>         __asm__( "sta %v+3", W);
>
>         __asm__( "lda %v+19", datap);
>         __asm__( "sta %v+4", W);
> ...
> ========
> , which is also wrong as I see it, since the compiler might
> rearrange the asm statements.

It might rearrange/change/delete the asm *instructions*. But this doesn't
depend on using one or multiple asm statements. And if your code doesn't do
something special, you don't have to fear anything. While the optmizer might
rearrange the code or remove stuff, it will only do so if the behaviour
doesn't change. The most prominent exception is data that is "volatile". The
compiler doesn't know about volatile data and may remove instructions that
reference memory if they don't seem to have an effect. But this is also true
for C code reading or writing such data.

BTW: There seems to be another error with the code above which makes me wonder
why it works: You're creating a pointer to your data, and then you use the
storage occupied by the pointer, not the storage it points to. Are you sure
this is correct?

> > And, using the token
> > pasting operator (##) is wrong, because pasting two strings together gives
> > an
> > invalid token. Strings do not need an operator to have them concatenated.
>
> That's what I thought. But when I had the macro argument in the
> string, I always got complaints, that asm expectected a ')'.

If you get errors like this, it is always advisable to strip down the code to
the minimum (in your case: one asm line), and then add more stuff until it
breaks. This allows you to find the location of the errors without having to
guess what might be wrong.

> > I would first fix the problems then try again. There are some problems
> > with
> > cc65 macros, but as far as I can say, your code doesn't trigger any of
> > them.
>
> So far I just accepted, that they don't work in asm Statements.

They do work as they should. But not if your macros are wrong.

> I know. It just couldn't find an example in the docs...

There is an example titled "The next example shows how to use global variables
to exchange data between C an assembler ...". What is wrong with this one?
BTW: If you have any suggestions how to improve the documentation, you're
always welcome!

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Thu Jan 19 17:59:23 2012

This archive was generated by hypermail 2.1.8 : 2012-01-19 17:59:26 CET