Why need to wrote in assembly?

Discussions about USB Audio on XMOS devices
Junior Member
Posts: 4
Joined: Mon Jun 03, 2024 10:19 am

Why need to wrote in assembly?

Post by Henry »

Hi Xcore team,
I'm confused when I met the assembly code in the xc source file, e.g. clockgen.xc, from line 369 :

Code: Select all

                    /* Read level data */
                    //g_inputLevelData[i] = samples_to_host_inputs[i];
                    asm volatile("ldw %0, %1[%2]":"=r"(tmp):"r"((const int *)samples_to_host_inputs),"r"(i));
                    g_inputLevelData[i] = tmp;

                    /* Reset level data */
                    //samples_to_host_inputs[i] = 0;
                    asm volatile("stw %0, %1[%2]"::"r"(0),"r"((const int *)samples_to_host_inputs),"r"(i));
Why this need to coding as assembly ?
Why can not use the commented C code ?
Is it written as C code, the compiler will convert it into more assembly code, which will affect the execution speed?

Thanks a lot.
User avatar
XCore Legend
Posts: 1133
Joined: Thu May 27, 2010 10:08 am

Post by infiniteimprobability »

That's a fair question. This looks like a speed optimisation to me. The original author helpfully left the high level code to make it clear what. the intent was. XC, by default, does array bounds checking. 90% of the time this is very helpful and catches an awful lot of mistakes (ask me how I know..) but it does add a few cycles and bytes for the bounds checking code. For inner loop stuff (especially copying arrays at 192 kHz) that might be too expensive.

Note that any code written in pure C for XMOS does not have the array bounds checking. You're on your own making sure things point to the right address.

XC supports #pragma unsafe arrays which should do this for you automatically. This code was written a long time ago (and is well verified) and it's possible that that pragma was not available at the time of writing.

See also https://www.xcore.com/viewtopic.php?t=1155


https://www.xmos.com/documentation/XM-0 ... agmas.html
Engineer at XMOS