Questions tagged [inline-assembly]

Assembly that is embedded within a source in another, higher language, such as x86 assembly embedded in C or C++.

Inline assembly is used in higher level language to provide access to features not exposed by intrinsics. and are the most common "host" languages that allow inline asm.

Don't use inline asm without being aware of the potential performance downsides, as well as the obvious maintainability / portability downsides. The compiler can't understand inline asm for constant-propagation and other optimizations. If you can get the compiler to generate equivalent asm from normal source code without inline asm, that is almost always preferable.

Resources

The tag wiki has tons of good stuff for that architecture, and the tag wiki also has a few links.

Making a function-call from inline asm: avoid if possible


Non-x86

GNU C inline asm works the same way for non-x86 architectures (you still use input and output operands with constraints to get data into / out of asm statements).

There are differences: many targets don't have a constraint syntax for requesting a specific register (e.g. x86's "a" for eax/rax).

2169 questions
11
votes
1 answer

How do I get GCC to put a char in ah/bh/ch/dh?

Suppose I have some inline assembly that needs a particular char value in ah, bh, ch, or dh. How can I tell GCC to put it there? I don't see a relevant constraint to do that, but the GCC manual says "If you must use a specific register, but your…
11
votes
1 answer

GCC code that seems to break inline assembly rules but an expert believes otherwise

I was engaged with an expert who allegedly has vastly superior coding skills than myself who understands inline assembly far better than I ever could. One of the claims is that as long as an operand appears as an input constraint, you don't need to…
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
11
votes
2 answers

GNU C inline asm input constraint for AVX512 mask registers (k1...k7)?

AVX512 introduced opmask feature for its arithmetic commands. A simple example: godbolt.org. #include __m512i add(__m512i a, __m512i b) { __m512i sum; asm( "mov ebx, 0xAAAAAAAA; \n\t" …
tert
  • 113
  • 6
11
votes
1 answer

Compare and swap in C++

So we're using a version of boost which is pretty old for now, and until upgrading I need to have an atomic CAS operation in C++ for my code. (we're not using C++0x yet either) I created the following cas function: inline uint32_t CAS(volatile…
11
votes
2 answers

GetThreadID in assembly

I read the source code of FastMM4, and notice this interesting function function GetThreadID: Cardinal; {$ifdef 32Bit} asm mov eax, FS:[$24] end; {$else} begin Result := GetCurrentThreadID; end; {$endif} I've tested it, and it works, so my…
justyy
  • 5,831
  • 4
  • 40
  • 73
11
votes
2 answers

Linux assembler error "impossible constraint in ‘asm’"

I'm starting with assembler under Linux. I have saved the following code as testasm.c and compiled it with: gcc testasm.c -otestasm The compiler replies: "impossible constraint in ‘asm’". #include int main(void) { int foo=10,bar=15; …
slashmais
  • 7,069
  • 9
  • 54
  • 80
11
votes
6 answers

What's the purpose of using assembly language inside a C program?

What's the purpose of using assembly language inside a C program? Compilers are able to generate assembly language already. In what cases would it be better to write assembly than C? Is performance a consideration?
san6086
  • 459
  • 8
  • 20
11
votes
0 answers

How to convert Linux 32-bit gcc inline assembly to 64-bit code?

I'm attempting to convert RR0D Rasta Ring 0 Debugger from 32-bit mode to 64-bit mode (long mode) in Linux, using gcc. I'm familiar with x86 32-bit assembly (in MS-DOS environment) but I'm a beginner in x86 64-bit assembly and in Linux assembly…
nrz
  • 10,435
  • 4
  • 39
  • 71
10
votes
2 answers

Why can't I get the value of asm registers in C?

I'm trying to get the values of the assembly registers rdi, rsi, rdx, rcx, r8, but I'm getting the wrong value, so I don't know if what I'm doing is taking those values or telling the compiler to write on these registers, and if that's the case how…
Fayeure
  • 1,181
  • 9
  • 21
10
votes
5 answers

How do I specify immediate floating point numbers with inline assembly?

When I try to compile this code: #include main(int argc, char *argv[]) { double y = 0; __asm__ ("fldl $150;" "fsqrt;" "fstl %0;" : : "g" (y) ); printf("%f\n", y); return 0; } I get this…
poundifdef
  • 18,726
  • 23
  • 95
  • 134
10
votes
2 answers

How to use address constants in GCC x86 inline assembly

The GCC toolchain uses AT&T assembler syntax by default, but support for Intel syntax is available via the .intel_syntax directive. Additionally, both AT&T and Intel syntax are available in a prefix and a noprefix version, which differ in whether or…
Christoph
  • 164,997
  • 36
  • 182
  • 240
10
votes
1 answer

Writing a Linux int 80h system-call wrapper in GNU C inline assembly

I'm trying to use inline assembly... I read this page http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx but I can't understand the parameters passing to my function. I'm writing a C write example.. this is my function header: write2(char…
RodrigoCR
  • 197
  • 1
  • 2
  • 10
10
votes
1 answer

propagate carry bit between two ASM GCC inline block

Dear Assembly/C++ dev, The question is: Does propagate the carry (or any flag) between two ASM block is realistic or totally insane, even if it works ? A few years ago I developed an integer library for large arithmetic lower than 512 bits (at…
Timocafé
  • 765
  • 6
  • 18
10
votes
2 answers

``continue`` breaks label placement

This works just fine: #include int main(){ volatile int abort_counter = 0; volatile int i = 0; while (i < 100000000) { __asm__ ("xbegin ABORT"); i++; __asm__ ("xend"); __asm__ ("ABORT:"); …
User1291
  • 7,664
  • 8
  • 51
  • 108
10
votes
3 answers

Equivalent for NOP in C for Embedded?

I use KEIL to compile a program. The program uses the code asm("NOP"); Unfortunately KEIL compiler does not accept the statement. The idea is to introduce a delay by using NOP (no operation) assembly code. What is the actual equivalent of this in…
AAI
  • 290
  • 1
  • 3
  • 20