Pretty self explanatory code. Why doesn’t it work!
#include <stdio.h>
int main() {
__asm__("number dw 0"); // declare number?
printf("%d",number);
__asm__("mov %eax,number"
"inc %eax"
"mov number,%eax");
printf("%d",number);
return 0;
}
cc ex1.c -o ex1
ex1.c: In function ‘main’:
ex1.c:22:17: error: ‘number’ undeclared (first use in this function)
ex1.c:22:17: note: each undeclared identifier is reported only once for each function it appears in
make: *** [ex1] Error 1
Thanks.
I have a lot of knowledge gaps to fill… the gcc manual was confusing me with regards to inline assembly as was google results for tutorials…
working on an intel i7 processor
Use this syntax, you can access variables declared in
Cfrom the inline assemblyYou can let the compiler load
numberinto theeaxregister for you by specifying the"a"constraint on the inputAnd since x86
incinstruction can operate on memory directly you could reduce it to thisFor more information see gcc documentation:
6.41 Assembler Instructions with C Expression Operands