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

Linux kernel assembly and logic

My question is somewhat weird but I will do my best to explain. Looking at the languages the linux kernel has, I got C and assembly even though I read a text that said [quote] Second iteration of Unix is written completely in C [/quote] I thought…
daniels_pa
  • 267
  • 4
  • 13
10
votes
7 answers

What's an example of a simple C function which is faster implemented in inline assembly?

I'm having a hard time beating my compiler using inline assembly. What's a good, non-contrived examples of a function which the compiler has a hard time making really, really fast and simple? But that's relatively simple to make with inline…
Hans Sjunnesson
  • 21,745
  • 17
  • 54
  • 63
10
votes
2 answers

Why does GCC inline assembler require clobbering information, but MSVC doesn't

I don't understand how this is supposed to work. GCC inline assembler is a pain to get right, but very specific about marking clobbering information, so that the compiler knows what you're doing. Microsoft Visual C++'s inline assember is really easy…
user541686
  • 205,094
  • 128
  • 528
  • 886
9
votes
2 answers

Converting help: __asm__ __volatile__

I would like to port C's outb function to D. static __inline void outb (unsigned char value, unsigned short int port) { __asm__ __volatile__ ("outb %b0,%w1" : : "a"…
user762630
9
votes
3 answers

is it possible to define a full assembly function in a c++ file

i use vscode with gcc on windows. i know that it is possible to write a c++ function and use asm("code"), however i came accross the following yt video:https://www.youtube.com/watch?v=7Xe9pCrzH98 where the author showcases the following c++…
9
votes
3 answers

Correct way to wrap CMPXCHG8B in GCC inline assembly, 32 bits

I'm trying to write GCC inline asm for CMPXCHG8B for ia32. No, I cannot use __sync_bool_compare_and_swap. It has to work with and without -fPIC. So far the best I've (EDIT: does not work after all, see my own answer below for details) is register…
Laurynas Biveinis
  • 10,547
  • 4
  • 53
  • 66
9
votes
2 answers

Why are Rust stack frames so big?

I encountered an unexpectedly early stack overflow and created the following program to test the issue: #![feature(asm)] #[inline(never)] fn get_rsp() -> usize { let rsp: usize; unsafe { asm! { "mov {}, rsp", …
chuck
  • 1,420
  • 4
  • 19
9
votes
1 answer

What does __asm volatile ("pause" ::: "memory"); do?

I am looking at an open source C++ project which has the following code structure: while(true) { // Do something work if(some_condition_becomes_true) break; __asm volatile ("pause" ::: "memory"); } What does the last statement do? I…
John Elaine
  • 359
  • 5
  • 22
9
votes
5 answers

CMPXCHG16B correct?

This doesn't exactly seem to be right although I am unsure why. Advice would be great as the documentation for CMPXCHG16B is pretty minimal (I don't own any intel manuals...) template<> inline bool cas(volatile types::uint128_t *src,…
JH.
  • 4,147
  • 3
  • 19
  • 20
9
votes
5 answers

how to set control register 0 (cr0) bits in x86-64 using gcc assembly on linux

I am using the following code to set the cr0 bit to disable cache. When I compile this #include int main() { __asm__("pushl %eax\n\t" "mov %cr0,%eax;\n\t" "orl $(1 << 30),%eax;\n\t" …
pranith
  • 869
  • 9
  • 24
9
votes
2 answers

Is Intel's timestamp reading asm code example using two more registers than are necessary?

I'm looking into measuring benchmark performance using the time-stamp register (TSR) found in x86 CPUs. It's a useful register, since it measures in a monotonic unit of time which is immune to the clock speed changing. Very cool. Here is an Intel…
Edd Barrett
  • 3,425
  • 2
  • 29
  • 48
9
votes
2 answers

Why is one of these sooooo much faster than the other?

I'm writing C++ code to find the first byte in memory that is non 0xFF. To exploit bitscanforward, I had written an inline assembly code that I like very much. But for "readability" as well as future proofing (i.e. SIMD vectorization) I thought I…
codechimp
  • 1,509
  • 1
  • 14
  • 21
9
votes
2 answers

Does __asm{}; return the value of eax?

Simple question. The function asm in C is used to do inline assembly in your code. But what does it return? Is it the conventional eax, and if not, what does it return?
Delupara
  • 359
  • 1
  • 4
  • 10
9
votes
4 answers

What is r() and double percent %% in GCC inline assembly language?

Example: int main(void) { int x = 10, y; asm ("movl %1, %%eax;" "movl %%eax, %0;" :"=r"(y) /* y is output operand */ :"r"(x) /* x is input operand */ :"%eax"); /* %eax is clobbered register…
brett
  • 5,379
  • 12
  • 43
  • 48
9
votes
2 answers

Constraining r10 register in gcc inline x86_64 assembly

I'm having a go at writing a very light weight libc replacement library so that I can better understand the kernel - application interface. The first task is clearly getting some system call wrappers in place. I've successfully got 1 to 3 argument…
Fenster34
  • 438
  • 5
  • 11