views:

142

answers:

1

Once again, I play with MinGW inline assembly.

#include <stdio.h>

int foobar(int);

int main(){
 int n = 0;
 printf("Number: ");
 scanf("%d", &n);
 printf("\n%d",foobar(n));
 return 0;
}

int foobar(int num){
 int result = 0;
 asm(".intel_syntax noprefix\n");
 asm("mov eax, num\n");
 asm("add eax, 110b\n");
 asm("sub eax, 2\n");
    asm("mov result, eax\n");
 return result;
}

Compile it:

C:\Users\Andre\Codes>gcc asmtest.c -o asmtest -masm=intel

Ouch, there are errors:

C:\Users\Andre\AppData\Local\Temp\ccqny4yb.s: Assembler messages: C:\Users\Andre\AppData\Local\Temp\ccqny4yb.s:53: Error: backward ref to unknown label "110:"

What's wrong here? I think my code is valid already?

+1  A: 
ephemient
Many thanks. That works. Now I wonder, how GCC know that %0 is an alias for result, and %1 is an alias for num. I still don't get it.BTW, what do these 3 lines mean? : "=g" (result) : "g" (num) : "eax");
anta40
@anta40 More details added. But really you should read the whole http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html section of the GCC manual.
ephemient