### Author Topic: multiplying/dividing a positive decimal number by a positive number  (Read 31871 times)

#### shankle

• Member
• Posts: 868
##### multiplying/dividing a positive decimal number by a positive number
« on: February 28, 2015, 01:57:08 PM »
So on a computer how to div/mul a positive decimal number by a positive number?
Ex:  1.00416 by 15??? If this involves Floating Point math then I will have to start from scratch.
Works fine on a calculator but loses the decimal places on the computer.

#### Yuri

• Member
• Posts: 179
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #1 on: February 28, 2015, 02:42:47 PM »
Yes, you will have to use the FPU.
Code: [Select]
`DATA SECTIONn1  DQ 1.00416n2  DQ 15.0n3  DQ 0CODE SECTIONStart:    fld Q[n1]   ; Load n1 into the ST0 register on the FPU stack.    fdiv Q[n2]  ; Divide it by n2 directly from memory.    fstp Q[n3]  ; Pop the result off the stack to memory.#if x64     ; Show the result.    invoke msvcrt:printf, "%g / %g = %g", [n1], [n2], [n3]#else    invoke msvcrt:printf, "%g / %g = %g", [n1], [n1+4], [n2], [n2+4], [n3], [n3+4]    add esp,1Ch#endif    ret`

#### dedndave

• Member
• Posts: 8828
• Still using Abacus 2.0
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #2 on: February 28, 2015, 02:58:30 PM »
well - learning to use the FPU is a good idea, and the results will be much faster

but, you can use "fixed point" math, as an alternative

1.00416 x 100000 = 100416
you can then divide 100416 by 15 using DIV
understand that the result is 100,000 times the desired answer
you can convert it to a string, and insert the decimal point

#### FORTRANS

• Member
• Posts: 1215
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #3 on: March 01, 2015, 12:50:52 AM »
Hi,

In the old forum I posted a DOS program to show how fixed point
arithmetic works.  Sort of.

http://www.masmforum.com/archive2012/5486_FIX2DEC.zip

I think that is it.

Steve

#### shankle

• Member
• Posts: 868
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #4 on: March 01, 2015, 12:54:28 AM »

#### shankle

• Member
• Posts: 868
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #5 on: March 01, 2015, 11:38:44 AM »
Sorry Fortrans the file unzipped is not compatible with Windows 7.
Maybe you know some other way of reading it?

#### FORTRANS

• Member
• Posts: 1215
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #6 on: March 01, 2015, 11:50:46 PM »
HI,

You can install a DOS emulator or virtual machine to run
16-bit programs.  DOSBOX, Bochs, and Virtual PC have been
mentioned by others as good solutions.

As that program uses the BIOS to write to video memory in CGA
mode 3, I do not think it a good program to translate to 32-bit (64-bit?)
Windows.  Dave, or others, can correct me if I have erred in my
estimate.

Steve

#### Gunther

• Member
• Posts: 4115
• Forgive your enemies, but never forget their names
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #7 on: March 02, 2015, 07:08:46 PM »
Hi shankle,

for the operation you can use the XMM registers, too.

For the program provided by FORTRANS you could use the VMWare Player as another alternative for the Virtual Machine.

Gunther
You have to know the facts before you can distort them.

#### dedndave

• Member
• Posts: 8828
• Still using Abacus 2.0
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #8 on: March 03, 2015, 02:17:42 AM »
Jack,

i used fixed-point math to display the Gamma value in the status bar

http://masm32.com/board/index.php?topic=4052.msg42784#msg42784

the track bar gives you integers (i used 0 to 400, in this case)
i convert those values to a range of 033 to 300
then, i display the value in the status bar by inserting a decimal point in the string (0.33 to 3.00)

later, i use the converted integer to perform floating point math
i load it into the FPU and divide by 100 for those calculations
but, it's easier to display using fixed-point integer math

#### shankle

• Member
• Posts: 868
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #9 on: March 06, 2015, 11:53:50 AM »
3-5-2015

I loaded this code into my 64-bit GoAsm Program.
I got nothing on the screen. Thought that was what msvcrt:printf did.
The program compiled without errors.
I am not familiar with msvcrt:printf.

