Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 837
  • Last Modified:

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?
0
marcy106
Asked:
marcy106
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now