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
100
votes
3 answers

The difference between asm, asm volatile and clobbering memory

When implementing lock-free data structures and timing code it's often necessary to suppress the compiler's optimisations. Normally people do this using asm volatile with memory in the clobber list, but you sometimes see just asm volatile or just a…
jleahy
  • 16,149
  • 6
  • 47
  • 66
85
votes
5 answers

Is there a way to insert assembly code into C?

I remember back in the day with the old borland DOS compiler you could do something like this: asm { mov ax,ex etc etc... } Is there a semi-platform independent way to do this now? I have a need to make a BIOS call, so if there was a way to do…
Nathan
  • 11,938
  • 12
  • 55
  • 62
82
votes
4 answers

Why does adding inline assembly comments cause such radical change in GCC's generated code?

So, I had this code: constexpr unsigned N = 1000; void f1(char* sum, char* a, char* b) { for(int i = 0; i < N; ++i) { sum[i] = a[i] + b[i]; } } void f2(char* sum, char* a, char* b) { char* end = sum + N; while(sum != end) { …
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
81
votes
2 answers

Can I use Intel syntax of x86 assembly with GCC?

I want to write a small low level program. For some parts of it I will need to use assembly language, but the rest of the code will be written on C/C++. So, if I will use GCC to mix C/C++ with assembly code, do I need to use AT&T syntax or can I…
Hlib
  • 2,944
  • 6
  • 29
  • 33
76
votes
6 answers

C/C++ function definitions without assembly

I always thought that functions like printf() are, in the last step, defined using inline assembly. That deep in the bowels of stdio.h is buried some asm code that actually tells CPU what to do. For example, in dos, I remember it was implemented by…
Jack
  • 761
  • 1
  • 7
  • 4
67
votes
3 answers

What does __asm__ __volatile__ do in C?

I looked into some C code from http://www.mcs.anl.gov/~kazutomo/rdtsc.html They use stuff like __inline__, __asm__ etc like the following: code1: static __inline__ tick gettick (void) { unsigned a, d; __asm__ __volatile__("rdtsc": "=a" (a),…
user3692521
  • 2,563
  • 5
  • 27
  • 33
64
votes
7 answers

Efficient integer compare function

The compare function is a function that takes two arguments a and b and returns an integer describing their order. If a is smaller than b, the result is some negative integer. If a is bigger than b, the result is some positive integer. Otherwise, a…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
57
votes
4 answers

What is the difference between 'asm', '__asm' and '__asm__'?

As far as I can tell, the only difference between __asm { ... }; and __asm__("..."); is that the first uses mov eax, var and the second uses movl %0, %%eax with :"=r" (var) at the end. What other differences are there? And what about just asm?
Adrian
  • 14,931
  • 9
  • 45
  • 70
55
votes
4 answers

Is it possible to include inline assembly in Go code?

Is it possible to include inline assembly in Go code? This blog post shows compiling Go to a separate .s file and editing it, but not inline asm as part of a Go function like many C compilers support.
Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
48
votes
8 answers

Reading a register value into a C variable

I remember seeing a way to use extended gcc inline assembly to read a register value and store it into a C variable. I cannot though for the life of me remember how to form the asm statement.
Brian
  • 481
  • 1
  • 4
  • 3
47
votes
2 answers

Labels in GCC inline assembly

In my ongoing experimentation with GCC inline assembly, I've run into a new problem regarding labels and inlined code. Consider the following simple jump: __asm__ ( "jmp out;" "out:;" : : ); This does nothing except jump to the out…
Channel72
  • 24,139
  • 32
  • 108
  • 180
36
votes
12 answers

Alloca implementation

How does one implement alloca() using inline x86 assembler in languages like D, C, and C++? I want to create a slightly modified version of it, but first I need to know how the standard version is implemented. Reading the disassembly from…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
35
votes
4 answers

64bit Applications and Inline Assembly

I am using Visual C++ 2010 developing 32bit windows applications. There is something I really want to use inline assembly. But I just realized that visual C++ does not support inline assembly in 64bit applications. So porting to 64bit in the…
JavaMan
  • 4,954
  • 4
  • 41
  • 69
35
votes
8 answers

x86/x64 CPUID in C#

Related to my other question, please help me debug "An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module. Additional information: Attempted to read or write protected memory. This is often an indication that…
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
30
votes
4 answers

Address of labels (MSVC)

We are writing a byte-code for a high-level compiled language, and after a bit of profiling and optimization, it became clear that the current largest performance overhead is the switch statement we're using to jump to the byte-code cases. We…
Trevor Sundberg
  • 1,584
  • 1
  • 14
  • 25
1
2 3
99 100