4example, 1/300 = 0.00333333

Fmt  db  '%d.%d',0

mov eax,1
mov ecx,3
div   ecx
mov Integer,eax
mov decimal,edx

invoke wsprintf,addr szBuffer,addr lFmt,Integer,decimal
invoke SetDlgItemText,hDlgItem,IDC_Show,addr szBuffer


the result is not right...


and another question, how to calc square/power(like 4^5) in assembly?
Posted on 2005-12-15 23:35:32 by Eric4ever

4example, 1/300 = 0.00333333

Fmt  db  '%d.%d',0

mov eax,1
mov ecx,3
div  ecx
mov Integer,eax
mov decimal,edx

invoke    wsprintf,addr szBuffer,addr lFmt,Integer,decimal
invoke  SetDlgItemText,hDlgItem,IDC_Show,addr szBuffer


the result is not right...


actually, the result is right. being that you are using integer division (the result should be 0). To obtain 0.003333... you will need to use FPU instructions like:


Dividend dq 1
Devisor dq 300
...
FLD Dividend
FDIV Divisor


if I'm not mistaken, all this has been documented over a hundred times on this board, try searching for FPU or Floating Point.. and a good place to learn this stuff is in the Game Programming section of this forum.

Regards,
Bryant Keller
Posted on 2005-12-16 00:48:40 by Synfire
I think you should set edx to zero.

Fmt  db  '%d.%d',0

xor  edx,edx
mov eax,1
mov ecx,3
div  ecx
mov Integer,eax
mov decimal,edx

invoke    wsprintf,addr szBuffer,addr lFmt,Integer,decimal
invoke  SetDlgItemText,hDlgItem,IDC_Show,addr szBuffer
Posted on 2005-12-16 01:20:58 by firefly2k
Eric4ever,
     Here is how you can do it without FPU instructions.  The submitted ZIPPED code can be modified and optimized.  Ratch
Attachments:
Posted on 2005-12-16 10:29:10 by Ratch
Eric4ever,
    Sorry, the code is incorrect.  Attached is a correction.  Ratch
Attachments:
Posted on 2005-12-16 12:40:07 by Ratch
and another question, how to calc square/power(like 4^5) in assembly?


If you only want to work with integers, it's easy. For squaring a 32-bit integer in memory, you simply do:
mov eax,mem_int
mul eax

and your result is a 64-bit number in EDX:EAX.

Powers of integers are almost as simple if you dont exceed a 32-bit number before the last multiplication; otherwise, you have to look at "big numbers" or use the FPU.

  mov ecx,power
  mov eax,mem_int
@@:
  dec ecx
  jz  @F
  mul mem_int
  jmp @B
@@:

and your result is also a 64-bit number in EDX:EAX.

Raymond
Posted on 2005-12-16 22:24:13 by Raymond
Ratch,

Thank you for your help, It's a good way to do it, but it has limitation(the REMAINDER must be less than DENOMINATOR,and the integer part of result is NULL), can it do the integer division without limitaion and show the results like the format "xx.xxxxxxx"(the length and precision is not very significant), whether it use FPU instructions or not.
I'm just a rookie and found nothing helpful in the google... :)

Posted on 2005-12-18 23:07:10 by Eric4ever
Your other option is to use the FPU after you learn to program it. In the meantime, you could use the attached library of FPU functions. The zip file also contains the source code of all the functions, which you can use in your learning process. Make sure you read the design philosophy section in the included Help file.

Raymond
Attachments:
Posted on 2005-12-18 23:50:57 by Raymond
Eric4ever,


... but it has limitation(the REMAINDER must be less than DENOMINATOR,and the integer part of result is NULL...


    The REMAINDER is ALWAYS less than the DENOMINATOR by definition.  Otherwise it would not be a REMAINDER, would it?  You can multiply a number by say 100000, and decompose it as a integer.  Then assume the last 5 digits are the decimal fraction part.  For instance, 1/300 times 100000 equals 100000/300 equals integer 333.  Then compensating by moving the decimal point left 5 times gives .00333 . Ratch
Posted on 2005-12-19 01:19:31 by Ratch