Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASM in C gives an error with -std=c99

I'm now willing to compile my project with -std=c99 and I'm facing an error I'm not understanding for the moment. This line :

my_type* td = ({ register kmy_type* arg0 asm("eax"); arg0; });

gives me the following error only in C99 :

warning: ISO C forbids nested functions
error: syntax error before ‘asm’
error: ‘arg0’ undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
warning: ISO C forbids braced-groups within expressions

Any clues are welcome to help me understanding what this means. I didn't write this line and I'm also not sure to understand what is its purpose.

like image 297
claf Avatar asked Dec 14 '22 04:12

claf


1 Answers

The line

my_type* td = ({ register my_type* arg0 asm("eax"); arg0; });

should get a value in the eax register, interpreted as a pointer, into td variable. However, it uses lots of GNU extensions, particularly statement expressions and this use of asm (explicit register allocation). I'd suggest you to switch to -std=gnu99 (or whatever it's called). Otherwise, you might want to play with double underscores (eg. asm -> __asm) or the __extension__ keyword, but I don't know if it'll help in c99 mode.

Edit: I just tried it and simply changing asm to __asm works.

like image 100
jpalecek Avatar answered Dec 25 '22 21:12

jpalecek