Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


floating point arithmetic operations

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking
View previous topic :: View next topic  
Author Message
ciglipaf
How do I cheat?
Reputation: 0

Joined: 26 Jul 2015
Posts: 6

PostPosted: Thu Aug 06, 2015 9:25 am    Post subject: floating point arithmetic operations Reply with quote

i need to calculate distance between my_posx and target_posx in assembly. Can you help me?

pseudo pseudo code:


substract target_posX from my_posX, take square of it, write the result to distanceX²
substract target_posY from my_posY, take square of it, write the result to distanceY²
Sum distanceX² and distanceY², take square root of it, write result to distance
Back to top
View user's profile Send private message
gameplayer
Advanced Cheater
Reputation: 2

Joined: 26 Jun 2011
Posts: 97
Location: Vietnam

PostPosted: Thu Aug 06, 2015 9:38 am    Post subject: Reply with quote

I only know how to use FPU instructions. Use dword ptr if the values are float types. Use qword ptr if the values are double types.
fld dword ptr [target_posX_address] //load a float value into FPU stack
fsub dword ptr [your_posX_address] //substract
fmul st(0) //square of previous result
fstp dword ptr [storage_address]
do the same with the y-coordinator
Back to top
View user's profile Send private message
ciglipaf
How do I cheat?
Reputation: 0

Joined: 26 Jul 2015
Posts: 6

PostPosted: Thu Aug 06, 2015 1:04 pm    Post subject: Reply with quote

Thanks for help.
Actually i dont really know how stacks works. push pop, fst/fstp, fmul/fmulp... As i understand, they work like this. Am i correct?

fld dword ptr [target_posX_address] //load a float value into FPU st(0)
fsub dword ptr [your_posX_address] //substract this value from st(0), write result to st(0)
fmul st(0) //square of previous result. st(0)=st(0)*st(0)
fstp dword ptr [storage_address] //write st(0) to this adress and remove st(0) from the stack. so now, st(1) becomes st(0), st(2) becomes st(0)...
Back to top
View user's profile Send private message
gameplayer
Advanced Cheater
Reputation: 2

Joined: 26 Jun 2011
Posts: 97
Location: Vietnam

PostPosted: Thu Aug 06, 2015 9:13 pm    Post subject: Reply with quote

It's correct.
Back to top
View user's profile Send private message
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Thu Aug 06, 2015 9:28 pm    Post subject: Reply with quote

When in doubt, like so many other things, it works great to just try it and step through w/ the debugger. You can view the FPU stack and watch how it changes w/ each instruction.

Alternatively, most C/C++ compilers can generate commented assembly code. You might try compiling a very simple C program that does the same calculations into ASM and see how it arranges things. This is very useful anytime you need to write ASM and don't know how to proceed.

_________________
A nagy kapu mellett, mindig van egy kis kapu.
----------------------
Come on...
Back to top
View user's profile Send private message
Gniarf
Grandmaster Cheater Supreme
Reputation: 43

Joined: 12 Mar 2012
Posts: 1285

PostPosted: Fri Aug 07, 2015 1:36 am    Post subject: Reply with quote

In reply to the OP: I remember posting that calculation in 3D, it was... there.
_________________
DO NOT PM me if you want help on making/fixing/using a hack.
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 222

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Fri Aug 07, 2015 8:18 am    Post subject: Reply with quote

If you have two free XMM registers, for example xmm7 and xmm6, you can do this:


Code:
movups xmm7,[targetpos]
subps xmm7,[mypos]
mulps xmm7,xmm7
movaps xmm6,xmm7
shufps xmm7,xmm7,01
addss xmm7,xmm6
sqrtss xmm7,xmm7
movss [distance],xmm7

_________________
Back to top
View user's profile Send private message MSN Messenger
deama1234
Master Cheater
Reputation: 3

Joined: 20 Dec 2014
Posts: 328

PostPosted: Sat Aug 08, 2015 5:06 pm    Post subject: Reply with quote

Here's my take on it using the FPU:
Code:
fld [myPosX]
fsub [targetX]
fmul ST(0)
fstp [distanceX]// (mx - tx)^2 = dx^2

fld [myPosY]
fsub [targetY]
fmul ST(0)
fstp [distanceY]// (my - ty)^2 = dy^2

fld [distanceX]
fadd [distanceY]
fsqrt
fstp [distance]// sqrt(dx^2 + dy^2) = distance
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites