Simple addition of single precision floats in x87 FPU

425 views Asked by At

I'm writing a small assembly program which takes two single-precision floating point numbers, adds them together and displays the result. However, I would like to have the result saved in a register for later use. To help me get started, I've compiled a c++ program into an assembly program, and tried to edit that. However, I don't understand the results I get, which are following:

    movl    $0x49742408, %eax
    movl    %eax, 20(%esp)
    movl    $0x49f42405, %eax
    movl    %eax, 24(%esp)
    flds    20(%esp)
    fadds   24(%esp)
    fstps   28(%esp)
    flds    28(%esp)
    fstpl   4(%esp)
    movl    $.LC2, (%esp)
    call    printf
    movl    $0, %eax

First, am I correct when regarding floats, that s means 32-bit and l means 64-bit? Second, why is it popping a 32-bit float value into 28(%esp), only to store it back into the data register stack? Removing these two lines results in a less precise result, which is odd. Lastly, it pops the data register stack again, but this time a 64-bit value. I would like a single precision 32-bit value. However, changing l to s results me in getting 0. Does anybody know what exactly is going on?

1

There are 1 answers

2
Jester On BEST ANSWER
  1. Yes, s is 32 bit (float), l is 64 bit (double).
  2. You can't just remove it of course, you then have to use fstpl 4(%esp). I assume you are looking at unoptimized code.
  3. printf expects arguments as per the C calling convention, so varargs are automatically promoted to double.