Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What c code would compile to something like `call *%eax`?

Tags:

c

assembly

I'm working with c and assembly and I've seen call *%eax in a few spots. I wanted to write a small c program that would compile to something like this, but I'm stuck.

I was thinking about just writing up some assembly code like in this question: x86 assembly instruction: call *Reg only using AT&T syntax in my case to get a small example with the call in it. However, that wouldn't solve my burning question of what kind of c code compiles to that?

I understand that it is a call to the address that eax is pointing to.

like image 235
hmatt1 Avatar asked Jan 24 '14 03:01

hmatt1


People also ask

What does C code compile to?

C is a compiled language. Its source code is written using any editor of a programmer's choice in the form of a text file, then it has to be compiled into machine code.

Which command is used to compile the C program?

Run the gcc command to compile your C program. The syntax you'll use is gcc filename. c -o filename.exe . This compiles the program and makes it executable.

Is C code compiled to assembly?

The compiler takes the preprocessed file and uses it to generate corresponding assembly code. Assembly code, or assembly language (often abbreviated asm), is a high-level programming language that corresponds programming code with the given architecture's machine code instructions.

Can you call C++ from C?

Accessing C++ Code from Within C SourceIf you declare a C++ function to have C linkage, it can be called from a function compiled by the C compiler. A function declared to have C linkage can use all the features of C++, but its parameters and return type must be accessible from C if you want to call it from C code.


1 Answers

Documentation: http://gcc.gnu.org/onlinedocs/gcc/Local-Reg-Vars.html#Local-Reg-Vars

Try this

#include <stdio.h>

typedef void (*FuncPtr)(void);
void _Func(void){
   printf("Hello");
}

int main(int argc, char *argv[]){
   register FuncPtr func asm ("eax") = _Func;
   func();

   return 0;
}   

And its relative assembly:

    .file   "functorTest.c"
    .section .rdata,"dr"
LC0:
    .ascii "Hello\0"
    .text
.globl __Func
    .def    __Func; .scl    2;  .type   32; .endef
__Func:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    movl    $LC0, (%esp)
    call    _printf
    leave
    ret
    .def    ___main;    .scl    2;  .type   32; .endef
.globl _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    call    ___main
    movl    $__Func, %eax
    call    *%eax       ; see? 
    movl    $0, %eax
    movl    %ebp, %esp
    popl    %ebp
    ret
    .def    _printf;    .scl    2;  .type   32; .endef
like image 53
Aniket Inge Avatar answered Sep 29 '22 16:09

Aniket Inge