Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *=$0801
- BYTE $0B, $08, $0A, $00, $9E, $32, $30, $36, $31, $00, $00, $00
- ;---------------------------------
- Example
- lda #0
- sta sgn
- lda #$c0
- sta int16
- lda #$fd
- sta int16+1
- jsr _UtoA ; converts UInt16 to String
- jsr _trimnum ; trims leading spaces
- jsr _PrnNum
- lda #0
- sta sgn
- lda #$c0
- sta int16
- lda #$fd
- sta int16+1
- jsr _ItoA ; converts Int16 to String
- jsr _trimnum ; trims leading spaces
- jsr _PrnNum
- rts
- _PrnNum
- ; prints untrimmed string
- lda #<CnvStr
- ldy #>CnvStr
- jsr $ab1e
- lda #13
- jsr $ffd2
- ; prints trimmed string
- lda #<CnvTrm
- ldy #>CnvTrm
- jsr $ab1e
- lda #13
- jsr $ffd2
- rts
- ;-------------------------------
- ; Converts a 16bit signed integer into string
- ;-------------------------------
- ;
- ; Call with 16 bit number in int16
- ;
- ;-------------------------------
- _ItoA
- lda int16+1
- bpl _pos
- _neg clc
- lda int16
- eor #$ff
- adc #1
- sta int16
- lda int16+1
- eor #$ff
- adc #0
- sta int16+1
- lda #'-'
- sta sgn
- _pos jsr _UtoA
- lda sgn
- beq _enditoa
- dex
- sta CnvStr,x
- _enditoa
- rts
- ;-------------------------------
- ; Converts a 16bit unsigned integer into string
- ;-------------------------------
- ;
- ; Call with 16 bit number in int16
- ;
- ;-------------------------------
- _UtoA
- jsr BinBcd16
- ;lda bcd+2
- and #$0f
- ora #$30
- sta CnvStr+1
- lda bcd+1
- and #$0f
- ora #$30
- sta CnvStr+3
- lda bcd+1
- lsr
- lsr
- lsr
- lsr
- ora #$30
- sta CnvStr+2
- lda bcd+0
- and #$0f
- ora #$30
- sta CnvStr+5
- lda bcd+0
- lsr
- lsr
- lsr
- lsr
- ora #$30
- sta CnvStr+4
- lda #$20
- sta CnvStr+0
- ; rts ; decomment to avoid stripping leading 0s
- ldx #1 ; remove 0s at beginning
- _rem0 lda CnvStr,x
- cmp #$30
- bne _rts
- lda #$20 ; put a space instead
- sta CnvStr,x
- inx
- cpx #$5
- bne _rem0
- _rts rts
- _trimnum
- cpx #0
- beq _rts
- ldy #0
- _trmlp lda CnvStr,x
- sta CnvTrm,y
- beq _rts
- inx
- iny
- jmp _trmlp
- bcd = $61 ; system Fac, 3 bytes
- int16 = $64 ; system Fac, 2 bytes
- sgn byte 0
- CnvStr byte 0,0,0,0,0,0,0 ; 7 bytes: sgn + 5bytes + '\0'
- CnvTrm byte 0,0,0,0,0,0,0
- ;-------------------------------
- ; Converts a 16bit number to BCD
- ;-------------------------------
- BINBCD16
- SED ; Switch to decimal mode 2
- LDA #0 ; Ensure the result is clear 2
- STA bcd+0; 3
- STA bcd+1; 3
- STA bcd+2; 3 13
- CBIT1 ASL int16 ; Shift out one bit 5
- ROL int16+1 ; 5
- ; LDA bcd+0 ; And add into result
- ADC bcd+0 ; 3
- STA bcd+0 ; 3
- ASL int16 ; Shift out one bit 5
- ROL int16+1 ; 5
- ADC bcd+0 ; 3
- STA bcd+0 ; 3
- ASL int16 ; Shift out one bit 5
- ROL int16+1 ; 5
- ADC bcd+0 ; 3
- STA bcd+0 ; 3
- ASL int16 ; Shift out one bit 5
- ROL int16+1 ; 5
- ADC bcd+0 ; 3
- STA bcd+0 ; 3
- ASL int16 ; Shift out one bit 5
- ROL int16+1 ; 5
- ADC bcd+0 ; 3
- STA bcd+0 ; 3
- ASL int16 ; Shift out one bit 5
- ROL int16+1 ; 5
- ADC bcd+0 ; 3
- STA bcd+0 ; 3 96
- LDX #7; 2 2
- CBIT7 ASL int16 ; Shift out one bit 5
- ROL int16+1 ; 5
- LDA bcd+0 ; And add into result 3
- ADC bcd+0 ; 3
- STA bcd+0 ; 3
- LDA bcd+1 ; propagating any carry 3
- ADC bcd+1 ; 3
- STA bcd+1 ; 3
- DEX ; And repeat for next bit 2
- BNE CBIT7 ; 3 33*7-1=230
- LDX #3; 2 2
- CBIT13 ASL int16 ; Shift out one bit 5
- ROL int16+1 ; 5
- LDA bcd+0 ; And add into result 3
- ADC bcd+0 ; 3
- STA bcd+0 ; 3
- LDA bcd+1 ; propagating any carry 3
- ADC bcd+1 ; 3
- STA bcd+1 ; 3
- LDA bcd+2 ; ... thru whole result 3
- ADC bcd+2 ; 3
- STA bcd+2 ; 3
- DEX ; And repeat for next bit 2
- BNE CBIT13 ; 3 42*3-1=125
- CLD ; Back to binary 2 2; tot 470
- rts ; All Done.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement