简体   繁体   English

仅使用整数寄存器将浮点值从 C 程序传递到汇编程序级程序?

[英]Passing float value from C program to assembler level program using only integer registers?

For my class we are writing a simple asm program (with C and AT&T x86-64) that prints all the bits of an integer or float.对于我的课程,我们正在编写一个简单的 asm 程序(使用 C 和 AT&T x86-64)来打印整数或浮点数的所有位。 I have the integer part working fine.我的整数部分工作正常。 For the float part my professor has instructed us to pass the float value only using integer registers.对于浮点部分,我的教授指示我们仅使用整数寄存器传递浮点值。 Not too sure why we're not allowed to use float registers.不太清楚为什么我们不允许使用浮点寄存器。 Regardless, does anyone have ideas on how to go about this?无论如何,有没有人有关于如何解决这个问题的想法?

my professor has instructed us to pass the float value only using integer registers.我的教授指示我们只使用整数寄存器传递浮点值。

A simple approach is to copy the float into an integer using memcpy()一种简单的方法是使用memcpy()float复制为整数

float f = ...;
assert(sizeof f == sizeof(uint32_t));

uint32_t u;
memcpy(&u, &f, sizeof u);
foo(u);

Another is to use a union .另一个是使用union Perhaps using a compound literal .也许使用复合文字

void foo (uint32_t);

int main() {
  float f;
  assert(sizeof f == sizeof(uint32_t));

  //  v----------- compound literal -----------v
  foo((union { float f; uint32_t u; }) { .f = f}.u);
  //   ^------ union object ------- ^
}

Both require that the integer type used and the float are the same size.两者都要求使用的整数类型和float大小相同。

Other issues include insuring the correct endian of the two, yet very commonly the endians of the float and integer will match.其他问题包括确保两者的字节序正确,但通常float和整数的字节序会匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM