Inline Assembler Question

Technical questions regarding the xTIMEcomposer, xSOFTip Explorer and Programming with XMOS.
Active Member
Posts: 50
Joined: Wed Jan 05, 2011 2:15 pm

Inline Assembler Question

Postby GerhardNorkus » Thu Mar 08, 2012 3:23 pm

Hi All,

Simple question...I hope. How do I access a function local variable with the inline assembler using ldw?

Example of the code below:

Code: Select all

long nGlobalVar ;
void MyFunction (int a)
    int b ;
    // This is global access
    asm("ldw r0, dp[nGlobalVar]") ;
    // How do I correctly do local access?
    // This will not compile correctly
    asm("ldw r0, sp[a]") ;
    // I can get the following to work, and avoid
    // the clobber registers, but I really want to simply
    // use a syntax similar to the line above
    asm("add r0, %0, 0" : : "r"(a) : "r0","r1","r2") ;
    // The above code will expand to
    //     ldw r11, sp[1]
    //     add r0, r11, 0
    // I want tight code    : (
User avatar
XCore Expert
Posts: 843
Joined: Sun Jul 11, 2010 1:31 am

Postby segher » Thu Mar 08, 2012 8:25 pm

You would write something like

Code: Select all

unsigned x, y;
asm("ldw %0,%1" : "=r"(x) : "m"(y));

but with the current tools that results in

Code: Select all

Could not match memory address.  Inline asm failure!

Another option is to pass the address of the var instead, like

Code: Select all

unsigned x, y;
asm("ldw %0,%1" : "=r"(x) : "p"(&y));

but that results in

Code: Select all

xcc2: /build/swnb/autobuild/Linux/sb_20110630_1915_02_1981/tools_llvm/src/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp:5326: void llvm::SelectionDAGLowering::visitInlineAsm(llvm::CallSite): Assertion `(OpInfo.ConstraintType == TargetLowering::C_RegisterClass || OpInfo.ConstraintType == TargetLowering::C_Register) && "Unknown constraint type!"' failed.
debug.c:189: internal compiler error: Aborted

Anyway, in general, it is best to pass things in registers, since XS1 is
a load-store architecture (that is, all instructions that access memory
do not compute things as well). It seems what you really want to do
is force things into the r0 register? If you can explain why, perhaps
there is a good solution.
XCore Addict
Posts: 132
Joined: Tue Dec 15, 2009 10:23 pm

Postby yzoer » Tue Apr 10, 2012 5:40 pm


Is there any specific reason you want to inline code? It's really easy to setup an external .S (capitalized, so it gets pre-processed) file and call that directly from XC.

Just wondering..


Return to “Development Tools and Programming”

Who is online

Users browsing this forum: ers35 and 6 guests