View previous topic :: View next topic |
Author |
Message |
ciglipaf How do I cheat?
Reputation: 0
Joined: 26 Jul 2015 Posts: 6
|
Posted: Thu Aug 06, 2015 9:25 am Post subject: floating point arithmetic operations |
|
|
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 |
|
 |
gameplayer Advanced Cheater
Reputation: 2
Joined: 26 Jun 2011 Posts: 97 Location: Vietnam
|
Posted: Thu Aug 06, 2015 9:38 am Post subject: |
|
|
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 |
|
 |
ciglipaf How do I cheat?
Reputation: 0
Joined: 26 Jul 2015 Posts: 6
|
Posted: Thu Aug 06, 2015 1:04 pm Post subject: |
|
|
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 |
|
 |
gameplayer Advanced Cheater
Reputation: 2
Joined: 26 Jun 2011 Posts: 97 Location: Vietnam
|
Posted: Thu Aug 06, 2015 9:13 pm Post subject: |
|
|
It's correct.
|
|
Back to top |
|
 |
justa_dude Grandmaster Cheater
Reputation: 23
Joined: 29 Jun 2010 Posts: 891
|
Posted: Thu Aug 06, 2015 9:28 pm Post subject: |
|
|
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 |
|
 |
Gniarf Grandmaster Cheater Supreme
Reputation: 43
Joined: 12 Mar 2012 Posts: 1285
|
Posted: Fri Aug 07, 2015 1:36 am Post subject: |
|
|
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 |
|
 |
mgr.inz.Player I post too much
Reputation: 222
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Fri Aug 07, 2015 8:18 am Post subject: |
|
|
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 |
|
 |
deama1234 Master Cheater
Reputation: 3
Joined: 20 Dec 2014 Posts: 328
|
Posted: Sat Aug 08, 2015 5:06 pm Post subject: |
|
|
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 |
|
 |
|