Multiplication with integers and decimals and rounding numbers in assembly masm

how can i do this operation in asembly and rounding the number to nearest interger Ex:
 2 * 1.618 = 3.236 and rounded to 3?
marcy106Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

trigger-happyCommented:
what assembler are you using? Nasm?

--trigger-happy
0
marcy106Author Commented:
The question about nearest integer in assembly is in masm
0
trigger-happyCommented:
oh shoot....sorry >.< I didn't scroll all the way to the right so i didn't see the word masm....there's just one thing that bothers me...why is this question in C++ when it should be in assembly section?....

--trigger-happy
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

marcy106Author Commented:
I'm sorry this question is in assembly but by mistake i posted first in c++. Anyway do you know the answer??? I really need this to write a program that it's due today. : (   .............................
0
trigger-happyCommented:
sorry, but i'm not really that good with assembly language, i dnt even knw anything about masm >.<, i'll try finding the answer on the internet anyway.

--trigger-happy
0
trigger-happyCommented:
I found something that might help:
http://doc.ddart.net/asm/Microsoft_MASM_Programmers_Guide_v6.1/Chap_06.htm
look into somewhere near the middle of the page, you'll find this:

FRNDINT               Rounds ST to an integer

I'm not sure of the details, but i suggest reading some more about it.

--trigger-happy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
grg99Commented:
The easiest way to do this:     write a little C program and compile it with the option to generate assembly code.
Very important:  select NO optimization  otherwise the compiler is likely to optimize all or most of the computation.



#include <math.h>

float op1, op2;  int Answer;

void compute() {  op1 = 2.000;  op2 = 1.618;  Answer = round( op1 * op2 ) ; printf("Answer is: %d\n", Answer ); }


Now depending on the compiler, it may generate a call to round() external function or do the round() in-line.

If it calls a function, that doesnt help you , so you'll have to write your own round:

int round( float x ) { int Ans; if( x > 0.0 ) Ans = x + 0.5; else Ans = x - 0.5;  return Ans; }

 
0
itsmeandnobodyelseCommented:
Look at that:

123:     register double d =1.618;
00402333   mov         dword ptr [ebp-14h],0F7CED917h
0040233A   mov         dword ptr [ebp-10h],3FF9E353h
124:     int       ix  = (int)(d + d + 0.5);
00402341   fld         qword ptr [ebp-14h]
00402344   fadd        qword ptr [ebp-14h]
00402347   fadd        qword ptr [__real@8@3ffe8000000000000000 (0055e038)]
0040234D   call        __ftol (004281ec)
00402352   mov         dword ptr [ebp-18h],eax

The C++ code of that is

   register double d =1.618;
   int       ix  = (int)(d + d + 0.5);


To include assembler to C++ code you may use __asm keyword, e. g.

int power2(int num, int power)
{
   __asm
   {
      mov eax, num    ; Get first argument
      mov ecx, power  ; Get second argument
      shl eax, cl     ; EAX = EAX * ( 2 to the power of CL )
   }
   /* Return with result in EAX */
}


Regards, Alex
0
grg99Commented:
Watch out:

>       shl eax, cl     ; EAX = EAX * ( 2 to the power of CL )

(1)  Works correctly only for "num"s and "powers"s greater or equal to zero.
(2)  And gives no indication of overflow.
(3)  And may give mysterious results if the optimizer is holding something in any part of ecx.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.