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
7
votes
0 answers

Remove superfluous `andi ..., 0xff` instruction from inline assembly when outputting an `i8`

I have the following function using inline assembly, targeting mipsel-unknown-linux-gnu: #![feature(asm)] #[no_mangle] pub unsafe extern "C" fn f(ptr: u32) { let value: i8; asm!( "lb $v0, ($a0)", in("$4") ptr, …
Filipe Rodrigues
  • 1,843
  • 2
  • 12
  • 21
7
votes
2 answers

Data Memory Barrier (DMB) in CMSIS libraries for Cortex-M3s

In the CMSIS definitions for gcc you can find something like this: static __INLINE void __DMB(void) { __ASM volatile ("dmb"); } My question is: what use does a memory barrier have if it does not declare "memory" in the clobber list? Is it an error…
jpc
  • 260
  • 2
  • 10
7
votes
2 answers

FreeBSD syscall clobbering more registers than Linux? Inline asm different behaviour between optimization levels

Recently I was playing with freebsd system calls I had no problem for i386 part since its well documented at here. But i can't find same document for x86_64. I saw people are using same way like on linux but they use just assembly not c. I suppose…
fsdfhdsjkhfjkds
  • 308
  • 1
  • 9
7
votes
0 answers

Alternative to mixing c code with naked function

So currently clang, msvc and the new llvm backed intel compiler do not support mixing c in a naked function. Example __declspec(naked) void func() { int j; _asm(add j, 1) } The compiler i've been using supports this, but it's been…
Infowarrior
  • 309
  • 1
  • 5
7
votes
1 answer

Optimal implementation of iterative Kahan summation

Intro Kahan summation / compensated summation is technique that addresses compilers´ inability to respect the associative property of numbers. Truncation errors results in (a+b)+c not being exactly equal to a+(b+c) and thus accumulate an undesired…
Egeris
  • 95
  • 8
7
votes
1 answer

Understanding Inline assembly in a pre-processor macro vs Inline assembly in a function

GGC's inline assembly can be difficult to implement properly and easy to get wrong1. From a higher level perspective inline assembly has some rules that have to be considered outside of what instructions an inline assembly statement may emit. The…
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
7
votes
2 answers

Is it possible to use explicit register variables in GCC with C++17?

I am using explicit register variables to pass parameters to a raw Linux syscall using registers that don't have machine-specific constraints (such as r8, r9, r10 on x86_64) as suggested here. #include #ifdef __i386__ #define…
r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
7
votes
1 answer

Unable to include ASM header file in C without losing preprocessor

Short version: I want to be able to define assembler macros in a macros.S and use them from inside asm() statements in GNU C. I can do this with asm(".include \"macros.S\""); near the top of my C source, but I want macros.S to go through the C…
Mania
  • 1,718
  • 14
  • 16
7
votes
3 answers

How can I accurately benchmark unaligned access speed on x86_64?

In an answer, I've stated that unaligned access has almost the same speed as aligned access a long time (on x86/x86_64). I didn't have any numbers to back up this statement, so I've created a benchmark for it. Do you see any flaws in this benchmark?…
geza
  • 28,403
  • 6
  • 61
  • 135
7
votes
2 answers

how to force the use of cmov in gcc and VS

I have this simple binary search member function, where lastIndex, nIter and xi are class members: uint32 scalar(float z) const { uint32 lo = 0; uint32 hi = lastIndex; uint32 n = nIter; while (n--) { int mid = (hi + lo) >>…
Fabio
  • 2,105
  • 16
  • 26
7
votes
2 answers

What does the declaration“extern struct cpu *cpu asm("%gs:0");” mean?

When I'm reading the xv6 source code, I'm confused about the syntax of the declaration below. Can anyone explain it to me? extern struct cpu *cpu asm("%gs:0");
xiaoma
  • 73
  • 4
7
votes
2 answers

Is it possible to put assembly instructions into CUDA code?

I want to use assembly code in CUDA C code in order to reduce expensive executions as we do using asm in c programming. Is it possible?
superscalar
  • 635
  • 2
  • 9
  • 15
7
votes
1 answer

x64 inline assembly in c to align instructions

I've got a very hot instruction loop which needs to be properly aligned on 32-bytes boundaries to maximize Intel's Instruction Fetcher effectiveness. This issue is specific to Intel not-too-old line of CPU (from Sandy Bridge onward). Failure to…
Cyan
  • 13,248
  • 8
  • 43
  • 78
7
votes
3 answers

Looping over arrays with inline assembly

When looping over an array with inline assembly should I use the register modifier "r" or he memory modifier "m"? Let's consider an example which adds two float arrays x, and y and writes the results to z. Normally I would use intrinsics to do this…
Z boson
  • 32,619
  • 11
  • 123
  • 226
7
votes
1 answer

Changing Array Values In Function - Inline Assembly

So I taught myself x86 Assembly a while back and was just playing around with inline Assembly in C++. So what I want to do is in a functions parameters, pass in an array, an index ( unsigned int ) and a number. With assembly it will then change the…
CMilby
  • 624
  • 1
  • 6
  • 23