flat assembler
Message board for the users of flat assembler.

 Index > DOS > shortest way to display decimal number ? Goto page Previous  1, 2
Author
donkey7

Joined: 31 Jan 2005
Posts: 127
Location: Poland, Malopolska
donkey7 01 Jun 2006, 18:42
optimizing for size sometimes means also optimizing for speed. imagine you have ten 8 mb applications running simultaneously. and then imagine ten 80 kb applications. you see how the second case is easy to handle by cpu cache.

loading code takes *much* more time than executing, so code inside loops should be optimized for speed and code outside loops should be optimized for size. this would guarantee best performance.

(i based on agner.org/assem intel optimization guide)

sorry for off topic
01 Jun 2006, 18:42
saigon

Joined: 29 May 2006
Posts: 62
saigon 12 Jun 2006, 17:47
Quote:
And about replacing DIV with MUL there is a trick to do that but I don't remember how to calculate the multiplicator number to simulate division.

We have a programmer without much mathematical experience
This is one of the simplest things to do.

Let's assume you want to divide 8 by 4, to get the multiplicator number, just divide 1 with 4 and you have your multiplicator Now you just have to replace your DIV's with MUL's using the result you got.

Theoretically, you still have to divide to get the multiplicator, so you won't get off this annoying word, "divide"
12 Jun 2006, 17:47
LocoDelAssembly

Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 12 Jun 2006, 18:56
saigon, 1 DIV 4 is 0, not very good multiplicator isn't it?

Of course your method works fine for floating poing where 1 FDIV 4 is 0.25 but for integer reciprocal you need the technics described in Agner Fog's optimization manual (or other sources).

Quote:

We have a programmer without much mathematical experience

I passed the high school and I still don't know how I did that :S Anyway I'm paying for that at the university, I'm 22 years old and I'm still wanting to pass the maths courses of the first year
12 Jun 2006, 18:56
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8041
Location: Kraków, Poland
Tomasz Grysztar 12 Jun 2006, 19:29
In context of the link I gave several posts above: is there anyone but me interested in p-adic calculations here?
12 Jun 2006, 19:29
saigon

Joined: 29 May 2006
Posts: 62
saigon 12 Jun 2006, 19:36
@locodelassembly: What I wanted to explain is what you explained And please don't get upset or mad of my comment, I am just a teenager and don't know even half of the standard mathematic stuff. Albert Einstein said: "If you had a problem, mine was bigger."
@Tomasz: I am interested in p-adic calculations
12 Jun 2006, 19:36
Just me

Joined: 10 Jun 2006
Posts: 14
Just me 12 Jun 2006, 20:35
Hi all
Don't know if this is going to be of any help.

http://www.df.lth.se/~john_e/gems.html
12 Jun 2006, 20:35
Borsuc

Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 14 Jun 2006, 13:51
Hey, dividing integers by multiplying is not a true mathmatical subject, it's more on numerical/bit analysis.

Just try this:

- when you multiply a 16-bit number with the max value+1 (which is 65536) what does happen? simple: the multiplicand gets placed in the upper 16-bits, which when using 16-bit means it's placed in dx, ok so far?

- to find the "magic" number, let's do some tricks. In math:
Code:
```x*5
--- = x
5    ```
So we will "rearrange" this property and do our previous x*65536, but instead we divide "x" by whatever we wish, so:
Code:
```x*65536
-------
y    ```
Where y is the number we wish to divide x by.

