How to call a function through a function pointer passed as an argument?

How to call the passed function (* f2) in the third argument of the function f1 in the assembly? The declaration looks like this:

extern float f1(int v1, float v2, float (*f2)(int v3, float v4));

I want to pass v1 to v3, v2 to v4, call function f2 and return the value

f1:
    push rbp           
    mov rbp, rsp

    mov rdx, rdi ; v1 to v3
    mov xmm1, xmm0 ; v2 to v4
    call ??? 
    mov xmm0, xmm1

    mov rsp, rbp       
    pop rbp    
ret

What did I put instead of question marks?

+4
source share
2 answers

, "Abi64". MASM, , Windows, , , "64" , 64- , . , 64- Windows. - __vectorcall, - Microsoft x64 (, , , hellip;).

Microsoft x64 , __vectorcall , , . . , , f1 f2, . f1 - , f2, f2 - f1. :

f1:
    rex_jmp  r8    ; third parameter (pointer to f2) is passed in r8

, , . v1 v2, , :

f1:
    inc      ecx        ; increment v1 (passed in ecx)

    ; multiply v2 (xmm1) by v1 (ecx)
    movd     xmm0, ecx
    cvtdq2ps xmm0, xmm0
    mulss    xmm1, xmm0

    rex_jmp  r8    ; third parameter (pointer to f2) is passed in r8

- , :

f1:
    sub   rsp, 40     ; allocate the required space on the stack
    call  r8          ; call f2 through the pointer, passed in r8
    add   rsp, 40     ; clean up the stack
    ret

, /, , , .

, , ! Microsoft x64 RCX, RDX, R8 R9. . XMM0, XMM1, XMM2 XMM3. , .

, "", 4 , fp. :

╔═══════════╦══════════════════════════╗
β•‘           β•‘           TYPE           β•‘
β•‘ PARAMETER ╠═════════╦════════════════╣
β•‘           β•‘ Integer β•‘ Floating-Point β•‘
╠═══════════╬═════════╬════════════════╣
β•‘ First     β•‘   RCX   β•‘      XMM0      β•‘
╠═══════════╬═════════╬════════════════╣
β•‘ Second    β•‘   RDX   β•‘      XMM1      β•‘
╠═══════════╬═════════╬════════════════╣
β•‘ Third     β•‘   R8    β•‘      XMM2      β•‘
╠═══════════╬═════════╬════════════════╣
β•‘ Fourth    β•‘   R9    β•‘      XMM3      β•‘
╠═══════════╬═════════╩════════════════╣
β•‘ (rest)    β•‘         on stack         β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

, . XMM0, , XMM1, , , "". ( x86-64 System V ABI, 6 args , FP args).

Windows , .

+5

.

, , , Windows Intel I7: -

C : -

extern float f1(int v1, float v2, float (*f2)(int v3, float v4))
{
    float a = 10.0;
    int b = 12;

    f2(b, a);
    return a+ 12.5;
}

: -

_f1:
 pushl  %ebp
 movl   %esp, %ebp
 subl   $40, %esp
 movl   LC0, %eax

 movl   %eax, -12(%ebp)
 movl   $12, -16(%ebp)
 movl   -12(%ebp), %eax
 movl   %eax, 4(%esp)
 movl   -16(%ebp), %eax
 movl   %eax, (%esp)
 movl   16(%ebp), %eax
 call   *%eax
 fstp   %st(0)
 flds   -12(%ebp)
 flds   LC1
 faddp  %st, %st(1)
 leave
ret

, .

-2

Source: https://habr.com/ru/post/1662949/


All Articles