What is Allison's Algorithm and how does it aid in converting hex digits into ASCII characters?
;algorithm (found online)
ADD AL, 90h
DAA
ADC AL, 40h
DAA
http://computer-programming-forum.com/46-asm/f85ec43b17441011.htm
What is Allison's Algorithm and how does it aid in converting hex digits into ASCII characters?
;algorithm (found online)
ADD AL, 90h
DAA
ADC AL, 40h
DAA
http://computer-programming-forum.com/46-asm/f85ec43b17441011.htm
First, we have to understand the inputs & outputs to this sequence.
A hex digit is simply a 4-bit value, here provide in the AL register. Thus, we're trying to convert a digit in the range 0x0 to 0xF into the corresponding ASCII character for hex digits; the output is also provided AL.
To convert these we need the following mapping, which is derived directly from the ASCII chart:
input | output
hex value | ascii char hex value
0x00 | '0' 0x30
0x01 | '1' 0x31
... | ... ...
0x09 | '9' 0x39
0x0A | 'A' 0x41
... | ... ...
0x0F | 'F' 0x46
Note that in the ASCII encoding, '0' thru '9' are consecutive, and 'A' thru 'F' are also; however, there is a break in the run (between '9' and 'A'), so some conditional logic is required.
The code you're showing is better (i.e. shorter) than using conditional tests & branches to perform the same mapping.
As an exercise for the reader try putting 0x9 into AL and simulate the execution of the sequence using the following references (then try with 0xA, or 0xF in AL).