Fast and Compact ASCII-binary Utilities - Keil Shell V1.0
Submitted By:
Graham Cole
Rating:
(Not rated) (
Rate It)
Fast ASCII Utilities
These files include functions for converting between ASCII characters and binary values.
The functions are written in a Keil "shell" and are easily incorporated into a Keil C51 based project. However, all the functionality is expressed in assembler and the functions may easily be converted to work with any assembly language environment.
A Keil C51 test harness is included.
Examination of the code should be sufficient to understand how these functions work.
The following functions all convert a binary value in the range 0...15 to the equivalent ASCII hexadecimal character:
* fast_binary_to_uppercase_ascii_hexadcimal()
* fast_binary_to_eithercase_ascii_hexadcimal()
* compact_binary_to_uppercase_ascii_hexadcimal()
* compact_binary_to_lowercase_ascii_hexadcimal()
* compact_binary_to_eithercase_ascii_hexadcimal()
The following functions all convert an ASCII hexadecimal character to a binary value in the range 0...15:
* fast_ascii_hexadecimal_to_binary()
fast_binary_to_uppercase_ascii_hexadcimal()
This function illustrates what is probably the most obvious method of conversion - a table look-up. This example uses a look-up table that is stored locally and can be addressed relative to the program counter using the instruction MOVC A,@A+PC.
The function is easily modified to generate lowercase ASCII characters: just change the table.
Of course, it is perfectly possible to have a look-up table anywhere in memory and to access it using the instruction MOVC A,@A+DPTR. However, there is potentially a significant disadvantage needing to overwrite the DPTR because it is likely that the calling function will be using it.
fast_binary_to_eithercase_ascii_hexadcimal()
This function illustrates how it is possible to select the case of the hexadecimal characters in the range 'A' to 'F' to be returned. The conversion from uppercase to lowercase simply requires setting bit 5 of the ASCII value. This bit is already set for ASCII characters '0' to '9' but setting it has the effect of converting uppercase letters in the range 'A' to 'F' to lowercase.
compact_binary_to_uppercase_ascii_hexadcimal()
This function illustrates a compact function for converting a value to an ASCII character. It the function is compact because it does not require a look-up table; yet it is only very slightly slower that the look-up table method.
The trick of using a combination of ADD and DA instructions to perform this conversion is an old one that I believe predates the 8051 and, as far as I know, the original author is lost to history.
compact_binary_to_lowercase_ascii_hexadcimal()
and
compact_binary_to_eithercase_ascii_hexadcimal()
These functions are simply variations on compact_binary_to_uppercase_ascii_hexadcimal() illustrating conversion from uppercase to lowercase. They should be reasonably self-explanatory.
fast_ascii_hexadecimal_to_binary()
This function illustrates a quick method of converting an ASCII character known to be in the range '0'-'9' or 'A'-'F' or 'a'-'f' to the equivalent binary value. Non-decimal digits are easily identified because bit 6 of the ASCII code is set for all characters in the range 'A'-'F' or 'a'-'f'. The rest is very simple!
This is a much quicker method of conversion that the more conventional application of a series of range checks to determine what value should be subtracted from an ASCII code in order to determine its binary equivalent.