Title: Number Conversions
1Number Conversions
2Number Conversions
- ASCII Number String to Binary Conversion
- Binary Number to ASCII String Conversion
3base
Number Clear binary number buffer (dnum) Loop
get next digit While digit is valid multiply
dnum by base and add digit value store result
in dnum Repeat
dnum
4 input A ascii code of char output if
carry0 Avalid hex value of char if
carry1 Ainvalid char in current
base digit pshb psha suba 30 ascii codes lt
30 blo fail are invalid cmpa 9 char
between bls dgt1 9 and A cmpa 17 are
invalid blo fail fill gap suba 7 between
9A dgt1 cmpa base1 digit must be bhs fail
lt base andcc FE clear carry
(valid) pulb pop old A pulb restore
B rts fail pula restore A pulb restore
B orcc 01 set carry (invalid) rts
5 Y -gt ascii number string (kbuf) number pshx ps
hy ldx base x -gt base ldd 0 std 2,x clear
dnum std 4,x ldy kbuf y -gt kbuf num1 ldaa 1,y
get next digit jsr digit conv to
value bcs num2 jsr dumul mult dnum by
base adda 5,x add digit value staa 5,x ldaa 4,
x adca 0 staa 4,x ldaa 3,x adca 0 staa 3,x
ldaa 2,x adca 0 staa 2,x bra num1 do until
invalid digit num2 puly pulx rts
Number Clear binary number buffer (dnum) Loop
get next digit While digit is valid multiply
dnum by base and add digit value store result
in dnum Repeat
Y
kbuf
31
32
33
34
35
36
6Number Conversions
- ASCII Number String to Binary Conversion
- Binary Number to ASCII String Conversion
7(No Transcript)
8(No Transcript)
9ddiv 32 / 16 32 quot, 16 rem
numHnumL
quotHquotL
remL
denom
0numH
quotH
remH
denom
remHnumL
quotL
remL
denom
10 double division 32 / 16 32 16
rem numHnumL / denom quotHqoutL remL YD
/ X YD rem X use EDIV twice YD / X Y
rem D ddiv pshd save numL tfr y,d d
numH ldy 0 0numL / denom ediv y quotH, d
remH bcc dd1 if div by 0 puld ldd FFFF q
uot FFFFFFFF tfr d,y tfr d,x rem
FFFF rts dd1 sty 2,-sp save quotH on
stack tfr d,y y remH ldd 2,sp d
numL ediv remHquotL/denom Y quotL D
remL tfr d,x x remL tfr y,d d
quotL puly y quotH leas 2,sp fix stack rts
11 x -gt ascii buffer sharp pshd save
regs pshy pshx save ptr ldy dnum ldd dnum2
ldx base jsr ddiv dnum/base rem in
X sty dnum gt dnum std dnum2 tfr x,d b
rem cmpb 9 if rem gt 9 bls shp1 addb 7
add 7 shp1 addb 30 conv to ascii pulx rest
ore ptr stab 1,-x store digit puly restore
regs puld rts
12(No Transcript)
13 input x -gt pad (ascii buffer) output x -gt
first char in ascii string sharps bsr sharp do
next digit ldd dnum repeat until bne sharps
quot 0 ldd dnum2 bne sharps rts
14 numconv.asm convert ascii string to 32-bit
number at dnum then convert back to ascii
string org 800 outa equ FF4F output ascii
char in A out1byt equ FF52 display hex value of
byte at X outcrlf equ FF5B carriage return,
line feed to term inchar equ FF64 wait for and
input a char from term kbuf rmb 16 bufend db 0 spa
n db 0 base dw 10 dnum rmb 4 buff rmb 12 pad db 0
15 org 4000 main jsr query enter a number
string jsr number convert it to
binary ldx dnum ldab 4 mn1 jsr out1byt displa
y the hex value decb bne mn1 ldx pad jsr shar
ps convert back to ascii string jsr outcrlf mn2
ldaa 1,x jsr outa display the ascii
string cpx pad blo mn2 jsr outcrlf swi
16(No Transcript)