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

Can register name be passed into assembly template in GCC inline assembly

unsigned int read_reg(char *reg_name) { unsigned int result; __asm__ __volatile__ ("sw xxx, %0" : : "r"(result)); return result; } The above is a piece of RISC-V code. reg_name is got from CLI, and it's a string, such as "ra" or "x1",…
Wanghz
  • 305
  • 2
  • 12
0
votes
1 answer

inline ARMv7-M assembly code into c using gcc (Load Register literal)

I would like to be able to have LDR literal instructions to load registers. I have: extern uint32_t _vStackBase; extern uint32_t _vStackTop; __asm volatile ( " mov r10, #0x5a5a5a5a \n" " ldr r8,…
mastupristi
  • 1,240
  • 1
  • 13
  • 29
0
votes
1 answer

Is there a non inline asm version of AngelScript?

I recently worked a bit with AngelScript and came across some linker errors when trying to compile in x64. If I try to compile my c++ code in x86 everything works fine. After a bit of research I noticed that inline assembly is not supported anymore…
user13455484
0
votes
0 answers

Program does not segfault in gdb

I have this attempt to switch to a different stack by setting %rbp and %rsp: #include #include void* main_rbp = NULL; void* main_rsp = NULL; int run_stack() { int one = 1; printf("Hello from my new stack!\n"); //…
ssmid
  • 350
  • 2
  • 12
0
votes
0 answers

ARM Inline assembly for C code not executing

I am trying to use inline assembly for an ARM C code. My 'main.c' code looks something like this: ... void jump() { __asm { B 0x15000 } } INT32 main(void) { ... Write val1 into register 1 jump(); Write val2 into register 2 …
TJ1
  • 7,578
  • 19
  • 76
  • 119
0
votes
1 answer

how to insert inline assembly instruction by using llvm pass

I tried to insert an assembly instruction into each base block using pass in the IR Pass of LLVM. Update: LLVMContext *Ctx = nullptr; Ctx = &M.getContext(); BasicBlock::iterator IP = BB.getFirstInsertionPt(); IRBuilder<> IRB(&(*IP)); StringRef…
gazile
  • 105
  • 6
0
votes
0 answers

far return gcc cross-compiler

I have a C function in my custom kernel that prints DEBUG on the top left corner. I call that function using a different cs : asm("lcall $0x28, $0x0"); (The code is copied at the beggining of the 0x28 code segment). In the c function that prints…
Leosa99 _
  • 33
  • 6
0
votes
0 answers

how to "nop ptr[eax + 0x00]" on C++ _asm inline

i have this code in asm: cmp ecx,46DC0E00 je 1A380021 nop dword ptr [eax+00] mov [edx+00000584],ecx mov [edx+00000584],00000000 jmp game.exe+A42067 mov [edx+00000584],ecx jmp game.exe+A42067 and i have this code on C++: __asm { cmp ecx,…
0
votes
0 answers

"asm volatile ("" ::: "memory");" does not work in c++

I was trying to follow the queue implementation in this blog that uses a memory barries like below: asm volatile ("" ::: "memory"); But the same is not working for me and throwing this error: error C2589: ':': illegal token on right side of…
Sayantan Ghosh
  • 998
  • 2
  • 9
  • 29
0
votes
2 answers

assembly - Issue with permutations generation

I'm trying to list all the permutations of the first N natural numbers (N <= 6) with MSVC Assembly 8086 inline. I can't change anything of the C program, I can only manage the assembly code. So this is what I've done (and works with N <= 3), using…
maicol07
  • 199
  • 1
  • 6
  • 16
0
votes
3 answers

Why doesn't this MSVC asm block have a ret, or the non-void function have a return?

I'm learning about using inline assembly inside the C++ code. Here is the very simple example: // Power2_inline_asm.c // compile with: /EHsc // processor: x86 #include int power2( int num, int power ); int main( void ) { printf_s(…
derek911
  • 19
  • 5
0
votes
0 answers

What is the most efficient way to pass a local variable address to extended inline ASM for ARM-v7?

Consider this code snippet: __asm volatile ( " MOVW R0, #0x0000 \n\t" " MOVT R0, #0x3004 \n\t" " LDRSH R1, [R0, #12] \n\t" ⋮ ) This is the hard-coded way for loading what's stored…
Hansel
  • 233
  • 1
  • 9
0
votes
1 answer

Homemade kernel Inline assembly gives bad register name error. C

I am coding a custom kernel (some of the work comes from https://www.codeproject.com/Articles/664165/Writing-a-boot-loader-in-Assembly-and-C-Part). Here is the error test.c: Assembler messages: test.c:12: Error: bad register name `%dil' ld: cannot…
TechTheGuy
  • 331
  • 1
  • 11
0
votes
1 answer

inline assembly function pointer calling

minimalist example: void someFunction(){ std::cout << "Hello World" << std::endl; } void (*functionPointer)(); functionPointer = someFunction; int main(){ __asm__("call *%P0"::"m"(functionPointer):); return 0; } In gcc 10.3.0, this results…
mingcr
  • 3
  • 6
0
votes
1 answer

How can use asm() with a const char* instead of a string literal?

If I'd do that: const char *str = "some assembly instructions"; asm(str); CLion'd say "Expected string literal in 'asm'"
Sivyziom
  • 1
  • 1