纳斯姆浮法召集公约 [英] Calling Convention of Floats in Nasm

查看:82
本文介绍了纳斯姆浮法召集公约的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,最近我们了解了浮点运算,并提出了一些作为作业的问题.
其中之一是:

So recently we learned about floating point operations and got a few questions as a homework.
One of those is:

写下单精度浮点数的调用约定!".

"Write down the calling convention of single precision floats!".

所以我知道xmm寄存器,并知道以双精度方式,第一个输入将进入xmm0,依此类推.
我在Google上查找了该主题,但找不到答案.如果有人可以帮助我解决这个问题,那就太好了.

So i know about the xmm registers and know that on double precision the first input goes into xmm0 and so on.
I looked up the topic on Google but couldn't find the answer. Would be nice if someone could help me on this question.

推荐答案

每个调用约定我都很熟悉处理单个float的方法,与处理double相同. (并非所有调用约定都对它们进行相同的处理,只是在一个调用约定中没有区别).

Every calling convention I'm familiar with handles single float the same as it does double. (Not that all calling conventions handle them the same, just that within one calling convention there's no diff).

一些(例如i386 System V)在堆栈上传递并返回x87 st(0).

float foo(float x) { return 2.0f*x; }

; NASM implementation for i386 System V, or Windows cdecl
foo:
     fld    dword [esp+4]         ; arg from the stack, above the return address
     fadd   st0                   ; st0 += st0
     ret                          ; return value = st0

某些通过/返回XMM寄存器. (例如,x86-64系统V在xmm0..7中传递了前8个FP args,并且在xmm0中返回了FP返回值.这些XMM寄存器的高字节不能保证为零,但是有标量指令,例如sqrtsd仅在低位元素上起作用.)

Some pass / return in XMM registers. (e.g. x86-64 System V passes the first 8 FP args in xmm0..7, and an FP return value is returned in xmm0. The upper bytes of those XMM registers isn't guaranteed to be zero, but there are scalar instructions like sqrtsd that only operate on the low element.)

; x86-64 System V, and Windows x64
foo:
    addss   xmm0, xmm0    ; xmm0 += xmm0, scalar single-precision
    ret                   ; returns in xmm0

( Windows调用约定相似但不同.请在标记 wiki . (例如Windows x64在xmm3中传递第三个arg,即使它是第一个FP arg,而较早的args是整数).

Windows calling conventions are similar but different. See links to ABI docs in the x86 tag wiki. (e.g. Windows x64 passes the 3rd arg in xmm3, even if it's the first FP arg and the earlier args were integer).

请注意,像printf这样的可变参数的C规则意味着float args被调用方提升为double. 为什么printf()将float提升为double? .单精度没有%转换,只有doublelong double. 如何使用printf打印单精度浮点数 (在asm中).

Note that C rules for variadic functions like printf mean that float args are promoted to double by the caller. Why does printf() promote a float to a double?. There is no % conversion for single-precision, only double or long double. How to print a single-precision float with printf (in asm).

请注意,在调用堆栈上在内存中传递double的32位调用约定中,它们占用的空间是整数/指针/float的两倍.因此,double占用两个堆栈插槽.

Note that in 32-bit calling conventions that pass doubles in memory on the call-stack, they take twice as much space as integers / pointers / floats. So a double takes up two stack slots.

它也可能具有8B对齐要求,导致填充.但由于这些原因,它们仍然不是特殊"的:我认为任何其他具有8B对齐要求的8B对象(例如,具有alignas(8)成员的结构)都将被视为相同.

It may also have an 8B alignment requirement leading to padding. But they're still not "special" because of this: any other 8B object with an 8B alignment requirement (e.g. a struct with an alignas(8) member) would be treated the same, I think.

这篇关于纳斯姆浮法召集公约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