Page 257 - Programming Microcontrollers in C
P. 257
242 Chapter 5 Programming Large 8-Bit Systems
{
if((mask & M[i])==0)
j++; /* use next node entry if bit is zero */
else
j+=Node[j];/*jump to designated node when
one */
if(Node[j]<0)
{ /* if a printable, send it out */
putchar(k=Node[l]&0x7f);
j=0; /* also go to the root node */
}
if((mask>>=1)==0) /* if the mask is zero,turn
on MSB */
{
mask = 0x80;
i++; /* and get the next byte from message */
}
}
}
Listing 5-1: Huffman Decode Function
The next part of the program is the Huffman tree needed to decode
the data. This tree was constructed to decode data specifically for the
nine messages of this problem. You will note that each byte in the
table is a node. Intermixed in the table are numbers that dictate jumps
to the next node depending on whether the incoming bit is a zero or
a one. The leaves each contain a negative number that is generated
by the inclusive OR of the character to be printed and the number
0x80.
The messages are sent to the function decode as a pointer to a bit
array. The data must be examined one bit at a time to implement the
decoding. The bit selection is accomplished by ANDing the data in
the incoming message with the contents of the variable mask. Mask
is initially set to 0x80, so the most significant bit of the first byte of
the incoming data is selected. Later in the routine, the value of mask
will be shifted right to select other bits in the incoming sequence.
The variable k is loaded with the character to be output during the
program. The linefeed character ‘\n’ was used to determine the end
of message, so the decoding sequence will be executed until the