Can't execute inline assembler with the instructions in a string variable
The instructions must be in a form of a string literal(actual written string, the name of a char array is a pointer btw).
Other than that you got the general idea:)
#include <stdio.h>
int main(int argc, char ** argv){
char a[20] = "nice try:)";
char * dst;
asm("mov %[dst], %[src]\n\t"
: [dst]"=r" (dst) : [src]"r"(a));
printf("%s\n", dst);
return 0;
}
and a useful link:
https://dmalcolm.fedorapeople.org/gcc/2015-08-31/rst-experiment/how-to-use-inline-assembly-language-in-c-code.html
Can I use asm function with a c - string variable instead of a string literal as an argument?
As far as I know asm code fragment are inlined during compilation phase, not run-time. Which means you can't do what you are doing.
As pmg suggested, you could do it by calling external commands, but it would mean user would need to have all the tools installed.
How to use a String in C++ Inline Assembly code?
The Visual C++ compiler allows you to use variables directly in assembly code. Example from here: http://msdn.microsoft.com/en-us/library/y8b57x4b(v=vs.80).aspx
// InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp
// processor: x86
#include <stdio.h>
char format[] = "%s %s\n";
char hello[] = "Hello";
char world[] = "world";
int main( void )
{
__asm
{
mov eax, offset world
push eax
mov eax, offset hello
push eax
mov eax, offset format
push eax
call printf
//clean up the stack so that main can exit cleanly
//use the unused register ebx to do the cleanup
pop ebx
pop ebx
pop ebx
}
}
It doesn't get any easier than this, IMO. You get all the speed, without all the hassle of trying to find out where variables are stored.
How to inline assembly code using bits (hard code) in C?
You can put raw bytes in with .byte
:
__asm__ (".byte 0xf0, 0x0b, 0xaa");
Add variable in assembly instruction (asm inline C example)
You can read something like this and this
static inline void cpuid(int code, uint32_t *a, uint32_t *d) {
asm volatile("cpuid":"=a"(*a),"=d"(*d):"a"(code):"ecx","ebx");
}
/** issue a complete request, storing general registers output as a string
*/
static inline int cpuid_string(int code, uint32_t where[4]) {
asm volatile("cpuid":"=a"(*where),"=b"(*(where+1)),
"=c"(*(where+2)),"=d"(*(where+3)):"a"(code));
return (int)where[0];
}
Related Topics
Remove Duplicates from a List<Int>
Std::Filesystem' Has Not Been Declared After Including <Experimental/Filesystem>
Boost::Spirit::Qi Parser: Index of Parsed Element
Inheriting and Overriding Functions of a Std::String
How to Convert a Time into Epoch Time
Compiling Code Containing Dynamic Parallelism Fails
What Are These Seemingly-Useless Callq Instructions in My X86 Object Files For
Is It Legal to Write to Std::String
Equality-Compare Std::Weak_Ptr
String-Interning at Compiletime for Profiling
How to Iterate Through Every Possible Combination of N Playing Cards
Why Are Pointers to a Reference Illegal in C++
Error Redeclaring a for Loop Variable Within the Loop
Why Do I Get a Template Error If I Name My Function 'Swap', But 'Swap' Is Okay
Is Substitution Performed on a Variadic Parameter Pack Type If the Pack Is Empty