Hardware Reference
In-Depth Information
the HCS12 members that do not have expanded memory capability, that is, with
memory capacity no larger than 64 kB.
Example 4.2
Write a subroutine to convert an unsigned 16-bit binary number into an ASCII string that
represents the decimal value of the original binary number. The resultant string must be termi-
nated by a NULL character (the ASCII code of a NULL character is 0). For example, the binary
value 0101,1011,1010,0000 2 (or 23456 10 ) will be converted to $32 $33 $34 $35 $36 $00. The
16-bit number to be converted is held in D, and the pointer (address) to the buffer to hold the
resultant string is stored in Y.
Solution: The individual decimal digit can be separated by repeatedly performing divide by 10
to the given number and adding $30 to each remainder. Let ptr , quo , and rem represent the
pointer to the buffer to hold the resultant string, the quotient after the divide operation, and the
remainder of the divide operation, respectively. The repeated divide-by-10 operation will sepa-
rate the least significant digit first and the more significant digits later; we cannot save them
directly in the buffer pointed to by ptr. The solution is to push these digits into the stack in the
order that they are separated and then pop them out and save them in the buffer. The following
algorithm implements this idea:
Step 1
Push a 0 into the stack; quo
number to be converted; the zero (NULL character) pushed
into the stack is used to tell the program to stop popping from the stack.
Step 2
rem
quo % 10; quo
quo 4 10.
Step 3
Push the sum of $30 and rem into the stack.
Step 4
If (quo 55 0) go to step 6.
Step 5
Go to step 2.
Step 6
Pull a byte from the stack. Save the byte in the memory location pointed to by ptr.
Step 7
ptr
ptr 1 1;
Step 8
If the byte pulled out in step 6 is NULL, then stop; else go to Step 6.
The subroutine that implements this algorithm is as follows:
bin2dec
pshx
; save X in stack
movb
#0, 1, 2 SP
; push NULL character into stack
divloop
ldx
#10
; divide the number by 10
idiv
; “
addb
#$30
; convert the remainder to ASCII code
pshb
; push it into the stack
 
Search WWH ::




Custom Search