Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly sending parameters - fastcall

Tags:

c++

assembly

I'm trying to call my asm function from c++ and send two parameters which should be saved in ecx and edx according to wikipedia regarding the fastcall calling convension.

This does however not work. Am i missing something?

Assembly x86

.model flat

.code
_TestFunction proc

    mov eax, ecx
    add eax, edx
    ret

_TestFunction endp
end

C++ Code

#include <iostream>

extern "C" int TestFunction(int a, int b);

int main()
{
    std::cout << "Function returns:" << TestFunction(200,100) << std::endl;
    
    std::cin.get();
    return 0;
}

The function returns 1 and this is the registers:

ECX = 00000000 EDX = 00000001

Build logs:

1>------ Rebuild All started: Project: Tutorial, Configuration: Debug Win32 ------ 1>
Assembling asm.asm... 1> Main.cpp 1> Tutorial.vcxproj -> C:\Users\nilo\documents\visual studio 2012\Projects\Tutorial\Debug\Tutorial.exe

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========


1 Answers

If you really want the __fastcall calling convention in Win32, your code need small changes:

In the assembly file, change

_TestFunction proc
...
_TestFunction endp

to

@TestFunction@8 proc
...
@TestFunction@8 endp

In the C++ file, change

extern "C" int TestFunction(int a, int b);

to

extern "C" int __fastcall TestFunction(int a, int b);
like image 110
rkhb Avatar answered Jun 11 '26 08:06

rkhb