Reverse Engineering for Beginners

(avery) #1

CHAPTER 17. FLOATING-POINT UNIT CHAPTER 17. FLOATING-POINT UNIT


Next:FADDPwas executed, now the result of the addition is inST(0), andST(1)is cleared:


Figure 17.5:OllyDbg:FADDPexecuted

The result is left inST(0), because the function returns its value inST(0). main()takes this value from the register
later.


We also see something unusual: the 13.93...value is now located inST(7). Why?


As we have read some time before in this book, theFPUregisters are a stack:17.2 on page 205. But this is a simplification.
Imagine if it was implementedin hardwareas it’s described, then all 7 register’s contents must be moved (or copied) to
adjacent registers during pushing and popping, and that’s a lot of work. In reality, theFPUhas just 8 registers and a pointer
(calledTOP) which contains a register number, which is the current “top of stack”. When a value is pushed to the stack,TOP
is pointed to the next available register, and then a value is written to that register. The procedure is reversed if a value is
popped, however, the register which was freed is not cleared (it could possibly be cleared, but this is more work which can
degrade performance). So that’s what we see here. It can be said thatFADDPsaved the sum in the stack, and then popped
one element. But in fact, this instruction saved the sum and then shiftedTOP. More precisely, the registers of theFPUare
a circular buffer.


GCC


GCC 4.4.1 (with-O3option) emits the same code, just slightly different:


Listing 17.2: Optimizing GCC 4.4.1
public f
f proc near


arg_0 = qword ptr 8
arg_8 = qword ptr 10h


push ebp
fld ds:dbl_8048608 ; 3.14

; stack state now: ST(0) = 3.14

Free download pdf