Solved

# Multiplication with integers and decimals and rounding numbers in assembly masm

Posted on 2005-03-04
Medium Priority
807 Views
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?
0
Question by:marcy106
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 2
• 2
• +1

LVL 14

Expert Comment

ID: 13464582
what assembler are you using? Nasm?

--trigger-happy
0

Author Comment

ID: 13464588
The question about nearest integer in assembly is in masm
0

LVL 14

Expert Comment

ID: 13464605
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

Author Comment

ID: 13464651
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

LVL 14

Expert Comment

ID: 13464675
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

LVL 14

Accepted Solution

trigger-happy earned 672 total points
ID: 13464697
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

LVL 22

Assisted Solution

grg99 earned 664 total points
ID: 13465675
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>

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

LVL 39

Assisted Solution

itsmeandnobodyelse earned 664 total points
ID: 13486954
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]
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

LVL 22

Expert Comment

ID: 13487410
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and â€¦
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anâ€¦
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month8 days, 10 hours left to enroll