cancel
Showing results for 
Search instead for 
Did you mean: 

Inserting asm instructions in C

nanayakkaraan
Associate II
Posted on October 29, 2010 at 14:10

Inserting asm instructions in C

4 REPLIES 4
Posted on May 17, 2011 at 14:13

Try:
 __asm 
volatile
(
''cpsie i''
);

__asm
(
''cpsie i''
);
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 17, 2011 at 14:13

''I need (sic?) to insert some assembly code into my C code''

Do you - really?!

In many (most?) cases, this approach is fundamentally flawed:

When you write in a High-Level Language (HLL) such as 'C', you delegate responsibility for code generation and the management of CPU resgisters, etc, to the Compiler - if you just ''jump in'' with the odd piece of arbitrary assembler code you are highly likely to mess stuff up!

So, what is it that you feel cannot be done in 'C', and yet will be safe to just insert arbitrarily into the middle of a 'C' file?

You will need a thorough understanding of what is and is not safe to fiddle with and what can and cannot be relied upon for your particular version of your particular compiler.

If you have something that really does need to be in assembler, it's usually better to put it into a proper assembler routine in a proper assembler source file of its own and call that from 'C.

Well, that's my 2p...

Posted on May 17, 2011 at 14:13

In the specific case of CPSIE, search your compiler manuals for something similar to this:

http://www.keil.com/support/man/docs/armcc/armcc_CHDFGFAB.htm#Ciabijcc

Posted on May 17, 2011 at 14:13

See: 

http://www.keil.com/forum/17856/

Here's a good paper from ARM TechCon about mixing assembler with High-Level Languages (HLLs) such as C and C++

http://www.eetimes.com/design/embedded/4210485/Reliable-programming-in-ARM-assembly-language?pageNumber=0

Note particularly the comment about inline assembly:

''While [inline assembly] may be an acceptable mechanism for somebody familiar with the inner workings of the ... compiler to provide assembly capabilities, they are very dangerous in the hands of a novice.''

It seems to me that the vast majority of questions about inline assembler on this (and other) forums are from novices - and, thus, are generally bad ideas.

The article outlines 3 ways to access assembler functionality, in order of preference:

1. First, use Compiler Intrinsics wherever possible;

2. Use separate assembler source files, with functions to be called from the HLL;

3. As a last resort, and only for experienced developers, use inline assembler.