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

Can GCC emit different instruction mnemonics when choosing between multiple alternative operand constraints of inline assembly?

I am trying to write inline x86-64 assembly for GCC to efficiently use the MULQ instruction. MULQ multiplies the 64-bit register RAX with another 64-bit value. The other value can be any 64-bit register (even RAX) or a value in memory. MULQ puts the…
staufk
  • 91
  • 6
9
votes
2 answers

What does the GCC error message, "Error: unsupported for `mov'", mean?

I am just trying to compile some simple example code I typed in from a book, and GCC gives me the above error. Here's my code: $ cat -n test.cpp 1 #define READ_COMMAND 3 2 3 #define MSG_LENGTH 128 4 5 #include 6 …
Dan
  • 130
  • 1
  • 1
  • 8
9
votes
2 answers

C/C++ convert int to short and inline asm (ARM specific)

This isn't a trivial question. NOTE: I don't need opinions or advises to use pure asm. I actually need to get done what I'm talking about: to get inline asm without this sign/zero extend optcode when assigning result to a short int. I'm dealing with…
Pavel P
  • 15,789
  • 11
  • 79
  • 128
8
votes
3 answers

How to use a global variable in gcc inline assembly

I am trying to use inline assembly like this, for a global variable, but the compiler gives an error by saying undefined reference to saved_sp. __asm__ __volatile__ ( "movq saved_sp, %rsp\n\t" ); saved_sp is declared as static long saved_sp…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
8
votes
1 answer

Asking for help to fix inline assembly issue in D program

Hello I'm trying to use ASM in a little D program : asm { mov AX,12h ; int 10h ; } I've got this message : "end of instruction" from the two lines in the asm statement I cannot fix the issue, that's why I'me asking help from…
8
votes
1 answer

Avoiding hard-number shift of flexible second operand in ASM

This question pertains to the ARM assembly language. My question is whether it is possible to use a macro to replace the immediate value in the ASM code to shift a register value so that I don't have to hard-code the number. I'm not sure whether the…
Jay
  • 373
  • 1
  • 10
8
votes
1 answer

Google Benchmark Frameworks DoNotOptimize

I am a bit confused about the implementation of the function void DoNotOptimize of the Google Benchmark Framework (definition from here): template inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) { asm volatile("" : :…
Hymir
  • 811
  • 1
  • 10
  • 20
8
votes
5 answers

Delphi read overflow flag

If I do this var a,b,c:cardinal; begin a:=$80000000; b:=$80000000; c:=a+b; end; c will equal 0, since the addition overflowed. What's the best way to catch this overflowed boolean? (a+b
Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67
8
votes
1 answer

Calling a static function from global inline assembly

I'm trying to define a function without any prologues/epilogues, a "landing address" so it can call an inner function which is properly managed by the compiler (useful for a caller-cleanup environment). I found I can create a global label using…
Paul
  • 113
  • 1
  • 8
8
votes
4 answers

"Custom intrinsic" function for x64 instead of inline assembly possible?

I am currently experimenting with the creation of highly-optimized, reusable functions for a library of mine. For instance, I write the function "is power of 2" the following way: template inline bool is_power_of_two( const IntType…
8
votes
1 answer

Provide an example of querying Intel CPU capabilities using Rust's inline assembly

I am trying to use inline assembly in Rust. The code I'm including is supposed to query Intel CPU capabilities and right now I just want to get the ebx register state after the system call. Using an nightly build of Rust compiler (required to use…
Barry
  • 237
  • 1
  • 2
  • 11
8
votes
1 answer

How do you explain gcc's inline assembly constraints for the IN, OUT instructions of i386?

As far as I can tell, the constraints used in gcc inline assembly tell gcc where input and output variables must go (or must be) in order to generate valid assembly. As the Fine Manual says, "constraints on the placement of the operand". Here's a…
Robert B
  • 3,195
  • 2
  • 16
  • 13
8
votes
2 answers

How do I access local C variable in arm inline assembly?

I want to access local variable declared in C in inline arm Assembly. How do I do that? Global variables can be accessed like this, int temp = 0; Function(){ __asm( ".global temp\n\t" "LDR R2, =temp\n\t" …
Muzahir Hussain
  • 1,009
  • 2
  • 16
  • 35
8
votes
3 answers

128bit hash comparison with SSE

In my current project, I have to compare 128bit values (actually md5 hashes) and I thought it would be possible to accelerate the comparison by using SSE instructions. My problem is that I can't manage to find good documentation on SSE…
fokenrute
  • 739
  • 6
  • 17
8
votes
1 answer

GCC Inline Assembly 'Nd' constraint

I'm developing a small toy kernel in C. I'm at the point where I need to get user input from the keyboard. So far, I have implemented inb using the following code: static inline uint8_t inb(uint16_t port) { uint8_t ret; asm volatile("inb…
Michael Morrow
  • 403
  • 2
  • 16