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
17
votes
2 answers

Retq instruction, where does it return

I am unable to understand where the assembly instruction retq returns to. I understand that when my normal code executes then it return to the address specified in the stack. But how does it know where in the stack is the return address located? In…
Kaushal Shah
  • 172
  • 1
  • 1
  • 6
17
votes
8 answers

How to store a C++ variable in a register

I would like some clarification regarding a point about the storage of register variables: Is there a way to ensure that if we have declared a register variable in our code, that it will ONLY be stored in a register? #include using…
ashutosh kumar
  • 183
  • 1
  • 1
  • 4
17
votes
7 answers

gcc removes inline assembler code

It seems like gcc 4.6.2 removes code it considers unused from functions. test.c int main(void) { goto exit; handler: __asm__ __volatile__("jmp 0x0"); exit: return 0; } Disassembly of main() 0x08048404 <+0>: push ebp …
iblue
  • 29,609
  • 19
  • 89
  • 128
16
votes
4 answers

Direct C function call using GCC's inline assembly

If you want to call a C/C++ function from inline assembly, you can do something like this: void callee() {} void caller() { asm("call *%0" : : "r"(callee)); } GCC will then emit code which looks like this: movl $callee, %eax call *%eax This…
mtvec
  • 17,846
  • 5
  • 52
  • 83
16
votes
2 answers

Which inline assembly code is correct for rdtscp?

Disclaimer: Words cannot describe how much I detest AT&T style syntax I have a problem that I hope is caused by register clobbering. If not, I have a much bigger problem. The first version I used was static unsigned long long rdtscp(void) { …
James
  • 9,064
  • 3
  • 31
  • 49
16
votes
2 answers

Calling fsincos instruction in LLVM slower than calling libc sin/cos functions?

I am working on a language that is compiled with LLVM. Just for fun, I wanted to do some microbenchmarks. In one, I run some million sin / cos computations in a loop. In pseudocode, it looks like this: var x: Double = 0.0 for (i <- 0 to 100 000…
Erkki Lindpere
  • 597
  • 5
  • 11
15
votes
1 answer

Is Clang generating incorrect code for inline assembly?

I have some C code: #include "stdio.h" typedef struct num { unsigned long long x; } num; int main(int argc, char **argv) { struct num anum; anum.x = 0; __asm__("movq %%rax, %0\n" : "=m" (anum.x) : "rax"(2)); …
David
  • 487
  • 1
  • 5
  • 18
15
votes
1 answer

How can I indicate that the memory *pointed* to by an inline ASM argument may be used?

Consider the following small function: void foo(int* iptr) { iptr[10] = 1; __asm__ volatile ("nop"::"r"(iptr):); iptr[10] = 2; } Using gcc, this compiles to: foo: nop mov DWORD PTR [rdi+40], 2 ret Note in…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
15
votes
1 answer

What is a clobber?

Clang TargetInfo has a method called getClobbers: Returns a string of target-specific clobbers, in LLVM format. So, what is a clobber?
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
15
votes
6 answers

Code injecting/assembly inlining in Java?

I know Java is a secure language but when matrix calculations are needed, can I try something faster? I am learning __asm{} in C++, Digital-Mars compiler and FASM. I want to do the same in Java. How can I inline assembly codes in functions? Is this…
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
14
votes
1 answer

How to access the control registers cr0,cr2,cr3 from a program? Getting segmentation fault

I have written a program which tries to read from and write to the control registers. The program compiles fine, but when the inline assembly is about to be executed, it produces a segmentation fault. Code: void instructions(int val) { int i; …
Chris
  • 141
  • 1
  • 1
  • 3
14
votes
1 answer

How to get VESA BIOS Information

I'm following through the Phil-Opp Tutorials about writing an OS in Rust, and, after playing around with it a little bit, I want to fiddle with displaying real graphics on the screen. I've figured out that I should probably start out by using the…
Ben Gubler
  • 1,393
  • 3
  • 18
  • 32
14
votes
1 answer

GCC/x86 inline asm: How do you tell gcc that inline assembly section will modify %esp?

While trying to make some old code work again (https://github.com/chaos4ever/chaos/blob/master/libraries/system/system_calls.h#L387, FWIW) I discovered that some of the semantics of gcc seem to have changed in a quite subtle but still dangerous way…
Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
14
votes
4 answers

How to tell GCC to generate 16-bit code for real mode

I am writing real mode function, which should be normal function with stackframes and so, but it should use %sp instead of %esp. Is there some way to do it?
user2443423
  • 175
  • 1
  • 1
  • 9
14
votes
1 answer

clang (LLVM) inline assembly - multiple constraints with useless spills / reloads

clang / gcc : Some inline assembly operands can be satisfied with multiple constraints, e.g., "rm", when an operand can be satisfied with a register or memory location. As an example, the 64 x 64 = 128 bit multiply: __asm__ ("mulq %q3" : "=a" (rl),…
Brett Hale
  • 21,653
  • 2
  • 61
  • 90