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
30
votes
5 answers

How do I do inline assembly on the IPhone?

How is it done? What steps do I need to take and what pitfalls and gotchas are there to consider?
Hans Sjunnesson
  • 21,745
  • 17
  • 54
  • 63
28
votes
2 answers

How to invoke a system call via syscall or sysenter in inline assembly?

How can we implement the system call using sysenter/syscall directly in x86 Linux? Can anybody provide help? It would be even better if you can also show the code for amd64 platform. I know in x86, we can use __asm__( " movl $1, %eax …
Infinite
  • 3,198
  • 4
  • 27
  • 36
25
votes
5 answers

Inline assembly that clobbers the red zone

I'm writing a cryptography program, and the core (a wide multiply routine) is written in x86-64 assembly, both for speed and because it extensively uses instructions like adc that are not easily accessible from C. I don't want to inline this…
Mike Hamburg
  • 359
  • 2
  • 5
24
votes
2 answers

Is there an equivalent instruction to rdtsc in ARM?

For my project I must use inline assembly instructions such as rdtsc to calculate the execution time of some C/C++ instructions. The following code seems to work on Intel but not on ARM processors: {unsigned a, d;asm volatile("rdtsc" : "=a" (a),…
Curious
  • 373
  • 1
  • 2
  • 8
23
votes
1 answer

GCC inline assembly: constraints

I'm having difficulty understanding the role constraints play in GCC inline assembly (x86). I've read the manual, which explains exactly what each constraint does. The problem is that even though I understand what each constraint does, I have very…
Channel72
  • 24,139
  • 32
  • 108
  • 180
21
votes
2 answers

Is it possible to write inline assembly in Swift?

I was wondering if you can write inline assembly in Swift. I know that in Objective-C you could use something like this: inline void assemblyFunc() { __asm__(/*Assembly*/); } But in Swift it seems that you can't use __asm__(/*Assembly*/). Does…
Bas
  • 4,423
  • 8
  • 36
  • 53
21
votes
2 answers

How to specify register constraints on the Intel x86_64 register r8 to r15 in GCC inline assembly?

Here's the list of register loading codes: a eax b ebx c ecx d edx S esi D edi I constant value (0 to 31) q,r dynamically allocated register (see below) g eax, ebx, ecx,…
Jin Chen
  • 632
  • 1
  • 4
  • 10
20
votes
9 answers

Negative clock cycle measurements with back-to-back rdtsc?

I am writing a C code for measuring the number of clock cycles needed to acquire a semaphore. I am using rdtsc, and before doing the measurement on the semaphore, I call rdtsc two consecutive times, to measure the overhead. I repeat this many times,…
Discipulus
  • 245
  • 1
  • 3
  • 13
19
votes
2 answers

When to use earlyclobber constraint in extended GCC inline assembly?

I understand when to use a cobbler list (e.g. listing a register which is modified in the assembly so that it doesn't get chosen for use as an input register, etc), but I can't wrap my head around the the earlyclobber constraint &. If you list your…
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
18
votes
4 answers

What is the use of .byte assembler directive in gnu assembly?

While going through some C code having inline assembly I came across the .byte (with a Dot at the beginning) directive. On checking the assembly reference on web I found that it is used to reserve a byte in memory. But in the code there was no label…
vjain27
  • 3,514
  • 9
  • 41
  • 60
18
votes
7 answers

x86 Assembly: INC and DEC instruction and overflow flag

In x86 assembly, the overflow flag is set when an add or sub operation on a signed integer overflows, and the carry flag is set when an operation on an unsigned integer overflows. However, when it comes to the inc and dec instructions, the situation…
Channel72
  • 24,139
  • 32
  • 108
  • 180
17
votes
3 answers

How to set a variable in GCC with Intel syntax inline assembly?

Why doesn't this code set temp to 1? How do I actually do that? int temp; __asm__( ".intel_syntax;" "mov %0, eax;" "mov eax, %1;" ".att_syntax;" : : "r"(1), "r"(temp) : "eax"); printf("%d\n", temp);
user541686
  • 205,094
  • 128
  • 528
  • 886
17
votes
3 answers

error: ‘asm’ undeclared (first use in this function)

I am getting the following error during compilation: error: ‘asm’ undeclared (first use in this function) EXCHANGE( s, *(a) ); ^ in a header file where the macro is invoked as follows: EXCHANGE( s, *(a) ); and the actual defintion of the macro…
Black_Zero
  • 445
  • 1
  • 6
  • 12
17
votes
2 answers

Using base pointer register in C++ inline asm

I want to be able to use the base pointer register (%rbp) within inline asm. A toy example of this is like so: void Foo(int &x) { asm volatile ("pushq %%rbp;" // 'prologue' "movq %%rsp, %%rbp;" // 'prologue' …
jaw
  • 510
  • 6
  • 15
17
votes
2 answers

In GNU C inline asm, what are the size-override modifiers for xmm/ymm/zmm for a single operand?

While trying to answer Embedded broadcasts with intrinsics and assembly, I was trying to do something like this: __m512 mul_bcast(__m512 a, float b) { asm( "vbroadcastss %k[scalar], %q[scalar]\n\t" // want vbcast.. %xmm0, %zmm0 …
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847