Go Down

Topic: How to give a C++ variable name to an asm statement? (Read 850 times) previous topic - next topic

GreyGnome

Hello,

I am trying to do something like this:

Code: [Select]
asm volatile("sts %0, r0" : "+M" (PCintPort::curr) );


But the error I'm getting is:

Code: [Select]
error: impossible constraint in 'asm'

I find that it's easy to do a c-style asm statment, like:

Code: [Select]

uint8_t myvariable;

asm volatile("sts myvariable, r0");


But using a static C++ class variable is confounding me.  The variable is declared thus:

Code: [Select]

class PCintPort {
public:
    static  uint8_t curr;
}


Thanks.

PeterH


Code: [Select]
asm volatile("sts %0, r0" : "+M" (PCintPort::curr) );



That syntax is nothing like your other working example. If (PCintPort::curr) is intended to be an input operand, shouldn't it be quoted? And probably separated by a colon from "+M" if that is intended to be an output operand.

If you aren't sure of the syntax of the assembler, I suggest you disassemble a bit of C++ that includes a reference to the type of variable you're after, and see what you get.
I only provide help via the forum - please do not contact me for private consultancy.

stimmer

Are you sure you know what you're doing? I *think* it's m not M but I am not sure. With inline asm the best technique is to search Google for as many examples as you can find of other people using it as it is not very well documented, the only way is to find someone elses example that works and copy it. Then disassemble the elf file and make sure it looks like it has done what you expect!
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

GreyGnome



Code: [Select]
asm volatile("sts %0, r0" : "+M" (PCintPort::curr) );


If you aren't sure of the syntax of the assembler, I suggest you disassemble a bit of C++ that includes a reference to the type of variable you're after, and see what you get.


I did that.  I got "0x0160" which is the memory location of the variable.  Of course, that will change as soon as I change my sketch... :-(  So I need to refer to it symbolically in my asm statement, which indeed I'm unaware of the syntax.  Google searching proved fruitless to me.

stimmer

I was right, it is m, here's the answer: http://gcc.gnu.org/onlinedocs/gcc-4.5.3/gcc/Simple-Constraints.html#Simple-Constraints

Also I found this: www.pek.nu/pekproj/proj/el/avrex/tiny2313_uart_firmware_update.c That code uses sts in inline assembler towards the end, you might be able to learn the syntax off it.

Also under some circumstances you need to clobber memory (usually when the function later on reads the variable you wrote to)

Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

GreyGnome

Thanks!  That's it!  The following statement is what I wanted:

Code: [Select]

asm volatile("sts %0, r0" : "+m" (PCintPort::curr) :: );


and it generated the following assembly language code:

     4be:       00 92 60 01     sts     0x0160, r0

Thanks again.  It finally compiles without complaining. :-)

Go Up