Pregunta

#include "stdio.h"
void fseek(void *, int, int);
main () {
   FILE* f = fopen("myfile", "rb");
   asm("push 2");
   asm("push 0");
   asm("push f");
   asm("call fseek");
   asm("add esp, 12");
}

gcc -masm=intel call.c

call.c:(.text+0x2c): undefined reference to `f'
call.c:(.text+0x31): undefined reference to `fseek'

I have been trying to use AT/T syntax but got the same result.

¿Fue útil?

Solución

Well you can not write like this, since there is no grantee that symbol f would exist in the generated assembly -- it's merely a symbol in C.

The solution is to use GCC's extended asm syntax. For example, push f could be rewrited into this:

asm volatile ("pushl %0"
               : /* no output operands */
               : "m" (f)
               : /* no clobbered operands */);

As for the function call fseek, I believed your code shall be alright (at least in my experience and on my laptop it works just now). What's your platform info? Do you have glibc or similar things providing the standard libraries of C?
Also Please notice you're using a weird declaration of fseek since it shall at least have a return value according to the C specification.

Just for your information, you may try this style of an indirect call:

asm volatile ("call *%0" 
               : /* no output operands */
               : "r"(fseek)
               : /* no clobbered operands */);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top