Hardware Reference
In-Depth Information
The conversion subroutine and its test program are as follows:
#include “c:\miniide\hcs12.inc”
minus
equ
$2D
; ASCII code of minus sign
dummy
equ
0
; offset of dummy to hold a 0
pdVal
equ
1
; offset of present digit value from SP in stack
val
equ
2
; offset of the 2-byte binary value of the BCD string
; from SP in stack
sign
equ
4
; offset of the sign from SP in stack
err
equ
5
; offset of error flag from SP in stack
locVar
equ
6
org
$1000
StrBuf
dc.b
2 9889”,0
; input ASCII to be converted
result
ds.w
1
org
$1500
start
lds
#$1500
ldx
#StrBuf
jsr
bcd2bin
std
result
swi
; *********************************************************************************
; This subroutine converts a BCD string into its equivalent binary value and also uses a CY
; flag to indicate error condition. The CY flag is set to 1 to indicate error.
; *********************************************************************************
bcd2bin pshy
leas
2 locVar,SP
; allocate 4 bytes for local variables
movw
#0,val,SP
; initialize accumulated value to 0
movb
#0,dummy,SP
movb
#0,sign,SP
; initialize sign to positive
movb
#0,err,SP
; clear error flag initially
ldaa
0,x
; check the first character
cmpa
#minus
; is the first character a minus sign?
bne
GEZero
; branch if not minus
movb
#1,sign,SP
; set the sign to 1
inx
; move the pointer
GEZero
ldab
1,x 1
; is the current character a NULL character?
lbeq
done
; yes, we reach the end of the string
cmpb
#$30
; is the character not between 0 to 9?
blo
inErr
; ''
cmpb
#$39
; ''
bhi
inErr
; ''
subb
#$30
; convert to the BCD digit value
stab
pdVal,SP
; save the current digit value
ldd
val,SP
; get the accumulated value
ldy
#10
emul
; perform 16-bit by 16-bit multiplication
addd
dummy,SP
; add the current digit value
std
val,SP
; save the sum
bra
GEZero
inErr
movb
#1,err,SP
; set the error flag to indicate error
bra
chkout
Search WWH ::




Custom Search