Solved

# multiplication and rounding numbers to nearest integer in assembly

Posted on 2005-03-04
Medium Priority
628 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
• 5
• 3
• 2
• +3

LVL 3

Accepted Solution

Dawaffleman earned 2000 total points
ID: 13465813
i would use the FPU, although im sure there are otherways to do it:

.model medium
.586                    ;pentium instructions
.stack 100h
.data

thosand dd 1000   ;needs to be 32 bit
two       dd 2        ;32 bit
value    dd  ?        ;doesnt need to be 32 bit, but what the heck

.code

start:

finit                   ;initiate FPU

fild 1618             ;this will load 1618 onto the fpu stack
fdiv thousand            ;divides 1618 by 1000 to get 1.618 on the stack
fild two                  ;load 2 onto the fpu stack
fmulp st(1),st(0)    ;multiply stack value and pop stack (0):   stack (1)=stack (1) * stack (0)
fist value             ;store value on top of stack to value, using fpu control flags (which are 'round'
;                                                                                      at default)

end start

there i think that is what your looking for. try it out and see if it works
0

LVL 22

Expert Comment

ID: 13466547
Ah, nice example, but I think the round-bit in the FPU only rounds at the LSBit, not by decimal 0.5 ??

0

LVL 3

Expert Comment

ID: 13468781
im not sure exactly what you mean. are you saying that the FPU will only round the part left of the decimal point?
here is an excerpt from the AOA talking about the FPU control register:
-------------------------------------------------------------------------------------------
Bits 10 and 11 provide rounding control according to the following values:

Rounding Control Bits 10 & 11 Function
00 To nearest or even
01 Round down
10 Round up
11 Truncate

The "00" setting is the default. The 80x87 rounds values above one-half of the least significant bit up. It rounds values below one-half of the least significant bit down. If the value below the least significant bit is exactly one-half the least significant bit, the 80x87 rounds the value towards the value whose least significant bit is zero. For long strings of computations, this provides a reasonable, automatic, way to maintain maximum precision.

The round up and round down options are present for those computations where it is important to keep track of the accuracy during a computation. By setting the rounding control to round down and performing the operation, the repeating the operation with the rounding control set to round up, you can determine the minimum and maximum ranges between which the true result will fall.

-----------------------------------------------------------------------------------------------------------------
0

LVL 22

Expert Comment

ID: 13470235
Hmm, I looked up FIST, and YOU're RIGHT!

It does round the real number according to the rounding settings of the FPU.

I've never seen this as every higher level language, like C, FORTRAN, Pascal that I've used has the convention of truncating reals when stored into integers.

So if you're unsure what the FPU rounding settings are, you'd better push the floating point status word, set the rounding option bits, do the math, then restore the FP status.

Regards,

grg99

The question is does the floating store to integer instruction round or truncate.  I always assumed it truncated.
0

LVL 3

Expert Comment

ID: 13470273
oh yeah, before i whipped out the AOA to refresh my memory i was almost sure that it had truncated too, instead of rounding. i was suprised to see that it rounded by default. i was also happy because it made my code easier : )
0

LVL 10

Expert Comment

ID: 13471423
Hi, just notice for grg99 Delphi (object pascal)
has a function SetRoundMode which corresponds
to rounding control values (i.e. Nearest,Down,Up,Truncate)

regards,
Kate
0

LVL 12

Expert Comment

ID: 13521398
Hi Dawaffleman,
> i would use the FPU, although im sure there are otherways to do it:

Sure, you can use fixed-point math, like in your case with three digits precision, meaning a factor of 1000:

2* 1.618
->
2000 * 1618 = 3236000
divide by 1000 -> 3226
Now, you can do truncate as:
3226 / 1000 = 3, 3*1000=3000

...or round to the nearest integer, that's simply by doing
(3226 + 500) / 1000 = 3, 3*1000 = 3000

I'm sure you can figure out the necessary ASM yourself.

Cheers!

Stefan
0

LVL 3

Expert Comment

ID: 13522886
ok...im not quite sure what your getting at.

i also dont understand why you multiply 3*1000 at the end

???
0

LVL 10

Expert Comment

ID: 13523977
Hi Dawaffleman or you can do it
in a complete floating-point manner.
Just like:

.model medium
.586
.stack 100h
.data

first dt 2.0
second dt 1.618
result dd  ?

.code

start:

finit
fld first
fld second
fmulp st(1),st(0)
fist value

end start

regards,
Kate
0

LVL 10

Expert Comment

ID: 13524013
well much rather :)

.model medium
.586
.stack 100h
.data

first dd 2.0
second dd 1.618
result dd ?

.code

start:

finit
fld first
fld second
fmulp st(1),st(0)
fist result

end start

I hate typos,
Kate
0

LVL 3

Expert Comment

ID: 13524280
oh i didnt know you could load floating point numbers into variable at the beginning like that, but duh me, why not heh, it makes sense.
0

LVL 12

Expert Comment

ID: 13524320
Dawaffleman,
> i also dont understand why you multiply 3*1000 at the end

Because you use the last 3 integer digits as your fractional part, 3000 representing 3.000.

Unlike floating point math, fixed point is resistant to precision problems - you just have to check against overflows.

For example: You have a 10-digit (decimal) mantissa and want to do currency calculations, requiring at least two fractional digits. When you're using floating point math, values more than 10^8 won't have enough precision to fully support a two-digit fraction. You can get nasty and unnoticed precision problems (i.e., the cents don't add up like they should).

By using fixed-point math, an overflow is easily detected. That's why acient languages like COBOL are still popular, they have proper fixed-point support.
0

LVL 7

Expert Comment

ID: 13742118
1618/1000 ?? What happened to (sqrt(5)+1)/ 2 ?
0

LVL 61

Expert Comment

ID: 15765690
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Dawaffleman {http:#13465813}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mbizup
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

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

The Summer 2017 Scholarship Winners have been announced!
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insiâ€¦
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (httpsâ€¦
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
###### Suggested Courses
Course of the Month12 days, 1 hour left to enroll