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

i doubt if this is valid:

__asm__("number dw 0");

I just tried compiling this and got this error:
I don't know much of assembly language myself, maybe you could post in the Assembly Language forum.

Error: no such instruction: 'number dw 0'

inline assembly is not standard, so it all depends on what compiler you are using. VC++ does not allow dw opcode.

Solution I found eventually. Just make sure compile with -masm=intel (or else look up at&t syntax.).

#include <stdio.h>

int main () {
    int number = 0;
    printf("%i\n", number);
    __asm__("mov eax,%[number]\n"
            "inc eax\n"
            "mov %[number],eax\n"
            : [number] "=g" (number)
            : "g" (number)
            : "eax"
           );
    printf("%i\n", number);
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.