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'

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;
}

Edited 4 Years Ago by Enders_Game

This question has already been answered. Start a new discussion instead.