I gave up on the fixed point suggestion.
Got a lot of invalid results that made no sense.
So something hidden was altering the results.

n1        dq  1.00416
n2        dq  15.0
n3        dq  0
fld  Q[n1]   ; load n1 into the ST0 register on the fpu stack
fdiv Q[n2]   ; divide it by n2 directly from memory
fstp Q[n3]   ; pop the results off the stack to memory
invoke msvcrt:printf, "%g / %g = %g", [n1], [n2], [n3]

Tnanks Yuri for the sample code.
Thanks Dave for the pointer to "fpu tutorial by Raymond Filiatreault"

#### dedndave

• Member
• Posts: 8828
• Still using Abacus 2.0
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #10 on: March 06, 2015, 12:07:51 PM »
i am not "real" big on printf, either - lol

https://msdn.microsoft.com/en-us/library/wc7014hz.aspx

#### rrr314159

• Member
• Posts: 1378
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #11 on: March 06, 2015, 03:43:06 PM »
shankle, here's how I would do that ...

 woops, didn't realize I was in the GoAsm forum at first, shouldn't have said anything; removed irrelevant material ...  ::)

...

GoAsm has some logical ideas, I should check it out further someday
« Last Edit: March 06, 2015, 07:12:12 PM by rrr314159 »
I am NaN ;)

#### Yuri

• Member
• Posts: 179
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #12 on: March 06, 2015, 05:46:36 PM »
shankle
Yes, it should show the result on the screen. If you just double-click the resulting exe, then probably the console window quickly flashes and disappers and you can't see what is in it. If so, then you can tell it to wait till you press any key, using the "system" function.
Code: [Select]
`DATA SECTIONn1  DQ 1.00416n2  DQ 15.0n3  DQ 0CODE SECTIONStart:    fld Q[n1]   ; Load n1 into the ST0 register on the FPU stack.    fdiv Q[n2]  ; Divide it by n2 directly from memory.    fstp Q[n3]  ; Pop the result off the stack to memory.#if x64     ; Show the result.    invoke msvcrt:printf, "%g / %g = %g", [n1], [n2], [n3]    invoke msvcrt:system, "echo. & pause"#else    invoke msvcrt:printf, "%g / %g = %g", [n1], [n1+4], [n2], [n2+4], [n3], [n3+4]    add esp,1Ch    invoke msvcrt:system, "echo. & pause"    add esp,4#endif    ret`
rrr314159
GoLink looks for imported functions directly in DLLs, so the "msvcrt" prefix is the name of the DLL. The ".dll" extension can be omitted. DLLs can also be bound with the #dynamiclinkfile directive in source code or specified on GoLink's command line, in that case the prefix is not needed.

#### rrr314159

• Member
• Posts: 1378
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #13 on: March 06, 2015, 06:31:01 PM »
Thanks Yuri, that's a very good feature. I got here by clicking on "show unread posts since last visit", didn't realize I was in the GoAsm forum at first, or I would have researched it b4 saying anything.

Thanks for the info!
I am NaN ;)

#### MichaelW

• Global Moderator
• Member
• Posts: 1196
##### Re: multiplying/dividing a positive decimal number by a positive number
« Reply #14 on: March 06, 2015, 09:20:59 PM »
I realize that the "Real Men" here are going to frown on this, but you can ease the task of using the FPU by creating a simple helper DLL in a HLL (I used Pelles C for this) that provides functions that make it relatively easy to do simple calculations. You need only a single instruction to get the return value into an appropriate variable, FSTP for 32-bit code where FP values are returned on the FPU stack, or MOVLPD for 64-bit code where FP values are returned in XMM0 (at least for Pelles C and I think also for the Microsoft compilers and CRT). The attachment contains the source for the DLL, the 64-bit DLL and import library, the C source and executable for my C test app, and a JWASM 64-bit test app linked with POLINK (sorry, I don't currently have GoAsm installed).

Also, I seem to recall that GoAsm does not use import libraries, so I'm not sure how you should link with the DLL.
Well Microsoft, here’s another nice mess you’ve gotten us into.