can this FPU assembly code be optimised

Just starting out? Need help? Post your questions and find answers here.
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

can this FPU assembly code be optimised

Post by michaeled314 »

Code: Select all

a.d = 3

FLD a
FLDPI
FADD st0,st1
FST a

Debug a
thanks assembly is amazing and I want to see if this can be optimized even more
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

michaeled314 try to leave FPU stack as you found it, otherwise you may run into problems, your example corrected follows.

Code: Select all

a.d = 3 

FLD qword [v_a] ;st0 but will be st1 after the instruction FLDPI
FLDPI ;st0
FADDP st1,st0
FSTP qword [v_a] ;store and pop st0 in a emptying the FPU stack

Debug a
Last edited by jack on Fri Apr 10, 2009 8:42 pm, edited 1 time in total.
dioxin
User
User
Posts: 97
Joined: Thu May 11, 2006 9:53 pm

Post by dioxin »

You can add the number without loading it from memory first:

Code: Select all

a.d = 3 

FLDPI   'get pi
FADD a  'add in the variable direct from memory
FSTP a  'store the result and clear up the stack

Debug a
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Post by michaeled314 »

Thanks I think assembly is amazing and I am always looking for ways to optimize my code.
dioxin
User
User
Posts: 97
Joined: Thu May 11, 2006 9:53 pm

Post by dioxin »

If you're going to take ASM programming seriously then you need to know what instructions are available and what they do.
Get the manuals from intel, they're free downloads:
http://www.intel.com/products/processor/manuals/
Volumes 2A and 2B are the ones you need most of the time as they describe all the opcodes in detail but the other volumes are also of use.


Then, for optimisation, try reading the AMD Athlon optimization manual:
http://support.amd.com/us/Processor_TechDocs/22007.pdf

I'm sure Intel will have an equivalent but they'll contain a lot of the same information.
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Post by michaeled314 »

I already have the intel manuals but I am taking a look at the other one
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Post by michaeled314 »

also I want to see the FPU stack what do I do
dioxin
User
User
Posts: 97
Joined: Thu May 11, 2006 9:53 pm

Post by dioxin »

also I want to see the FPU stack what do I do
Get Ollydbg from here:
http://www.ollydbg.de/

Once installed, you place a !INT 3 opcode just ahead of the code which is of interest and the debugger will start and allow you to single step while showing you the stack and all registers and a lot more.
Post Reply