Retrieve arguments 5 & 6 from a C++ function call in masm proc

58 Views Asked by At

Given:

#include <iostream>

extern "C" int AddAsm(int a, int b, int c, int d, int e, int f);

int main()
{
    std::cout << AddAsm(1, 2, 3, 4, 5, 6);
}

I want to implemet the AddAsm function in x64 masm like this:

.code
    AddAsm proc
        mov rax, 0
        add rax, rcx
        add rax, rdx
        add rax, r8
        add rax, r9

        add rax, qword ptr [rsp + 32]
        add rax, qword ptr [rsp + 40]
        add rax, qword ptr [rsp + 48]
        add rax, qword ptr [rsp + 56]
        ret
    AddAsm endp
end

Now everything is clear for the first 4 parameters, but I can't manage to understand/find a practical example on how the rest of the parameters are put on the stack, what does the rbp and rsp register values actually represent and what are the offsets from one parameter to another.

Can somebody please shed some light with an example, please?

1

There are 1 best solutions below

1
Jacob Krieg On
.code
    AddAsm proc
        mov eax, 0
        add eax, ecx ; a
        add eax, edx ; b
        add eax, r8d ; c
        add eax, r9d ; d
        add eax, dword ptr [rsp + 40] ; e
        add eax, dword ptr [rsp + 48] ; f
        ret
    AddAsm endp
end