Page 130 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 130
4.6 Summary 107
* Get hexadecimal value
* entry: X->first character of hex number
* exit: A:value, X->next character after hex number
* saved: B,Y
*
GETHEX: BSR GH1 ; convert ascii character to a nibble
LSLA ; move to high nibble
LSLA
LSLA
LSLA
PSHA ; save on stack
BSR GH1 ; convert ascii character to a nibble
ORAA 1, sp+ ; pop and combine
RTS
*
GH1: LDAA 1, x+ ; get next symbol
CMPA #'9'
BLS GH2
SUBA #7
GH2 : SUBA #' 0 ' ; subtract ascii 0
RTS
Figure 421. Convert ASCII Hex String to a Binary Number
The reader should observe that this subroutine, PASS2, is broken into subroutines
GETOPCD, GETHEX, and FINLBL. Each of these subroutines is more easily understood
and debugged than a long program PASS2 that doesn't use subroutines. Each subroutine
corresponds to an easily understood operation, which is described in the subroutine's
header. This renders the subroutine PASS2 much easier to comprehend.
The contents of the vector OBJECT will be downloaded into the target machine and
executed there. The assembler permits the programmer the ability to think and code at a
higher level, not worrying about the low-level encoding of the machine code.
The reader should observe the following points from the above example. First, the
two-pass assembler will determine where the labels are in the first pass. Thus, labels that
are lower in the source code than the instructions that use these labels will be known in
the second pass when the instruction machine code is generated. Second, these
subroutines further provide many examples of techniques used to convert ASCII to
hexadecimal, used to search for matching characters, and used to insert data into a vector.
4.6 Summary
In this chapter, we learned that an assembler can help you write much larger programs
than you would be able to write by hand coding in machine code. Not only are the
mnemonics for the instructions converted into instruction opcode bytes, but also
symbolic addresses are converted into memory addresses. However, every new powerful