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

Why does MSVC not support inline assembly for AMD64 and Itanium targets?

Yesterday I learned that inline assembly (with the __asm keyword) is not supported under Microsoft Visual C++ when compiling for AMD64 and Itanium targets. Is that correct? And if so, does anyone know why they would not support inline assembly for…
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
14
votes
4 answers

How to represent hex value such as FFFFFFBB in x86 assembly programming?

I'm learning about x86 inline assembly programming. I wanted to write mov ecx, FFFFFFBB, however the compiler isn’t recognizing it. How should hex numbers like that be written in inline assembler code?
Arcytoi
  • 163
  • 1
  • 3
  • 8
13
votes
2 answers

Accessing a register without using inline assembly with gcc

I want to read the stack pointer register value without writing inline assembly.The reason I want to do this is because I want to assign the stack pointer register value to an element of an array and I find it cumbersome to access an array using…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
13
votes
1 answer

Inline assembly in Haskell

Can I somehow use inline assembly in Haskell (similar to what GCC does for C)? I want to compare my Haskell code to the reference implementation (ASM) and this seems the most straightforward way. I guess I could just call Haskell from C and use GCC…
akosch
  • 4,326
  • 7
  • 59
  • 80
12
votes
2 answers

GCC Inline Assembly: Jump to label outside block

When using inline assembly under MSVC, one is allowed to jump outside of the assembly block by referencing a label in the C/C++ code, as explained in this MSDN article. Can such thing be done when using inline assembly under GCC? Here's an example…
Vicent Marti
  • 7,203
  • 6
  • 30
  • 34
12
votes
1 answer

Defining Bytes in GCC Inline Assembly in Dev-C++(.ascii in AT&T syntax on Windows)

The code below is just showing a Message Box on the screen. The addresses are hardcoded to facilitate: int main () { asm("xorl %eax, %eax \n" "xorl %ebx, %ebx \n" "xorl %ecx, %ecx \n" "xorl %edx, %edx…
jyz
  • 6,011
  • 3
  • 29
  • 37
12
votes
2 answers

How to write multiline inline assembly code in GCC C++?

This does not look too friendly: __asm("command 1" "command 2" "command 3"); Do I really have to put a doublequote around every line? Also... since multiline string literals do not work in GCC, I could not cheat with that either.
johnny-john
  • 846
  • 2
  • 9
  • 19
12
votes
7 answers

Is it possible to access 32-bit registers in C?

Is it possible to access 32-bit registers in C ? If it is, how ? And if not, then is there any way to embed Assembly code in C ? I`m using the MinGW compiler, by the way. Thanks in advance!
C4theWin
  • 123
  • 1
  • 1
  • 5
12
votes
3 answers

How do you populate an x86 XMM register with 4 identical floats from another XMM register entry?

I'm trying to implement some inline assembler (in C/C++ code) to take advantage of SSE. I'd like to copy and duplicate values (from an XMM register, or from memory) to another XMM register. For example, suppose I have some values {1, 2, 3, 4} in…
jbl
  • 2,710
  • 3
  • 18
  • 13
12
votes
4 answers

Programmatically cause Undefined Instruction exception

I want to cause an ARM Cortex-M3 Undefined Instruction exception for the test of my test fixture. The IAR compiler supports this with inline assembly like this: asm("udf.w #0"); Unfortunately the GNU CC inline assembler does not know this opcode…
harper
  • 13,345
  • 8
  • 56
  • 105
12
votes
2 answers

Syscall from inline asm in x86_64 Linux?

Why does this print garbage instead of exiting my program gracefully? I use system calls this way on BSD, and I wonder what would I need to make it work in Linux. int main(int argc, char **argv) { __asm ("movq $1,%rax; movq $0,%rdi; syscall");…
user14554
12
votes
4 answers

GCC inline assembler, mixing register sizes (x86)

Does anyone know how I can get rid of the following assembler warning? Code is x86, 32 bit: int test (int x) { int y; // do a bit-rotate by 8 on the lower word. leave upper word intact. asm ("rorw $8, %0\n\t": "=q"(y) :"0"(x)); return…
Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
12
votes
6 answers

Using GCC inline assembly with instructions that take immediate values

The problem I'm working on a custom OS for an ARM Cortex-M3 processor. To interact with my kernel, user threads have to generate a SuperVisor Call (SVC) instruction (previously known as SWI, for SoftWare Interrupt). The definition of this…
Kevin Vermeer
  • 2,736
  • 2
  • 27
  • 38
11
votes
2 answers

Adding two numbers

I am trying to familiarise myself with x86 assembly using GCC's inline assembler. I'm trying to add two numbers (a and b) and store the result in c. I have four slightly different attempts, three of which work; the last doesn't produce the expected…
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
11
votes
2 answers

Concrete example of incorrect behavior of an early-clobber affecting a memory operand's addressing mode in GCC inline asm?

Below is excerpted from the GCC manual's Extended Asm docs, on embedding assembly instructions in C using asm keyword: The same problem can occur if one output parameter (a) allows a register constraint and another output parameter (b) allows a…
zzzhhh
  • 319
  • 1
  • 8