MykelXIII
MykelXIII

Reputation: 1135

Floating point assembly on intel processor

I have been studying how floating point operations are performed on a 32 bit intel machine. I have disassembled the following lines of C code to obtain how the compiler translates these lines on assembly.

a = 13;
b = 5;
d = (float) a / (float) b;

And here is the disassembled version of the code shown above:

mov    DWORD PTR [ebp-0x10],0xd
mov    DWORD PTR [ebp-0x14],0x5
fild   DWORD PTR [ebp-0x10]
fild   DWORD PTR [ebp-0x14]
fdivrp st(1),st
fstp   DWORD PTR [ebp-0x18]

What I find confusing is the fdivrp and fstp instructions. From what I read the code above will store the result of the floating point division on the st(1) register, and then pop the top of stack making st(1) the top and not st(0). However the next fstp instruction stores the contents of st(0) to the memory location pointed by the address ebp-0x18 and then pops the stack making st(1) the top. I believe I have misunderstood the documents on how these instructions operate as my understanding will not store the result in memory. I would be grateful if someone can please explain how exactly these 2 instructions operate.

Upvotes: 3

Views: 948

Answers (1)

Jester
Jester

Reputation: 58762

Floating point registers are always addressed relative to the current stack top. Thus, st(0) is always the stack top. The fdivrp st(1), st places the result in st(1) and then immediately pops st(0) so the stack only holds a single item, which is the new st(0) containing the result. The fstp writes it to memory and removes it from the fpu stack, leaving it empty.

Upvotes: 4

Related Questions