Hardware Reference
In-Depth Information
Example 4.4
▼
Write a subroutine that can convert a BCD ASCII string to a binary number and leave the result
in double accumulator D. The ASCII string represents a number in the range of 22
15
,2
15
21. A
pointer to the string is passed to this subroutine in X.
Solution:
The subroutine needs to perform error checking. If there is any illegal character, the
error flag will be set to 1. The subroutine will return the error flag to the caller using the CY
flag in the CCR register. Let
in_ptr
,
sign
,
error
, and
number
represent the pointer to the BCD
string, sign flag, error flag, and the number represented by the BCD string. The algorithm of the
subroutine is as follows:
Step 1
sign
←
0; error
←
0; number
←
0;
Step 2
If the character pointed to by in_ptr
is the minus sign, then
sign
←
1; in_ptr
←
in_ptr
1
1
Step 3
If the character pointed to by in_ptr
is the NULL character,
then go to step 4.
else if the character is not a BCD digit (i.e., m[in_ptr] > $39 or m[in_ptr] < $30), then:
error
←
1;
go to step 4;
else:
number
←
number × 10 + m[in_ptr]
2
$30;
in_ptr
←
in_ptr
1
1;
go to step 3;
Step 4
If sign
5
1 and error
5
0, then
number
←
two's complement of number;
else
stop;
In this example, the subroutine allocates local variables in the stack as shown in
Figure 4.6.
SP
Present digit value
The binary value of the BCD string
Sign of the number
Error flag
1
dummy
pdVal
2
val
4
sign
5
err
Y
Ret_addr
Figure 4.6
■
Stack frame of Example 4.4
Search WWH ::
Custom Search