Re: Hex to ascii



If you want any kind of fast binary to ascii conversion, you should take a close look at the code I posted here several years ago, and which AMD borrowed, without attribution :-(, for their optimization guide.

The key is that MUL is so much faster than DIV (and the related A* opcodes) that you can do quite a lot of funky stuff.

In the case of converting a binary value less than 100 to ascii, I'd look into using multiplication by ceil(65536/10):

; CX has binary value
mov ax,6554
mul cx
; At this point DX(DL) will contain the most significant digit!
mov al,10
mul dl
; If MUL is relatively slow, use LEA and/or shifts to do the same!

sub cx,ax
mov dh,cl
add dx,3030h

Yes, this is quite a bit larger than using AAM, but it is also 2 to 5 times faster on almost all x86 cpus.

In 32-bit mode the difference is even larger, the code AMD "borrowed" converts a full 32-bit unsigned integer to 10 decimal digits in less time than the cpu needs for a single DIV opcode! :-)

Terje

--
- <Terje.Mathisen@xxxxxxxxxxxxx>
"almost all programming can be viewed as an exercise in caching"

.



Relevant Pages

  • Re: Hex to ascii
    ... a close look at the code I posted here several years ago, and which AMD ... The key is that MUL is so much faster than DIV (and the related A* ... mov ax,6554 ... times faster on almost all x86 cpus. ...
    (comp.lang.asm.x86)
  • Re: Hex to ascii
    ... You used reciprocal MUL as a simple substitution for DIV, and you probably didn't do the full error analysis needed to prove how to get exact results either? ... repeated subtraction approach is the fastest I've created so far... ... The rest is mostly LEA, SUB, AND, ADD and SHR by constant. ... mov eax,2814749767 ...
    (comp.lang.asm.x86)
  • Re: Dividing 64-bit by a constant
    ... I need to divide a 64-bit unsigned integer by a constant, and truncate the result to 32 bits. ... In general, as long as the result has to fit in 32 bits, then you can take the 64-bit value, shift it down by ceil) bits, and then multiply by the reciprocal. ... After a back-multiplication and subtraction, you're probably done, you might have to make a small adjustment step if exact results are required, worst case just run another reciprocal mul: ... mov esi, ...
    (comp.lang.asm.x86)
  • Re: When random isnt random
    ... > slow down the Random function by making it use 64-bit seeds and results. ... mov EAX,XLO ... mul YLO ...
    (borland.public.delphi.language.objectpascal)