Best integer to string routines
From: Beth (BethStone21_at_hotmail.NOSPICEDHAM.com)
Date: 02/28/04
- Next message: Beth: "Re: Editors"
- Previous message: The Half A Wannabee: "Cost of calling a standard library function"
- Next in thread: Herbert Kleebauer: "Re: Best integer to string routines"
- Reply: Herbert Kleebauer: "Re: Best integer to string routines"
- Reply: Herman Dullink: "Re: Best integer to string routines"
- Reply: JohnFound: "Re: Best integer to string routines"
- Reply: RoWsRaIrTiEo : "Re: Best integer to string routines"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 28 Feb 2004 09:01:59 -0000
Hi,
It's that good old problem rearing its ugly head once
more...namely, some code that'll do some of the standard conversion
stuff...e.g. print a byte / word / dword as ASCII binary / octal / hex
/ decimal on the screen...
Who's got the most optimal code for this kind of thing? "Optimal" can
mean _either_ "speed" or "size" (or, better, _both_ ;), as long as we
know what it is you're striving to achieve...and regards the output
ASCII string, it can be in any format - zero-terminated, leading
zeroes or not, etc. - so long as, again, it's made clear what kind of
output you're going for...
So, the routines of interest - listing out all the possibilities -
would be:
1. Convert byte value to ASCII binary string
2. Convert byte value to ASCII hex string
3. Convert byte value to ASCII decimal string
4. Convert word value to ASCII binary string
5. Convert word value to ASCII hex string
6. Convert word value to ASCII decimal string
7. Convert dword value to ASCII binary string
8. Convert dword value to ASCII hex string
9. Convert dword value to ASCII decimal string
The values will start out in a register and you can either print each
character as you're going or load it all into a string and then print
that or push things to the stack and then pop them off to
print...whatever...
_Any_ method is acceptable here because the whole point is, basically,
to try to discover _which_ particular method of approaching this
problem ends up with the smallest and / or fastest code for doing
it...
I do remember someone posting up some code with some AAM, AAD, DAA,
etc. kind of "trick" which was short and fast but I've forgotten how
it worked now...it's also acceptable for the hexadecimal stuff to use
a look-up table for the digits ("0123456789ABCDEF", to avoid some
"exception" code for the different numeric / alphabetic characters :)
or perhaps your method already accounts for this that it doesn't
matter...
Again, whatever...the point here is just to gather up some different
methods of approaching this pretty typical problem - getting a value
onto the screen for the user to be able to read it - for the purposes
of comparing them to discover which is the best - either size or speed
but, of course, preferably _both_ - for dealing with this problem...
For example, one method you could use to print a hex string might be
like this:
----------- 8< --------------
Digits db "0123456789ABCDEF"
; eax = value to be printed
;
HexPrint: mov cx, 8
NextDigit: rol eax, 4
mov ebx, eax
and ebx, 000Fh
mov al, [ offset Digits + ebx ]
int 29h
dec cx
jne NextDigit
ret
----------- >8 --------------
Which makes use of rotating a value around in a register to print the
digits in the right order and a simple look-up table is used to grab
the digits rather than have some "if < 9 then add '0' otherwise add
'A'" style of code...
Although, this code isn't particularly the fastest or the smallest,
I'd reckon, because 16 bytes is being used up for the look-up table
thingy...so, if you know some "trick" with DAA or whatever which can
do the same thing in less space, that's exactly the kind of code to
post up...then, if someone can do better still than that, post that up
too...until, Hopefully, we've got a bunch of "optimal" routines for
this (and if someone else's code beats yours then, hey, you can
"borrow" their method :)...
Beth :)
- Next message: Beth: "Re: Editors"
- Previous message: The Half A Wannabee: "Cost of calling a standard library function"
- Next in thread: Herbert Kleebauer: "Re: Best integer to string routines"
- Reply: Herbert Kleebauer: "Re: Best integer to string routines"
- Reply: Herman Dullink: "Re: Best integer to string routines"
- Reply: JohnFound: "Re: Best integer to string routines"
- Reply: RoWsRaIrTiEo : "Re: Best integer to string routines"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]