SteveAndrew Master Cheater
Reputation: 30
Joined: 02 Sep 2012 Posts: 323
|
Posted: Thu Mar 21, 2013 9:20 pm Post subject: Re: how to add float number to a register in asm(x86) |
|
|
azginporsuk wrote: | lets say I want to add x(float) to the eax register.How can I do this? |
So the eax register contains a float value, and you want to add a certain amount to it right?
Okay well there's two ways to do it... But you can't just use a standard register, it has to be a memory location to work with... Chances are your code you want to do this with had that value in a memory location before it was in eax, so you could use that location...
Or if you have to use your own memory location then its still not too bad... just move it into one first...
1. Use the extended floating point stack
or
2. Use SSE instructions (the xmm registers [xmm0-xmm7])
So for example with using the extended floating point stack:
Code: |
Someplace:
mov [MyMemoryLocation],eax //copy eax to memory location
fld dword ptr [MyMemoryLocation] //load first value into FPU stack from memory location
fadd dword ptr [AddThisValueToIt] //Add your value
fstp dword ptr [MyMemoryLocation] //pop from FPU back into memory location
mov eax,[MyMemoryLocation] //copy sum of both floats back into eax
MyMemoryLocation:
dd 0
AddThisValueToIt:
dd (float)1337.7 //this is the value to add
|
And example using xmm registers
Code: |
Someplace:
mov [MyMemoryLocation],eax //copy eax to memory location
movss xmm0,[MyMemoryLocation] //load first value into xmm0 (first position in it)
addss xmm0,[AddThisValueToIt] //add your value to first position in xmm0
movss [MyMemoryLocation],xmm0 //copy from first position in xmm0 back to memory location
mov eax,[MyMemoryLocation] //copy sum of both floating points back into eax
MyMemoryLocation:
dd 0
AddThisValueToIt:
dd (float)1337.7 //this is the value to add
|
both approaches are very nice, but I prefer the FPU (especially when you aren't sure whether xmm registers are used, if they are you should push and pop the one(s) you utilize...)
you can't just push/pop an xmm register like you can a normal register though, however you can do this which is equivalent:
Code: |
//Push xmm0 -->
sub esp,10
movdqu dqword [esp],xmm0
// <--
//Pop xmm0 -->
movdqu xmm0,dqword [esp]
add esp,10
// <--
|
It's sub/add esp,10 (16 decimal) because each xmm register is 128 bits in total size (4 bytes * 4) which is what I was talking about the the first position of the xmm0 register up there... (the SS in movss or addss means single scalar, but whats important to know is it only effects the first 4 byte value of whats inside the xmm register)
So say [MyMemoryLocation] contained a value of 110.0 and you moved it into xmm0 with movss
xmm0 will contain and will look like this in memory:
[110.0]-[0.0]-[0.0]-[0.0]
So then you add 1337.7 to it with addss and now its like:
[1447.7]-[0.0]-[0.0]-[0.0]
then when you copy it back into your memory location it still remains in there... So that's why you have to push/pop it just to be safe, it will effect a value that was in there before, also when using the 'SS' instructions movss, addss, etc it clears the other 96 bits (12 bytes) of the xmm register, so keep that in mind... (if you push/pop though it wont matter since you restore all the 128 bits (16 bytes) after you've messed with it)
Hope this clears things up
_________________
|
|