Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

x86 - How to multiply 5% in assembly language and display it out in 2 decimal place

I initialize the price of the food and request user to input a quantity and I am able to get an accurate result after multiplication. But can I ask how to use the total amount to multiply with 5% so that when user apply promo code they can get a 5% discount? and how to display out the final result to 2 decimal places? I am new to this language and could not solve this, any of your help is much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since it looks like you are working with a processor that provides only integer operations, you'll have to pick a numeric format for storing your prices and for your percentages.

Among the choices are strings, binary coded decimal (bcd), fixed point (binary scaling), and (decimal) scaling.

A string stores one digit per byte possibly as an ASCII character.? To add and multiply these will take considerable work.? Similar is true for the various bcd forms.

The scaled forms use integers to store numbers that are understood as being pre-multiplied by the scale.? For example, using a x100 scale, we can store a number like $5.05 as the integer 505.? When working with scaled numbers we have to remember how scaling affects arithmetic.

For addition of scaled numbers, both operands need to have the same scale or else the results will be bad.? So to add $1 to $5.05 scaled x100 as 505, we have to also scale $1 x100 as 100 — now we can add 505 + 100 using the normal processor integer addition operation to get 605, which is also x100 scaled, so that means $6.05.

When we multiply, the scales can be mixed but the result will have the scale of the product of operands' scales.? $5.05 scaled x100 as 505 multiplied by 5 (say for qty of 5 items of said price) is 2525, at x100 scale, value $25.25.? We know the scale of the first operand is x100 and of the second operand is x1, so the multiplication result's scale is 100x1 or x100.

To represent a %, like 5% in x100 scale, 5% = 0.05, which would be simply 5, also in x100.? Multiplying $5.05 x100 scaled as 505 by 5(%) is also 2525, but now the result's scale is x10000, because both numbers started at scale x100 and 100x100 = 10000.? So this 2525 really means $0.2525.

(You may or may not want to round such results to return this x10000 result to x100 scale, by dividing by 100 — e.g. 25 at x100 scale, meaning $.25)

Printing scaled numbers requires printing a . at the scale factor.? So, for x100 scale, for example, we might turn the whole scaled number into a string, print the first n digits of the string, print a . and then print the last 2 digits.

Or, we can divide the number to print by 100, and print that, then print the . and modulo the number to print by 100 and print that.? (On x86, the quotient (the result of integer division) and remainder (the result of modulus operator) can both be had in one instruction.)

Fixed point (aka binary scaling) uses a power of 2 for the scale factor so that the division and remainder operation necessary for printing can be done with shifting and masking instead of actual division.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...