Download Print this page

ZiLOG Z80 Handbook page 228

Hide thumbs Also See for Z80:

Advertisement

LOOP LD A,(DE) GET SOURCE BYTE
SBC A,(HL) SOURCE-DEST-CARRY
LD (HL),A STORE RESULT
DEC HL DECREMENT DEST PNTR
DEC DE DECREMENT SOURCE PNTR
DEC C
JR NZ,LOOP GO IF NOT N SUBTRACTS
RET RETURN
ASCII TO BASE X CONVERSIONS
It is convenient to have subroutines that will take a string of
ASCII characters representing ASCII binary, decimal, hexadecimal,
or bcd digits and convert the ASCII characters to the proper number
of base representation. By employing these routines, data can be en-
tered from a keyboard and converted to variables to be used in exe-
cution of a program, or to instructions.
The general philosophy in the following routines will be that there
is a string of ASCII characters, starting at location (HL), that must
be converted to the proper internal format. ASCII characters repre-
senting binary numbers will be converted eight at a time since eight
bits can be held nicely in an 8-bit register. ASCII characters repre-
senting hexadecimal will be converted two at a time as two hex
digits may be held in eight bits; ASCII bcd characters will be con-
verted two at a time for the same reason. When the data represents
ASCII decimal digits, the question becomes one of a convenient
length for conversion. Eight bits will hold the decimal values of 0 to
255, and sixteen bits will hold up to 65,536. Neither range lends itself
to consecutive conversions of strings of data as in the binary, hexa-
decimal, and bcd cases. The decimal conversion will arbitrarily be
made of six ASCII characters, as this represents a reasonable range
of values to be input to a program. After each conversion, the pointer
to the string will be incremented by the number of characters con-
verted so that subsequent conversions can be made from the next
character in the string.
The following routine converts eight ASCII characters, assumed
to be ASCII ones or zeros, to an 8-bit binary value in the A register.
On input, HL points to the first character, and on output, HL points
to the ninth character in the string.
ASXBIN
LD
B,8
SET COUNT
LD
C,0
CLEAR RESULT
LOOP
SLA
LD
INC
C
A,(HL)
HL
SHIFT RESULT
GET ASCII
BUMP POINTER
SUB
30H
CONVERT TO 0 OR 1
237

Advertisement

loading
Need help?

Need help?

Do you have a question about the Z80 and is the answer not in the manual?

Questions and answers