Page 1 of 1

Help with printing numbers

Posted: Sat Jul 20, 2002 3:03 pm
by Slasher
Hi, I've been reading the messages on this forum and am impressed with the solutions! So i'm turning to You guys for help!!! I just joined this forum and am also working on an operating system (nameless at the moment) and would like someone to explaind how i could print a number that spans two registers ie [dx:ax] or [edx:eax] for my lib ( i can print a number in a single reg) but have been unable to implement a routine to do this!Help please, it turning my mind to mush ???

Re:Help with printing numbers

Posted: Sat Jul 20, 2002 5:08 pm
by Schol-R-LEA
Decimal or Hex, signed or unsigned?

Re:Help with printing numbers

Posted: Sun Jul 21, 2002 3:38 am
by Pype.Clicker
In hex display, the problem is quite trivial : you just have to write the higher-word before you start writing the lower one :)
For base10 printing, this is much more complicated as any bit of your n-bits input can affect any digit of your m-digit output ... thus you have to work with repeated divisions by 10 ...

now, afaik, (l+b*h) MOD 10 == l MOD 10 + (b MOD 10 * h MOD 10) MOD 10

thus, provided that you know 2^32 (=b) MOD 10, for each digit you want to print, you can simply get the two corresponding digits of your long word (l MOD 10 and h MOD 10) and do the computation described above ...

and as (l+b*h) DIV 10 == l DIV 10 + (b * (h DIV 10)),
all you have to do for the reduction is to divide your two words (l and h) separately ...

This should be tested before applied (for instance, by trying it with b=256 and comparing the results of this technique against result obtained by direct division),
but theorically, it should give the result you expected...

Re:Help with printing numbers

Posted: Sun Jul 21, 2002 3:53 am
by Pype.Clicker
oops... the division part works only with floats :(, but the modulo formula is correct ...

correct division is :
low = (low div 10) + int ((hi mod 10) * (b/10))
hi = hi div 10

where DIV is integer division and / is exact division.
Note that the second term is tricky to compute as you need 2^32 / 10, but you can easily use a look-up table to do this and pre-compute 1*b/10 , 2*b/10 , ... , 9*b/10 and then just having

low = (low div 10 ) + remainder[hi mod 10]

have a nice print ;)

Re:Help with printing numbers

Posted: Sun Jul 21, 2002 12:29 pm
by Slasher
Thanks Men! I'll just work on it and see how i go! Can you please tell me how or where you came up with this formula?
:)

Re:Help with printing numbers

Posted: Tue Jul 23, 2002 2:14 pm
by Pype.Clicker
well, the modulo formula come simply from MOD N algebra course at university , and is based on some simple axioms like (a + b) mod N == ((a mod N) + (b mod N)) mod N...
the division formula is basically a reduction of a*N + b / x as you could do in secondary-school maths, but with a carry of the remainder of the high-term division to the low-term division (i got it by try-and-crash with some Basic code ;)