2

We've been given the following function to try and implement in C as part of a CS course. We are programming on x86 Linux.

function(float x, float y, float *z); 

For a function such as example(int x, int y) I understand that the x value resides at [ebp+8] and y at [ebp+12] on the stack, is the same convention used when pushing floats?

We also have to perform some masking and calculations on the float numbers. Do these float numbers behave the same as 32-bit integers just in IEEE-754 format?

5
  • 2
    This depends on the calling convention and variant of the x86 architecture you are programming for. What operating system and specific x86 variant (i.e. 32 bit or 64 bit) are you programming for? Commented Oct 19, 2020 at 11:42
  • 1
    As for the second question (please avoid asking multiple unrelated questions at once), yes, that's the case. Commented Oct 19, 2020 at 11:42
  • 1
    If you do not respond to comments asking for clarification, you are going to have a hard time getting a good answer. Commented Oct 19, 2020 at 11:49
  • The operating system is Ubuntu and we are using the 32-bit architecture Commented Oct 19, 2020 at 11:52
  • 2
    In this case, your understanding is correct. Floats are pushed on the stack the same way as integers or pointers. Commented Oct 19, 2020 at 12:00

1 Answer 1

1

here is a simple function and it's asm code :

function(float x, float y, float *z){ float sum = x + y; float neg = sum - *z; } 

the asm of the above function will be like this:

function: pushl %ebp movl %esp,%ebp subl $8,%esp pushl %ebx flds 8(%ebp) fadds 12(%ebp) fstps -4(%ebp) movl 16(%ebp),%ebx flds -4(%ebp) fsubs (%ebx) fstps -8(%ebp) leal -12(%ebp),%esp popl %ebx leave ret 

as you can see from asm above the reference to ebp+x in this case x will be 8/12/16 to get the parameter from the stack, so as fuz point out it in the comments it is indeed stored on the stack

Sign up to request clarification or add additional context in comments.

3 Comments

This function doesn't have any outputs; *z is only read, not written. This fact is obfuscated in the asm by compiling with optimization disabled, making the asm pointlessly hard to read and not a good example for hand-written code. I'm curious what compiler produced that asm, though; there's no reason for it to use EBX, forcing it to save/restore it. The lea is also totally pointless, setting ESP to the value it already had. I can't repro that with GCC, clang, or ICC on Godbolt; I tried a few versions with -O0 -m32 -mno-sse. godbolt.org/z/b1qGrb shows yours and a useful func
@PeterCordes x86 gcc 1.27 with no optimization
I'm curious why you have such a completely ancient GCC version lying around at all!! That was released in 1988, 3 years before the first release of Linux 0.01! (It does seem to be targeting the same calling convention, presumably i386 System V.) Separate from that, why on earth would you use that instead of gcc -m32 with the default GCC on some distro, or godbolt.org?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.