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
8
votes
3 answers

Using condition flags as GNU C inline asm outputs

I'm working on some code where it would be highly desirable to take condition-flags output from an inline asm block and use that as a condition to branch on in the calling C code. I don't want to store the flags (that would be useless and…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
8
votes
1 answer

gcc intrinsic vs inline assembly : which is better?

If I want to expose a single machine specific instruction to the programmer, there are two ways I can do so : Define a new builtin / intrinsic Expose the same as inline assembly asm() [As its a single arithmetic type instruction, I believe there…
Cherry Vanc
  • 781
  • 1
  • 4
  • 19
8
votes
1 answer

Gcc inline assembly what does "'asm' operand has impossible constraints" mean?

I have this below code within function: void makeSystemCall(uint32_t num, uint32_t param1, uint32_t param2, uint32_t param3){ asm volatile ( "mov %0, %%eax\n\t"//Move num to eax "mov %1, %%ebx\n\t"//Move param1 to ebx …
The amateur programmer
  • 1,238
  • 3
  • 18
  • 38
8
votes
2 answers

Calling assembly in GCC?

#include static inline uint xchg(volatile unsigned int *addr, unsigned int newval) { uint result; asm volatile("lock; xchgl %0, %1" : "+m" (*addr), "=a" (result) : "1" (newval) : "cc"); return result; } Can some one tell me…
rbr200
  • 81
  • 1
8
votes
1 answer

What does '__asm__(".previous");' mean?

While trying to compile my project, that uses some third party headers, with mingw 4.4, I encountered the following error: Assembler messages: Error: junk at end of line, first unrecognized character is '"' Error: unknown pseudo-op:…
Maxwin
  • 383
  • 3
  • 17
8
votes
1 answer

Why is this inline assembly calling release, retain, and autorelease in libobjc?

The snippet below is taken from Apple's ObjC runtime (libobjc) source code. I wonder what this means exactly. (Not very google-able, sorry) // HACK -- the use of these functions must be after the @implementation id bypass_msgSend_retain(NSObject…
mojuba
  • 11,842
  • 9
  • 51
  • 72
8
votes
3 answers

How do I pass arguments to C++ functions when I call them from inline assembly

So, I would like to be able to call functions from a c++ dll. For certain reasons, I would like to call them from an __asm block in my C++ code. My question is this: I know that before I call the function, I have to push its arguments on the stack…
Emil D
  • 1,864
  • 4
  • 23
  • 40
8
votes
3 answers

Is inline asm part of the ANSI C standard?

I always thought it was but many IDEs and syntax highlighting tools do not highlight ASM in C, but they always do with C++. Is inline assembly part of the C standard (ANSII or ISO) or not?
8
votes
2 answers

GCC extended asm, struct element offset encoding

I am trying to write a small piece of my code in GCC style extended asm (x86-64 target) and am having trouble encoding struct offsets. I have a struct s with a member size_t a[], a pointer to such a struct and an index both of which are generated…
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
8
votes
4 answers

What is this x86 inline assembly doing?

I came across this code and need to understand what it is doing. It just seems to be declaring two bytes and then doing nothing... uint64_t x; __asm__ __volatile__ (".byte 0x0f, 0x31" : "=A" (x)); Thanks!
MK.
  • 3,907
  • 5
  • 34
  • 46
8
votes
5 answers

How to make a C program that can run x86 hex codes

I have an array of hex codes that translate into assembly instructions and I want to create program in C that can execute these. unsigned char rawData[5356] = { 0x4C, 0x01, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x0C, 0x00, 0x00, 0x3D,…
Iowa15
  • 3,027
  • 6
  • 28
  • 35
8
votes
3 answers

Using C++ with assembly to allocate and create new functions at runtime

I've been working on a (C++) project, which requires completely dynamically allocated functions, which means malloc/new and mprotect and then modify the buffer manually to assembly code. Because of this I've wondered exactly, what is required in…
Elliott Darfink
  • 1,153
  • 14
  • 34
7
votes
3 answers

Visual C++ x64 add with carry

Since there doesn't seem to be an intrinsic for ADC and I can't use inline assembler for x64 architecture with Visual C++, what should I do if I want to write a function using add with carry but include it in a C++ namespace? (Emulating with…
jnm2
  • 7,960
  • 5
  • 61
  • 99
7
votes
1 answer

Use both SSE2 intrinsics and gcc inline assembler

I have tried to mix SSE2 intrinsics and inline assembler in gcc. But if I specify a variable as xmm0/register as input then in some cases I get a compiler error. Example: #include int main() { __m128i test = _mm_setzero_si128(); …
Jens Schwarzer
  • 2,840
  • 1
  • 22
  • 35
7
votes
2 answers

how to write inline assembly codes about LOOP in Xcode LLVM?

I'm studying about inline assembly. I want to write a simple routine in iPhone under Xcode 4 LLVM 3.0 Compiler. I succeed write basic inline assembly codes. example : int sub(int a, int b) { int c; asm ("sub %0, %1, %2" : "=r" (c) : "r" (a),…
이영보
  • 71
  • 1
  • 3