User Tools

Site Tools


base:int16_and_uint16_conversion_to_string

This is an old revision of the document!


Conversion of a number in int16 (lo/hi) into a string in CnvStr.
_ItoA converts a signed number
_UtoA converts an unsigned number
The conversion is obtained via a BCD intermediate value.

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 signed integer into string
;-------------------------------
; by Verz - Jul2019
;-------------------------------
;
;       Call with 16 bit number in int16
;       result in CnvStr
;-------------------------------

_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
;-------------------------------
; by Verz - Jul2019
;-------------------------------
;
;       Call with 16 bit number in lobyte/hibyte
;       result in CnvStr
;-------------------------------


_UtoA
        jsr BINBCD16_2
        ;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

;-------------------------------
; Remove leading spaces from int string
;-------------------------------
; by Verz - Jul2019
;-------------------------------
;       use right after _ItoA or _UtoA
;       needs the pos of the first digit in .X 
;       result in CnvTrm
;-------------------------------
_TrimNum
        ldy #0
        cpx #0
        beq _rts
_trmlp  lda CnvStr,x
        sta CnvTrm,y
        beq _rts
        inx
        iny
        jmp _trmlp




;-------------------------------
; Converts a 16bit number in 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.                             

usage example:

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
base/int16_and_uint16_conversion_to_string.1565969033.txt.gz · Last modified: 2019-08-16 17:23 by verz