为什么 Windows x64 调用约定需要将 XMM (FP) args 复制到整数寄存器,用于 printf 等可变参数函数?

问题描述

我正在尝试在 Windows 上使用 NASM 组装以下代码。 printf 函数应该采用 xmm0 到 xmm2 作为小数点参数。为什么我必须在 RDX、R8 和 R9 中放置小数参数?新手在这里

global main                     
extern printf
section .data   
    ;Area = pi * r^2            
    pi  dq 3.141592653589793238                 
    radius dq 10.0
    fmt dq "Pi=%.10f  radius=%.10f  area=%.10f",10,0 
section .bss                                                    
section .text                                   
main:
    push    rbp
    mov     rbp,rsp
    sub     rsp,32          ;shadow space
    
    mov     rcx,fmt                    
    movq    xmm0,[pi]
    movq    rdx,xmm0
    
    movq    xmm1,[radius]
    movq    r8,xmm1 

    movq     xmm2,[pi]
    mulsd    xmm2,xmm1; pi*r multiply scalar double
    mulsd    xmm2,xmm1; pi * r^2
    movq     r9,xmm2      
           
    call     printf

    add      rsp,32                
    leave
    ret

Microsoft x64 和 System-V x64 之间的小数参数过程完全不同。出现的问题是将为 Linux 设计的程序移植到 Windows 上的 NASM。该代码在 Jo Van Hoey 所著的“开始 x64 汇编编程”一书中给出。

https://github.com/mabs239/beginning-x64-assembly-programming/blob/master/Chapter%2042/42%20variadic1/variadic1.asm

https://github.com/mabs239/beginning-x64-assembly-programming/blob/master/Chapter%2012/12%20function2/function2.asm

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)