- Ok, what can we observe? Simple: (x/y)*65536 just places the result (x/y) in dx (we're in 16-bit). But of course, in math, this is the same as: x*(65536/y), so we just GOT RID of the division!! 65536/y is just a constant, in fact it's that "magic" number.

note1: you see now where 6554 comes from? it's 65536/10, rounded of course.

note2: this is the same with 32-bit or any other size. Just replace 65536 with the max number+1 (for example, in 32-bit it is 0x100000000 which is 0xFFFFFFFF+1). The result is, in 32-bit, placed in edx.

hint: generally, numerical/bit analysis is done by a lot of testing, so keep testing. Also, you can see how general math is used here, and how useful it can be, even when you might consider it useless.

_________________
Previously known as The_Grey_Beast
14 Jun 2006, 13:51
LocoDelAssembly

Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 14 Jun 2006, 14:22
Thanks The_Grey_Beast
14 Jun 2006, 14:22
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8041
Location: Kraków, Poland
Tomasz Grysztar 14 Jun 2006, 14:50
The_Grey_Beast wrote:
Hey, dividing integers by multiplying is not a true mathmatical subject, it's more on numerical/bit analysis.

I can ensure you that what I wrote in that other thread about dividing by multiplying is a true and pure abstract algebra. The formal reasoning for the general case is even more so.

And your approach focuses on obtaining the approximate result of division, while in general the magic-number division should give the same result as regular integer division done by CPU - division with the remainder, thus the result should be a dividend - the floor integer.

Consider x=200, y=100, 65536/100=655 (rounded), x*655=131000, which is 1:65464, so you'd get 200/100=1 this way!

Similar, but better method was that proposed by NightWare in the other thread - you'd take (65535 div 100)+1=656 as a multiplier, this one would give correct result for this case, however - as I also pointed out there - it still may give wrong results even with relatively small numbers.
14 Jun 2006, 14:50
ATV

Joined: 31 Aug 2004
Posts: 109
Location: Finland
ATV 15 Jun 2006, 14:16
This is little offtopic, but some of these has been used in Hugi Size Compo
Code:
```imul    bx,ax,128       ;bh=ax/2   (works for 0-511)
imul    bx,ax,86        ;bh=ax/3   (works for 0-127 )
imul    bx,ax,64        ;bh=ax/4   (works for 0-1023 )
imul    bx,ax,52        ;bh=ax/5   (works for 0-63 )
imul    bx,ax,43        ;bh=ax/6   (works for 0-130 )
imul    bx,ax,37        ;bh=ax/7   (works for 0-89 )
imul    bx,ax,32        ;bh=ax/8   (works for 0-2047 )
imul    bx,ax,29        ;bh=ax/9   (works for 0-52 )
imul    bx,ax,26        ;bh=ax/10  (works for 0-68 )
imul    bx,ax,24        ;bh=ax/11  (works for 0-31 )
imul    bx,ax,22        ;bh=ax/12  (works for 0-34 )
imul    bx,ax,20        ;bh=ax/13  (works for 0-63 )
imul    bx,ax,16        ;bh=ax/16  (works for 0-4095 )
imul    bx,ax,13        ;bh=ax/20  (works for 0-78 )
imul    bx,ax,10        ;bh=ax/26  (works for 0-76 )
imul    bx,ax,8         ;bh=ax/32  (works for 0-8191 )
imul    bx,ax,7         ;bh=ax/37  (works for 0-109 )
imul    bx,ax,6         ;bh=ax/43  (works for 0-127 )
imul    bx,ax,4         ;bh=ax/64  (works for 0-16383 )
imul    bx,ax,2         ;bh=ax/128 (works for 0-32767 )    ```
15 Jun 2006, 14:16
saigon

Joined: 29 May 2006
Posts: 62
saigon 16 Jun 2006, 06:48
@ATV: Thanks for the list! Helps me a lot right now.

[finnish]
Hei! Miten menee?? Me taidetaan olla vähemmistössä tässä foorumissa jotka puhuu suomee, vai? Pidä hauskaa ja muista, että assembly rulaa!
PS: Mä en ole suomalainen, jos sitä tuumailit, osaan vaan liian monta kieltä laskeakseni, lol
[/finnish]
16 Jun 2006, 06:48
ATV

Joined: 31 Aug 2004
Posts: 109
Location: Finland
ATV 20 Jun 2006, 13:05
saigon, you are lucky one, I have lost lot of hairs even with english text.
20 Jun 2006, 13:05
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page Previous  1, 2

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum