Two algorithms (expressed in C) to convert an 8-bit value,
n, to two hex characters c1 and c2 are:
c1 = ( n >> 4 ) & 0xf ; /* MS nybble */
if ( c1 <= 9 ) {
c1 = c1 + '0' ;
} else {
c1 = c1 - 10 + 'A' ;
}
c2 = n >> 4 ; /* LS nybble */
if ( c2 <= 9 ) {
c2 = c2 + '0' ;
} else {
c2 = c2 - 10 + 'A' ;
}
and:
char hexchars[16] = "0123456789ABCDEF" ;
c1 = hexchars [ ( n >> 4 ) & 0xf ] ;
c2 = hexchars [ n & 0xf ] ;
You might also want to look at the solutions for last year's Assignment 2 in the last
year's course. Note that the syntax for the shift instruction
given in the notes is wrong, it should be:
SHR AL,4 ; logical shift right by 4 bits