flat assembler
Message board for the users of flat assembler.
Index
> DOS > shortest way to display decimal number ? Goto page Previous 1, 2 
Author 

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 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:
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 12 Jun 2006, 19:29
In context of the link I gave several posts above: is there anyone but me interested in padic calculations here?


12 Jun 2006, 19:29 

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

12 Jun 2006, 19:36 

Just me 12 Jun 2006, 20:35


12 Jun 2006, 20:35 

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 16bit number with the max value+1 (which is 65536) what does happen? simple: the multiplicand gets placed in the upper 16bits, which when using 16bit 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 Code: x*65536  y  Ok, what can we observe? Simple: (x/y)*65536 just places the result (x/y) in dx (we're in 16bit). 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 32bit or any other size. Just replace 65536 with the max number+1 (for example, in 32bit it is 0x100000000 which is 0xFFFFFFFF+1). The result is, in 32bit, 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 14 Jun 2006, 14:22
Thanks The_Grey_Beast


14 Jun 2006, 14:22 

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 magicnumber 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 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 0511) imul bx,ax,86 ;bh=ax/3 (works for 0127 ) imul bx,ax,64 ;bh=ax/4 (works for 01023 ) imul bx,ax,52 ;bh=ax/5 (works for 063 ) imul bx,ax,43 ;bh=ax/6 (works for 0130 ) imul bx,ax,37 ;bh=ax/7 (works for 089 ) imul bx,ax,32 ;bh=ax/8 (works for 02047 ) imul bx,ax,29 ;bh=ax/9 (works for 052 ) imul bx,ax,26 ;bh=ax/10 (works for 068 ) imul bx,ax,24 ;bh=ax/11 (works for 031 ) imul bx,ax,22 ;bh=ax/12 (works for 034 ) imul bx,ax,20 ;bh=ax/13 (works for 063 ) imul bx,ax,16 ;bh=ax/16 (works for 04095 ) imul bx,ax,13 ;bh=ax/20 (works for 078 ) imul bx,ax,10 ;bh=ax/26 (works for 076 ) imul bx,ax,8 ;bh=ax/32 (works for 08191 ) imul bx,ax,7 ;bh=ax/37 (works for 0109 ) imul bx,ax,6 ;bh=ax/43 (works for 0127 ) imul bx,ax,4 ;bh=ax/64 (works for 016383 ) imul bx,ax,2 ;bh=ax/128 (works for 032767 ) 

15 Jun 2006, 14:16 

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

Goto page Previous 1, 2 < Last Thread  Next Thread > 
Forum Rules:

Copyright © 19992024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.