How to make change gcc calling convention

Viewed 153

Hi I would like to tell gcc how to call functions, for example:

    __mycall void my_function(arg1) {
        do_something(arg1)
    }
    __mycall:
        move $a0, (the first argument)
        jalr (the function we should call)
        nop

I would like the generated assembly code of:


    my_function(1);

To be


    move $a0, 1
    jalr my_function
    nop

I obviously don't want to implement stdcall or fastcall or something like that, i just explained by an example.

I don't think it should be hard to do that but I don't find any example on how to do that I can write inline assembly and call my function with inline assembly but it is ugly, I will appreciate help (:

1 Answers

You can use fastcall or thiscall function attribute (x86-32)

On x86-32 targets, the fastcall attribute causes the compiler to pass the first argument (if of integral type) in the register ECX and the second argument (if of integral type) in the register EDX. Subsequent and other typed arguments are passed on the stack. The called function pops the arguments off the stack. If the number of arguments is variable all arguments are pushed on the stack.

On x86-32 targets, the thiscall attribute causes the compiler to pass the first argument (if of integral type) in the register ECX. Subsequent and other typed arguments are passed on the stack. The called function pops the arguments off the stack. If the number of arguments is variable all arguments are pushed on the stack. The thiscall attribute is intended for C++ non-static member functions. As a GCC extension, this calling convention can be used for C functions and for static member methods.

Related