Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [115]
ASCII is a 7-bit code using binary codes 0000000 through 1111111, which are hexadecimal codes 00h through 7Fh. Let's take a look at the ASCII codes, but let's not start at the very beginning because the first 32 codes are conceptually a bit more difficult than the rest of the codes. I'll begin with the second batch of 32 codes, which includes punctuation and the ten numeric digits. This table shows the hexadecimal code and the character that corresponds to that code:
Hex Code
ASCII Character
Hex Code
ASCII Character
20
Space
30
0
21
!
31
1
22
"
32
2
23
#
33
3
24
$
34
4
25
%
35
5
26
&
36
6
27
'
37
7
28
(
38
8
29
)
39
9
2A
*
3A
:
2B
+
3B
;
2C
,
3C
<
2D
-
3D
=
2E
.
3E
>
2F
/
3F
?
Notice that 20h is the space character that divides words and sentences.
The next 32 codes include the uppercase letters and some additional punctuation. Aside from the @ sign and the underscore, these punctuation symbols aren't normally found on typewriters. They're all now standard on computer keyboards.
Hex Code
ASCII Character
Hex Code
ASCII Character
40
@
50
P
41
A
51
Q
42
B
52
R
43
C
53
S
44
D
54
T
45
E
55
U
46
F
56
V
47
G
57
W
48
H
58
X
49
I
59
Y
4A
J
5A
Z
4B
K
5B
[
4C
L
5C
\
4D
M
5D
]
4E
N
5E
^
4F
O
5F
_
The next 32 characters include all the lowercase letters and some additional punctuation, again not often found on typewriters:
Hex Code
ASCII Character
Hex Code
ASCII Character
60
`
70
p
61
a
71
q
62
b
72
r
63
c
73
s
64
d
74
t
65
e
75
u
66
f
76
v
67
g
77
w
68
h
78
x
69
i
79
y
6A
j
7A
z
6B
k
7B
{
6C
l
7C
|
6D
m
7D
}
6E
n
7E
~
6F
o
Notice that this table is missing the last character corresponding to code 7Fh. If you're keeping count, the three tables here show a total of 95 characters. Because ASCII is a 7-bit code, 128 codes are possible, so 33 more codes should be available. I'll get to those shortly.
The text string
Hello, you!
can be represented in ASCII using the hexadecimal codes
48 65 6C 6C 6F 2C 20 79 6F 75 21
Notice the comma (code 2C), the space (code 20) and the exclamation point (code 21) as well as the codes for the letters. Here's another short sentence:
I am 12 years old.
and its ASCII representation:
49 20 61 6D 20 31 32 20 79 65 61 72 73 20 6F 6C 64 2E
Notice that the number 12 in this sentence is represented by the hexadecimal numbers 31h and 32h, which are the ASCII codes for the digits 1 and 2. When the number 12 is part of a text stream, it should not be represented by the hexadecimal codes 01h and 02h, or the BCD code 12h, or the hexadecimal code 0Ch. These other codes all mean something else in ASCII.
A particular uppercase letter in ASCII differs from its lowercase counterpart by 20h. This fact makes it fairly easy to write some code that (for example) capitalizes a string of text. Suppose a certain area of memory contains a text string, one character per byte. The following 8080 subroutine assumes that the address of the first character in the text string is stored in register HL. Register C contains the length of that text string, which is the number of characters:
Capitalize: MOV A,C ; C = number of characters left
CPI A,00h ; Compare with 0
JZ AllDone ; If C is 0, we're finished
MOV A,[HL] ; Get the next character
CPI A,61h ; Check if it's less than 'a'
JC SkipIt ; If so, ignore it
CPI A,7Bh ; Check if it's greater than 'z'
JNC SkipIt ; If so, ignore it
SBI A,20h ; It's lowercase, so subtract 20h
MOV [HL],A ; Store the character
SkipIt: INX HL ; Increment the text address
DCR C ; Decrement the counter
JMP Capitalize ; Go back to the top
AllDone: RET
The statement that subtracts 20h from the lowercase letter to convert it to uppercase can be replaced with this:
ANI