flat assembler
Message board for the users of flat assembler.

Index > Windows > please help (fixed point math)

Author
Thread Post new topic Reply to topic
rain_storm



Joined: 05 Apr 2007
Posts: 67
Location: Ireland
rain_storm 02 Oct 2007, 16:30
Can anyone tell me why my fixed point macros dont work? Im using a scale factor of 65536 (16.16) so the high word should contain the integer and the low word should contain the fraction. I am having major problems with fixdiv macro I think its overflowing and it always crashes the program.

Code:
macro fixadd Des,Src,Tar
{
    mov  eax,Src
    add  eax,Tar
    mov  Des,eax
}
macro fixsub Des,Src,Tar
{
    mov  eax,Src
    sub  eax,Tar
    mov  Des,eax
}
macro fixmul Des,Src,Tar
{
    mov  eax,Src
    mov  edx,Tar
    imul edx
    shrd eax,edx
    mov  Des,eax
}
macro fixdiv Des,Src,Tar
{
    mov  eax,Src
    mov  edx,eax
    sar  edx,16
    shl  edx,16
    idiv Tar
    mov  Des,eax
}
    
Post 02 Oct 2007, 16:30
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 02 Oct 2007, 17:03
Code:
    sar  edx,16 
    shl  edx,16 
    

Same as just
Code:
and edx, $FFFF0000    


And IDIV faults when (EDX != -1) OR (EDX != 0) after division. The division algo is not properly scaling the number.

Here my version
Code:
macro fixdiv Des,Src,Tar
{ 
    mov  eax,Src
    mov  edx, 1 shl 16
    imul edx
    idiv Tar
    mov  Des,eax 
}     

Note that this topic was discussed and someone even wrote an article on this forum, search for it.

In case you don't want to use IMUL this is another way
Code:
macro fixdiv Des,Src,Tar
{ 
    mov  eax,Src
    cdq
    shld edx, eax, 16
    shl  eax, 16
    idiv Tar
    mov  Des,eax 
}    


[edit]Or
Code:
macro fixdiv Des,Src,Tar
{ 
    mov  eax,Src
    mov  edx, eax
    shl  eax, 16
    sar  edx, 16
    idiv Tar
    mov  Des,eax 
}     
Which is a fix of your own version Razz[/edit]
Post 02 Oct 2007, 17:03
View user's profile Send private message Reply with quote
rain_storm



Joined: 05 Apr 2007
Posts: 67
Location: Ireland
rain_storm 02 Oct 2007, 19:33
I will use your version since it works and its only 5 instructions =D
Thank you Loco' for the quick reply
Post 02 Oct 2007, 19:33
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.