The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: devilhorse on October 25, 2008, 07:19:49 PM

Title: COnvert remainder to decimal form?
Post by: devilhorse on October 25, 2008, 07:19:49 PM
I know the result of the code snippet below is 51 with a remainder of 3. I have used dwtoa to display the quotient 51 in a textbox. I have also used dwtoa to display the remainder 3 in a textbox. The problem is I don't know how to show the remainder as a decimal instead of a whole number. I know the remainder is in edx. How do I divide the remainder by 7 and then display it in decimal form?


Mov Eax, 360
Cdq
Div 7
Title: Re: COnvert remainder to decimal form?
Post by: jj2007 on October 25, 2008, 10:04:48 PM
Could be a nice exercise. Some time ago I did that using the FPU - see float$. (http://www.masm32.com/board/index.php?topic=9756.msg72641#msg72641)

If you want a fixed number of decimals, multiply 360 with 1000, divide by 7 = 51428, and insert a dot at the third last position: 51.428
Title: Re: COnvert remainder to decimal form?
Post by: raymond on October 26, 2008, 01:24:40 AM
At least, you do know that your remainder is always in EDX. After converting the integer part to ascii, assuming EDI points to the byte following the last integer digit,

   push  edx               ;save the remainder
   ....                    ;your code to convert EAX to ascii
   mov   byte ptr[edi],"."
   inc   edi
   mov   ecx,decimal_cnt   ;number of decimal digits required
   pop   eax               ;recover remainder
@@:
   mov   edx,10            ;<=
   mul   edx               ;<=
   div   divisor
   add   al,30h            ;convert to ascii
   mov   [edi],al          ;store it
   inc   edi               ;advance pointer
   mov   eax,edx           ;transfer remainder to eax
   dec   ecx
   jnz   @B                ;continue until count exhausted
   mov   byte ptr[edi],0   ;ready for display


The above should work regardless of the size of the divisor as long as it is not larger than 32 bits.
Title: Re: COnvert remainder to decimal form?
Post by: devilhorse on October 26, 2008, 06:05:41 AM
 :UThanks for the help. I will use both methods when appropriate.
Title: Re: COnvert remainder to decimal form?
Post by: raymond on October 26, 2008, 05:09:17 PM
My apology. I forgot one important instruction in the provided code. I thus edited the original code for it.