News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Percentages for the mathematically challenged ...

Started by James Ladd, May 22, 2005, 02:03:22 AM

Previous topic - Next topic

James Ladd

Im mathematically challenged at the best of times so trying to do some math in assembler is also
mind boggling.

I want to get the percentage of a number.
For example, I have allocated 'n' bytes and when I want to increase the memory I want to do so
by a user specified amount. Say 'x'.

So if I have 1024 bytes and I want to increase by 10% that would be 1126.4 bytes.
I dont care about the .4 and I dont care if its rounded up or down.

I dont have a clue how to do it in assembler and was wondering if someone could help.

rgs, striker.

roticv

Try


;in = eax
imul eax, 11
mov ecx, 10
xor edx, edx
div ecx
;result in eax

tenkey

1% = 1/100

To get 1% of N, divide it by 100, resulting in N / 100.

If you want X%, multiply by X, so that X% of N is X * N / 100.

To avoid losing more digits than necessary, multiply before dividing.

If you want to increase a value by X%, add the X% to the original value.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

Farabi

#3
If you want the value behind comma, multiply iy by 100.


IncByPersent proc theval:dword,persen:dword

xor edx,edx
mov eax,theval

mov ecx,persen
mul ecx
mov ecx,100
div ecx

add theval,eax
xchg eax,theval

ret
endp  ; THe result is theval/100




Thanks, you are right, I made mistake on it, im copy paste the mul instruction. And also, its all reversed, Im not testing it, just type down. I think I did made it somewhere and the name is fGrad.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

RuiLoureiro

Hi

Farabi,
          1.  This topic is to play, i think.

          2.   You made a mistake: mul  ecx    and next   div  ecx.

          3.  At the end you said: «THe result is theval/100». But what do you want ?

          4.  If the value is in EBX [ eg. ebx= 1024 ] and the percentage ( not divided by 100 ) is in ECX [ eg. ecx= 32 ] then

               the result is:  ebx + ebx *( ecx / 100)
                                = ebx * (1 + ecx / 100 )
                                = ebx * ( 100 + ecx ) / 100                 [eg.  1024 * 132 / 100 = 1351.68
                                                                                                   135 168 / 100   = 1 358. 68]

           5. If we want to discount 32% we change signal + to minus -:

                                = ebx * ( 100 - ecx ) / 100

The roticv case is valid only to 10%:     ebx * (110 /100)
                                                      = ebx * 11 / 10

                               
stay well

AeroASM

But since striker does not care about the remaining fraction bit, it seems a waste of time converting to floating point and then back to integer. Also th FPU floating point is slower than integer.

BTW I am quite sure that roticv is a "her", and don't you mean Raymond anyway?

RuiLoureiro

Hi Mark,
            about this
Quote from: Mark Jones on May 23, 2005, 09:48:45 PM
Perhaps there is an easy way to convert the integer to MMX, and do a multiply by 0.xx where xx is 0-100%.
... best course of action is to process the math in floating-point format

i say the same as AeroASM: «... it seems a waste of time converting to floating point and then back to integer. Also th FPU floating point is slower than integer».
It is also important  to write all the code for both cases and to see the timing of each instruction.
Another question is «what do we want ?».



Phil

Wow, I glanced at farabi's code last night and thought it would work! Here's a revision that I've tested:

IncByPersent proc theval:dword,persen:dword

xor edx,edx
mov eax,theval
mov ecx,100
add ecx,persen
mul ecx
mov ecx,100
div ecx
ret

IncByPersent endp     ; The result is theval*(100+persen)

thomasantony

Quote from: AeroASM on May 24, 2005, 06:20:46 AM
BTW I am quite sure that roticv is a "her", and don't you mean Raymond anyway?
You need to know more  :bdg :bdg
Roticv = Victor

Check out his site roticv.rantx.com

Thomas :lol :lol
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

AeroASM

I knwo that. Scroll down to the family photo, and I swear that roticv is the girl on the far left.

Nilrem

Victor is a male name. Anyways who cares? Male, female, somewhere inbetween!? Doesn't matter, this is the internet a place where you are judged by what you do not what sex you are.

Ops I nearly went into a rent, let's keep this on-topic.

thomasantony

Quote from: AeroASM on May 26, 2005, 06:14:05 AM
I knwo that. Scroll down to the family photo, and I swear that roticv is the girl on the far left.
Yeah she is good looking :bdg :bdg :bdg :bdg :bdg :bdg. Heeellppp roticv is coming after me ....
:wink :wink :wink

Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free


AeroASM

Argh! Me and pbrennick were wrong. Nilrem and others, sorry for wasting your time.

Farabi

xchg is 743 tick at 10 000 000 loop and mov  is 0. Im using 1.7 Ghz processor.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"