Page 271 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 271
248 Chapter 8 Programming in C and C++
a. Write a C procedure encode ( ) to convert the ASCII string to Huffman code, as
defined by the coding tree in Figure 8.5a, storing the code as a bit string, first bit as
most significant bit of first element of int code [16];.
b. Write a C procedure decode ( ) that decodes such a code in int code [ 16 ], using
the coding tree in Figure 8.5a, putting the ASCII string back as it was in char
string[4Q].
9. Repeat Problem 8 for the Huffman coding tree in Figure 8.5b.
10. Write an initialization and four shortest C procedures void pstop( int) push to
top, int pltop() pull from top, psbot(int) push to bottom, int plbot( ) pull
from bottom, of a ten-element 16-bit word deque. The deque's buffer is int
deque [ 10 ]. Use global int pointers, top and bottom. Use global char varaibles
for the size of the deque, size, and error flag errors which is to remain cleared if
there are no errors and to be 1 if there are underflow or overflow errors. Note that C
always initializes global variables to zero if not otherwise initialized. The procedures
should manage the deque correctly as long as errors is zero. Procedures pstop() and
psbot ( ) pass by value, and procedures pltop() and plbot ( ) pass by result.
11. Write a C procedure get (char *a, int i), whose body consists entirely of
embedded assembly language, which moves i bytes following address a into a char
global vector v, assuming v has a dimension larger than or equal to i. To achieve
speed, use the MOVE and DBNE instructions. The call to this procedure, get (s, n) ,
is implemented:
Idx s
pshx
Idx n
jsr get
leas 4,sp
12. Write a shortest C procedure hexString(unsigned int n, char *s) that runs in a target
machine to convert an unsigned integer n into printable characters in s that represent it
in hexadecimal so that sfOJ is the ASCII code for the 1000's hex digit, s[l] is the code
for the IQO's hex digit, and so on. Suppress leading Os by replacing them with blanks.
13. Write the shortest procedure int inhex() in C to input a four-digit hexadecimal
number from the keyboard (the letters A through F may be upper or lower case; typing
any character other than 0...9, a...f, A...F, or entering more than four hexadecimal digits
terminates the input and starts the conversion) and convert it to a binary number,
returning the converted binary number as an unsigned int. Although you do not have to
use a compiler and target machine to answer this problem, you can use it without
penalty, and it may help you get error-free results faster.