简体   繁体   中英

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. 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()

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

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

Another is to use a 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.

Other issues include insuring the correct endian of the two, yet very commonly the endians of the float and integer will match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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