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
0
votes
1 answer

Error: invalid operands (*UND* and *UND* sections) for `%'

This cppreference example fails with Error: invalid operands (*UND* and *UND* sections) for %'` on g++ 11.1.0 on my Archlinux box, but not on coliru. Why? The file name is _1h6.cpp: $ g++ _1h6.cpp -S && as _1h6.s # same error as g++ _1h6.cpp _1h6.s:…
Roland Puntaier
  • 3,250
  • 30
  • 35
0
votes
0 answers

unresolved external symbol asm in cython

Basically, I have a cython code where i want to call C's inline assembly. I tried below: cdef extern from *: """ #include void print_endln(){ asm("nop"); } """ void print_endln() print_endln() But I get…
hashy
  • 175
  • 10
0
votes
2 answers

C Inline Assembly Unordered?

In C I have: __asm__ ( "mov $0x2,%rax;" "mov $0x6000dd,%rdi;" "mov $0x0,%rsi;" "syscall;" ); But when I comppile it in the assembly file I see: # 12 "mine.cxx" 1 mov …
user16255957
0
votes
1 answer

Clang errors "expected register" with inline x86 assembly (works with GCC)

I wrote a demo with some inline assembly (showing how to shift an array of memory right one bit) and it compiles and functions fine in GCC. However, the with Clang, I'm not sure if it's generating bad code or what but it's unhappy that I'm using…
Gravis
  • 992
  • 11
  • 17
0
votes
3 answers

Expected ')' before token inline assembly error

I would like to learn some inline assembly programming, but my first cod snippet does not work. I have a string and I would like to assign the value of the string to the rsi register. Here is my code: string s = "Hello world"; const char…
0
votes
0 answers

why i can't get correct value by cmpxchg instruction?

int cas(int exchange_value,volatile int *dest,int compare_value){ __asm__ volatile("lock cmpxchgl %1,(%3)" :"=a"(exchange_value) :"r"(exchange_value),"a"(compare_value),"r"(dest) :"memory"); return…
0
votes
1 answer

Assembly code in C break instruction to get current time

I need to write inline assembly code in C in format like this: asm( "mov %1, %%ax\n\t" "inc %%ax\n" "mov %%ax, %0" :"=r" (a) :"r" (a) ); This code in asm() need to do break and assign current time to variables declared in C…
0
votes
0 answers

Swapping 2 integers in an array with inline assembly results in weird memory values

int Perm[4326]; __asm { MOV EBX, 6 FACT: MOV Perm[4320 + EBX * 4], EBX DEC EBX TEST EBX, EBX JE FACTEND JMP…
JustJohn
  • 59
  • 1
  • 7
0
votes
1 answer

Assembler code in C adding 1 to input and to power x times

I have to write some asm("code") in C using CodeBlocks. I need to increment input value 'a' by one and then power the result like this a * a * a. Where incrementing and power value a*a is easy, the another power powers the result of previous power.…
0
votes
0 answers

How does inline assembly skip directly to the end of a recursive function?

HPSTART: CMP EBX, 1 // if n = 1 JNE HPERMUT MOV EDX, N INSERT: MOV ECX, Perm[4320 + EDX * 4] MOV Perm[EDI * 4], ECX INC EDI DEC EDX TEST EDX, EDX JNZ…
0
votes
0 answers

How do I exit the inline assembler?

Suppose I have a bit of _asm code that is like the following: // Unimportant calculations that end in one of the following Labels OPTIONA : MOVE EAX, EDX JMP END OPTIONB : MOVE EBX, EDX JMP END OPTIONC : MOVE ECX, EDX …
JustJohn
  • 59
  • 1
  • 7
0
votes
2 answers

Why can `asm volatile("" ::: "memory")` serve as a compiler barrier?

It is known that asm volatile ("" ::: "memory") can serve as a compiler barrier to prevent compiler from reordering assembly instructions across it. For example, it is mentioned in https://preshing.com/20120625/memory-ordering-at-compile-time/,…
zzzhhh
  • 319
  • 1
  • 8
0
votes
0 answers

Visual Studio unhandled exception only when using breakpoints in certain lines

LEA ECX, Perm[3000] START: MOV EAX, N MOV EBX, EAX MOV Perm[3000 + EBX * 4], EBX FACT : DEC EBX MOV Perm[3000 + EBX * 4], EBX TEST EBX, EBX JZ FACTEND …
JustJohn
  • 59
  • 1
  • 7
0
votes
2 answers

State of EFLAGS

Over the past few days I've been struggling with a weird behaviour trying to get the states of EFLAGS. To accomplish this I've written this code: #include int flags_state() { int flags = 0; __asm__ __volatile__("pushfq"); __asm__…
Bruno Criado
  • 120
  • 7
0
votes
0 answers

inline assembly block with multiple outputs

How does one specify multiple outputs with an inline asm statement using gcc? I don't follow how the garbage value for ret is printed, but I suspect it's possibly related to both syscall and the mov at the top of the inline assembly section both…
user3882729
  • 1,339
  • 8
  • 11