Post Reply 
(16C) HEX2ASC/ASC2HEX Conversion Routines
04-21-2019, 03:55 AM (This post was last modified: 04-21-2019 05:38 PM by mfleming.)
Post: #1
(16C) HEX2ASC/ASC2HEX Conversion Routines
These two conversion routines were common in the early 80's when writing code in assembly language for the early 8-bit microprocessors. You might communicate via a serial port to peek and poke values in memory when debugging, so you needed a way to translate between an 8-bit value in memory and the ASCII representation of the value on the console.

These two routines are complementary. HEX2ASC takes an 8-bit value in X and returns the low and high ASCII representation of the value in X and Y. ASC2HEX does the opposite, taking two ASCII characters in X and Y and returning an 8-bit value in X. The two routines are listed separately but were entered together as can be seen in the line numbering.


Conditions:
HEX mode, word size 8 bits

Usage: HEX2ASC
Input:
X - 8 bit value
Output:
Y - ASCII character for upper nibble
X - ASCII character for lower nibble

Code:
001  LBL A        | 43 22 A      HEX2ASC
002  STO 0        |    44 0
003  F            |       F
004  0            |       0
005  AND          |    42 20     Mask high nibble
006  4            |       4
007  RLn          |    42 E
008  GSB 0        |    21 0      Convert to ASCII
009  RCL 0        |    45 0
010  F            |       F      Mask low nibble
011  AND          |    42 20
012  GSB 0        |    21 0      Convert to ASCII
013  RTN          |    43 21
014  LBL 0        | 43 22 0      Convert nibble to ASCII
015  A            |       A
016  x<=y         |    43 1      Value A - F?
017  GTO 1        |    22 1      Yes
018  CLX          |    43 35     Disable stack lift
019  3            |       3      ASCII "0"
020  0            |       0
021  +            |       40     Add to value 0 - 9
022  RTN          |    43 21
023  LBL 1        | 43 22 1      Nibble was A - F
024  CLX          |    43 35     Disable stack lift
025  3            |       3      Offset to ASCII "A"
026  7            |       7
027  +            |       40
028  RTN          |    43 21

Usage: ASC2HEX
Input:
Y - ASCII character for upper nibble
X - ASCII character for lower nibble
Output:
X - 8 bit value

Code:
029  LBL B        | 43 22 B      ASC2HEX
030  GSB 0        |    21 0      Convert low ASCII to nibble
031  x<>y         |       34
032  GSB 0        |    21 0      Convert high ASCII to nibble
033  4            |       4
034  RLn          |    42 E      Shift result to high nibble
035  OR           |    42 40     Combine nibbles to a byte result
036  RTN          |    43 21
037  LBL 0        | 43 22 0      Convert ASCII to nibble 0 - F
038  STO 0        |    44 0
039  F            |       F
040  0            |       0
041  AND          |    42 20     Mask upper ASCII
042  3            |       3
043  0            |       0
044  x=y          |    43 49     Character "0" - "9"?
045  GTO 1        |    22 1      Yes
046  CLX          |    43 35     Remove 30h from stack
047  Rv           |       33
048  CLX          |    43 35     Disable stack lift
049  RCL 0        |    45 0
050  F            |       F      Low ASCII nibble
051  AND          |    42 20
052  9            |       9      Convert to "A" - "F"
053  +            |       40
054  RTN          |    43 21
055  LBL 1        | 43 22 1      Character was "0" - "9"
056  CLX          |    43 35     Remove 30h from stack
057  Rv           |       33
058  CLX          |    43 35     Disable stack lift
059  RCL 0        |    45 0
060  F            |       F
061  AND          |    42 20
062  RTN          |    43 21

Examples:
5Ch
GSB A
Y - 35h
X - 43h

x<>y
GSB B
X - C5h

Edit: Word size must be 8 bits due to Rotate-Left (RLn) instruction.

Remember kids, "In a democracy, you get the government you deserve."
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)