verz

sqrt32 - 6502

Aug 19th, 2019
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;********************************************
  2. ;*    sqrt32 unrolled
  3. ;*
  4. ;*   computes integer Sqrt of a 32bit number
  5. ;********************************************
  6. ;*   by Verz - Jul2019
  7. ;********************************************
  8. ;*
  9. ;*  input:  square, 32bit source number
  10. ;*  output: sqrt,   16bit value
  11. ;*          rmnd,  17bit value
  12. ;********************************************
  13.  
  14. sqrt32_unrolled
  15.         lda #0
  16.         sta sqrt       ; R=0
  17.         sta sqrt+1
  18.         sta M+4        ; aka T+0
  19.         ;sta T+1       ; T+1 = 0 until last iteration; T+0 is always 0
  20.         clc
  21.  
  22. ; iterations 1..7
  23.         ldy #6         ; 7 iterations (6-->0)
  24. _loop1  
  25.                        ; T =((2*R+D) LSR 1) ASL 16 ; actually: T+2 = R+(D LSR 1)
  26.         ;lda sqrt / ora stablo,y / sta T+2 ;   Dlo=0 / sqrt+0=0 during these iterations
  27.         lda sqrt+1
  28.         ora stablo,y   ; would be stabhi,y but changed the counter
  29.         sta T+3
  30.         bcs elab1
  31.        
  32.         lda M+3
  33.         cmp T+3
  34.         bcc nxt1       ; T <= M    (branch if T>M)
  35. ;        bne skip06
  36. ;        lda M+2 / sbc sqrt      ; T+2 = sqrt =0 during these iterations
  37. ;        bcc skip16
  38. elab1  ;sec
  39.                        ; M=M-T ; T+2 = sqrt+0 =0 during these iterations
  40. ;        lda M+2 / sbc sqrt / sta M+2
  41.         lda M+3
  42.         sbc T+3
  43.         sta M+3
  44.                        ; R=R+D   ; stablo+1,y = 0 during these iterations
  45. ;        lda sqrt / ora stablo+1,y / sta sqrt
  46.         lda sqrt+1
  47.         ora stablo+1,y ; would be stabhi,y but changed the counter
  48.         sta sqrt+1
  49.  
  50. nxt1    asl M          ; M=M*2
  51.         rol M+1
  52.         rol M+2
  53.         rol M+3
  54.         dey            ; implicit: D=D/2, by the move of .Y
  55.         bpl _loop1
  56.  
  57. ; 8th iteration
  58. _loop8  
  59. ;               sqrt+0 = 0, stabhi,y = 0 during this iteration
  60. ;        lda #$80 / ora sqrt / sta T+2 / lda sqrt+1 / sta T+3
  61.         bcs elab8
  62.        
  63.         lda M+3
  64.         cmp sqrt+1     ; T+3=sqrt+1
  65.         bcc nxt8      ; T <= M    (branch if T>M)
  66.         bne elab8
  67.         lda M+2
  68.         sbc #$80       ; T+2=$80
  69.         bcc nxt8
  70. elab8   ;sec
  71.         lda M+2        ; M=M-T
  72.         sbc #$80       ; T+2=$80
  73.         sta M+2
  74.         lda M+3
  75.         sbc sqrt+1     ; T+3=sqrt+1
  76.         sta M+3
  77. ;        lda sqrt      ; R=R+D
  78. ;        ora stablo+1,y         ; stablo+1,y =0 during this iteration
  79. ;        sta sqrt
  80.         inc sqrt+1     ; lda sqrt+1 / ora #1 / sta sqrt+1
  81.  
  82. nxt8    asl M          ; M=M*2
  83.         rol M+1
  84.         rol M+2
  85.         rol M+3
  86.  
  87. ; iterations 9..15
  88.         ldy #6         ; 7 iterations (6-->0)
  89. _loop9  
  90.         lda stablo,y
  91.         ora sqrt
  92.         sta T+2
  93. ;        lda sqrt+1 / sta T+3   ; stabhi,y = 0 during these iterations
  94.         bcs elab9
  95.        
  96.         lda M+3
  97.         cmp sqrt+1     ; T+3=sqrt+1
  98.         bcc nxt9      ; T <= M    (branch if T>M)
  99.         bne elab9
  100.         lda M+2
  101.         sbc T+2
  102.         bcc nxt9
  103. elab9   ;sec
  104.         lda M+2        ; M=M-T
  105.         sbc T+2
  106.         sta M+2
  107.         lda M+3
  108.         sbc sqrt+1     ; T+3=sqrt+1
  109.         sta M+3
  110.         lda sqrt       ; R=R+D
  111.         ora stablo+1,y
  112.         sta sqrt
  113. ;        lda sqrt+1 / adc #0 / sta sqrt+1 ; stabhi+1,y =0 during these iterations
  114.  
  115. nxt9    ;asl M         ; M=M*2  \  M+0 =0 during these iterations
  116.         asl M+1
  117.         rol M+2
  118.         rol M+3
  119.         dey            ; implicit: D=D/2, by the move of .Y
  120.         bpl _loop9
  121.  
  122. ; 16th and last iteration
  123. _lastiter             ; code for last iteration
  124.         ; during last iteration D=1, so (2*R+D) LSR 1 makes D the MSB of T+1
  125.         bcs elab16
  126.  
  127.         lda M+3
  128.         cmp sqrt+1    ; T+3 = sqrt+1
  129.         bcc nxt16     ; T <= M    branch if T>M
  130.         bne elab16
  131.         lda M+2
  132.         cmp sqrt      ; T+2 = sqrt
  133.         bcc nxt16
  134.         bne elab16
  135.         lda M+1
  136.         cmp #$80      ; T+1 = $80
  137.         bcc nxt16
  138. elab16  ;sec
  139.         lda M+1
  140.         sbc #$80      ; T+1 = $80
  141.         sta M+1
  142.         lda M+2       ; M=M-T
  143.         sbc sqrt
  144.         sta M+2
  145.         lda M+3
  146.         sbc sqrt+1
  147.         sta M+3
  148.         inc sqrt      ; R=R+D with D=1
  149. nxt16   ;asl M        ; M=M*2  \  M+0 =0 during this iteration
  150.         asl M+1
  151.         rol M+2
  152.         rol M+3
  153.         rol M+4
  154. _sqrend rts
  155.  
  156. ;**** Variables and Shift table
  157. ;stabhi byte 0,0,0,0,0,0,0,0
  158. stablo BYTE $01,$02,$04,$08,$10,$20,$40,$80
  159. ;       byte 0,0,0,0,0,0,0,0
  160.  
  161. square  = $57     ; 4 bytes: $57-$5a; input value
  162. sqrt    = $60     ; 2 bytes: $60-$61; result
  163. rmnd    = M+2     ; 3 bytes: $59-$5b; in the high bytes of M (M LSR 16)
  164. T       = $5B     ; 4 bytes: $5b-$5e; could be 2 bytes: T+0 is always 0; T+1 is 0 until last iteration
  165. M       = square  ; 5 bytes: $57-$5b, over the input square and over T+0
Add Comment
Please, Sign In to add comment