Manipulating variable c with inline assembly
Possible duplicate:
How to access the c variable to handle inline assemblies
Given this code:
#include <stdio.h>
int main(int argc, char **argv)
{
int x = 1;
printf("Hello x = %d\n", x);
}
I would like to access and manipulate the x variable in an inline assembly. Ideally I want to change its value using inline assembly. GNU and use AT & T syntax. Suppose I want to change the value of x to 11, right after the printf statement, how would I do that?
+3
source to share
1 answer
The function asm()
follows this order:
asm ( "assembly code"
: output operands /* optional */
: input operands /* optional */
: list of clobbered registers /* optional */
);
and put 11 in x using assembly via your c code:
int main()
{
int x = 1;
asm ("movl %1, %%eax;"
"movl %%eax, %0;"
:"=r"(x) /* x is output operand and it related to %0 */
:"r"(11) /* 11 is input operand and it related to %1 */
:"%eax"); /* %eax is clobbered register */
printf("Hello x = %d\n", x);
}
You can simplify the above asm code by avoiding the registered case
asm ("movl %1, %0;"
:"=r"(x) /* related to %0*/
:"r"(11) /* related to %1*/
:);
You can simplify by avoiding the input operand and using the local constant value from asm instead of c:
asm ("movl $11, %0;" /* $11 is the value 11 to assign to %0 (related to x)*/
:"=r"(x) /* %0 is related x */
:
:);
Another example: compare 2 numbers with assembly
+4
source to share