Pages: [1]   Go Down
Author Topic: How to give a C++ variable name to an asm statement?  (Read 807 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am trying to do something like this:

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

But the error I'm getting is:

Code:
error: impossible constraint in 'asm'

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

Code:
uint8_t myvariable;

asm volatile("sts myvariable, r0");

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

Code:
class PCintPort {
public:
    static  uint8_t curr;
}

Thanks.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged


0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)

Logged


0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 94
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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. :-)
Logged

Pages: [1]   Go Up
Jump to: