160 likes | 250 Views
Number Conversions. Lecture L3.4. Number Conversions. ASCII Number String to Binary Conversion Binary Number to ASCII String Conversion. base. Number : Clear binary number buffer ( dnum ) Loop : get next digit While : digit is valid multiply dnum by base and add digit value
E N D
Number Conversions Lecture L3.4
Number Conversions • ASCII Number String to Binary Conversion • Binary Number to ASCII String Conversion
base 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
; input: A = ascii code of char ; output: if carry=0 A=valid hex value of char ; if carry=1 A=invalid char in current base digit pshb psha suba #$30 ;ascii codes < 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 9&A dgt1 cmpa base+1 ;digit must be bhs fail ; < 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
; Y -> ascii number string (kbuf) number pshx pshy ldx #base ;x -> base ldd #0 std 2,x ;clear dnum std 4,x ldy #kbuf ;y -> 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 base dnum 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
Number Conversions • ASCII Number String to Binary Conversion • Binary Number to ASCII String Conversion
ddiv = 32 / 16 = 32 quot, 16 rem numH:numL quotH:quotL remL = denom 0:numH quotH remH = denom remH:numL quotL remL = denom
; double division 32 / 16 = 32 16 rem ; numH:numL / denom = quotH:qoutL remL ; Y:D / X = Y:D rem X ; use EDIV twice Y:D / X = Y rem D ddiv pshd ;save numL tfr y,d ;d = numH ldy #0 ;0:numL / denom ediv ;y = quotH, d = remH bcc dd1 ;if div by 0 puld ldd #$FFFF ;quot = $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 ;remH:quotL/denom Y = quotL D = remL tfr d,x ;x = remL tfr y,d ;d = quotL puly ;y = quotH leas 2,sp ;fix stack rts
base dnum ; x -> ascii buffer sharp pshd ;save regs pshy pshx ;save ptr ldy dnum ldd dnum+2 ldx base jsr ddiv ;dnum/base rem in X sty dnum ; => dnum std dnum+2 tfr x,d ;b = rem cmpb #9 ;if rem > 9 bls shp1 addb #7 ; add 7 shp1 addb #$30 ;conv to ascii pulx ;restore ptr stab 1,-x ;store digit puly ;restore regs puld rts
base dnum ; input: x -> pad (ascii buffer) ; output: x -> first char in ascii string sharps bsr sharp ;do next digit ldd dnum ;repeat until bne sharps ; quot = 0 ldd dnum+2 bne sharps rts
base dnum ; 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 span db 0 base dw 10 dnum rmb 4 buff rmb 12 pad db 0
org $4000 main jsr query ;enter a number string jsr number ;convert it to binary ldx #dnum ldab #4 mn1 jsr out1byt ;display the hex value decb bne mn1 ldx #pad jsr sharps ;convert back to ascii string jsr outcrlf mn2 ldaa 1,x+ jsr outa ;display the ascii string cpx #pad blo mn2 jsr outcrlf swi