verz

BinBcd16 unrolled

Aug 15th, 2019
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;-------------------------------
  2. ; Converts a 16bit number to BCD
  3. ;-------------------------------
  4. BINBCD16
  5.         SED             ; Switch to decimal mode        2
  6.         LDA #0          ; Ensure the result is clear    2
  7.         STA bcd+0;                                      4
  8.         STA bcd+1;                                      4
  9.         STA bcd+2;                                      4       16
  10.  
  11.         LDX #6;                                         2       2
  12. CNVBIT1          
  13.         ASL lobyte     ; Shift out one bit              6
  14.         ROL hibyte      ;                               6
  15. ;        LDA bcd+0      ; And add into result          
  16.         ADC bcd+0       ;                               4
  17.         STA bcd+0       ;                               4
  18. ;        LDA bcd+1      ; propagating any carry
  19. ;        ADC bcd+1
  20. ;        STA bcd+1
  21. ;        LDA bcd+2      ; ... thru whole result
  22. ;        ADC bcd+2
  23. ;        STA bcd+2
  24.         DEX             ; And repeat for next bit       2
  25.         BNE CNVBIT1     ;                               3       25*6-1=149
  26.  
  27.         LDX #7;                                         2       2
  28. CNVBIT2
  29.         ASL lobyte     ; Shift out one bit              6
  30.         ROL hibyte      ;                               6
  31.         LDA bcd+0      ; And add into result            4
  32.         ADC bcd+0       ;                               4
  33.         STA bcd+0       ;                               4
  34.         LDA bcd+1      ; propagating any carry          4
  35.         ADC bcd+1       ;                               4
  36.         STA bcd+1       ;                               4
  37. ;        LDA bcd+2      ; ... thru whole result
  38. ;        ADC bcd+2
  39. ;        STA bcd+2
  40.         DEX             ; And repeat for next bit       2
  41.         BNE CNVBIT2     ;                               3       41*7-1=286
  42.  
  43.         LDX #3;                                         2       2
  44. CNVBIT3
  45.         ASL lobyte     ; Shift out one bit              6
  46.         ROL hibyte      ;                               6
  47.         LDA bcd+0      ; And add into result            4
  48.         ADC bcd+0       ;                               4
  49.         STA bcd+0       ;                               4
  50.         LDA bcd+1      ; propagating any carry          4
  51.         ADC bcd+1       ;                               4
  52.         STA bcd+1       ;                               4
  53.         LDA bcd+2      ; ... thru whole result          4
  54.         ADC bcd+2       ;                               4
  55.         STA bcd+2       ;                               4
  56.         DEX             ; And repeat for next bit       2
  57.         BNE CNVBIT3     ;                               3       53*3-1=158
  58.  
  59.         CLD             ; Back to binary                2       2; tot 615
  60.        
  61.         rts             ; All Done.                            
  62.  
  63. bcd    byte 0,0,0
  64. lobyte byte 0
  65. hibyte byte 0
  66.  
  67.  
  68. ; takes 615+rts cycles. If variables are in page zero (bcd and lobyte/hibyte), it goes down to 499 cycles.
  69. ; bcd = $a3
  70. ; lobyte = $2
  71. ; hibyte = $3
Add Comment
Please, Sign In to add comment