Advertisement
verz

UtoA and ItoA (int and uint to string)

Aug 16th, 2019
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. *=$0801
  2.  
  3.         BYTE    $0B, $08, $0A, $00, $9E, $32, $30, $36, $31, $00, $00, $00
  4.  
  5. ;---------------------------------
  6.  
  7.  
  8. Example
  9.         lda #0
  10.         sta sgn
  11.         lda #$c0
  12.         sta int16
  13.         lda #$fd
  14.         sta int16+1
  15.  
  16.         jsr _UtoA               ; converts UInt16 to String
  17.         jsr _trimnum            ; trims leading spaces
  18.  
  19.         jsr _PrnNum
  20.  
  21.  
  22.         lda #0
  23.         sta sgn
  24.         lda #$c0
  25.         sta int16
  26.         lda #$fd
  27.         sta int16+1
  28.  
  29.         jsr _ItoA               ; converts Int16 to String
  30.         jsr _trimnum            ; trims leading spaces
  31.  
  32.         jsr _PrnNum
  33.  
  34.         rts
  35.  
  36. _PrnNum
  37.                 ; prints untrimmed string
  38.         lda #<CnvStr
  39.         ldy #>CnvStr
  40.         jsr $ab1e
  41.         lda #13
  42.         jsr $ffd2
  43.                 ; prints trimmed string
  44.         lda #<CnvTrm
  45.         ldy #>CnvTrm
  46.         jsr $ab1e
  47.         lda #13
  48.         jsr $ffd2
  49.  
  50.         rts
  51.  
  52.  
  53. ;-------------------------------
  54. ; Converts a 16bit signed integer into string
  55. ;-------------------------------
  56. ;
  57. ;       Call with 16 bit number in int16
  58. ;
  59. ;-------------------------------
  60.  
  61. _ItoA
  62.         lda int16+1
  63.         bpl _pos
  64.  
  65. _neg    clc
  66.         lda int16
  67.         eor #$ff
  68.         adc #1
  69.         sta int16
  70.         lda int16+1
  71.         eor #$ff
  72.         adc #0
  73.         sta int16+1
  74.         lda #'-'
  75.         sta sgn
  76.  
  77. _pos    jsr _UtoA
  78.  
  79.         lda sgn
  80.         beq _enditoa
  81.         dex
  82.         sta CnvStr,x
  83. _enditoa
  84.         rts
  85.  
  86.  
  87.  
  88. ;-------------------------------
  89. ; Converts a 16bit unsigned integer into string
  90. ;-------------------------------
  91. ;
  92. ;       Call with 16 bit number in int16
  93. ;
  94. ;-------------------------------
  95.  
  96.  
  97. _UtoA
  98.         jsr BinBcd16
  99.         ;lda bcd+2
  100.         and #$0f
  101.         ora #$30
  102.         sta CnvStr+1
  103.  
  104.         lda bcd+1
  105.         and #$0f
  106.         ora #$30
  107.         sta CnvStr+3
  108.         lda bcd+1
  109.         lsr
  110.         lsr
  111.         lsr
  112.         lsr
  113.         ora #$30
  114.         sta CnvStr+2
  115.  
  116.         lda bcd+0
  117.         and #$0f
  118.         ora #$30
  119.         sta CnvStr+5
  120.         lda bcd+0
  121.         lsr
  122.         lsr
  123.         lsr
  124.         lsr
  125.         ora #$30
  126.         sta CnvStr+4
  127.         lda #$20
  128.         sta CnvStr+0
  129. ;        rts                    ; decomment to avoid stripping leading 0s
  130.  
  131.         ldx #1                  ; remove 0s at beginning
  132. _rem0   lda CnvStr,x
  133.         cmp #$30
  134.         bne _rts
  135.         lda #$20                ; put a space instead
  136.         sta CnvStr,x
  137.         inx
  138.         cpx #$5
  139.         bne _rem0
  140.  
  141. _rts    rts
  142.  
  143.  
  144. _trimnum
  145.         cpx #0
  146.         beq _rts
  147.         ldy #0
  148. _trmlp  lda CnvStr,x
  149.         sta CnvTrm,y
  150.         beq _rts
  151.         inx
  152.         iny
  153.         jmp _trmlp
  154.  
  155.  
  156. bcd     = $61    ; system Fac, 3 bytes
  157. int16   = $64    ; system Fac, 2 bytes
  158. sgn     byte 0
  159. CnvStr  byte 0,0,0,0,0,0,0      ; 7 bytes: sgn + 5bytes + '\0'
  160. CnvTrm  byte 0,0,0,0,0,0,0
  161.  
  162.  
  163.  
  164. ;-------------------------------
  165. ; Converts a 16bit number to BCD
  166. ;-------------------------------
  167. BINBCD16
  168.         SED             ; Switch to decimal mode        2
  169.         LDA #0          ; Ensure the result is clear    2
  170.         STA bcd+0;                                      3
  171.         STA bcd+1;                                      3
  172.         STA bcd+2;                                      3       13
  173.  
  174. CBIT1   ASL int16       ; Shift out one bit             5
  175.         ROL int16+1     ;                               5
  176. ;        LDA bcd+0      ; And add into result            
  177.         ADC bcd+0       ;                               3
  178.         STA bcd+0       ;                               3
  179.         ASL int16       ; Shift out one bit             5
  180.         ROL int16+1     ;                               5
  181.         ADC bcd+0       ;                               3
  182.         STA bcd+0       ;                               3
  183.         ASL int16       ; Shift out one bit             5
  184.         ROL int16+1     ;                               5
  185.         ADC bcd+0       ;                               3
  186.         STA bcd+0       ;                               3
  187.         ASL int16       ; Shift out one bit             5
  188.         ROL int16+1     ;                               5
  189.         ADC bcd+0       ;                               3
  190.         STA bcd+0       ;                               3
  191.         ASL int16       ; Shift out one bit             5
  192.         ROL int16+1     ;                               5
  193.         ADC bcd+0       ;                               3
  194.         STA bcd+0       ;                               3
  195.         ASL int16       ; Shift out one bit             5
  196.         ROL int16+1     ;                               5
  197.         ADC bcd+0       ;                               3
  198.         STA bcd+0       ;                               3       96
  199.  
  200.         LDX #7;                                         2       2
  201. CBIT7   ASL int16       ; Shift out one bit             5
  202.         ROL int16+1     ;                               5
  203.         LDA bcd+0       ; And add into result           3
  204.         ADC bcd+0       ;                               3
  205.         STA bcd+0       ;                               3
  206.         LDA bcd+1       ; propagating any carry         3
  207.         ADC bcd+1       ;                               3
  208.         STA bcd+1       ;                               3
  209.         DEX             ; And repeat for next bit       2
  210.         BNE CBIT7       ;                               3       33*7-1=230
  211.  
  212.         LDX #3;                                         2       2
  213. CBIT13  ASL int16       ; Shift out one bit             5
  214.         ROL int16+1     ;                               5
  215.         LDA bcd+0       ; And add into result           3
  216.         ADC bcd+0       ;                               3
  217.         STA bcd+0       ;                               3
  218.         LDA bcd+1       ; propagating any carry         3
  219.         ADC bcd+1       ;                               3
  220.         STA bcd+1       ;                               3
  221.         LDA bcd+2       ; ... thru whole result         3
  222.         ADC bcd+2       ;                               3
  223.         STA bcd+2       ;                               3
  224.         DEX             ; And repeat for next bit       2
  225.         BNE CBIT13      ;                               3       42*3-1=125
  226.  
  227.         CLD             ; Back to binary                2       2; tot 470
  228.         rts             ; All Done.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement