Advertisement
verz

Sqrt32 - Z80

Aug 3rd, 2019
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;
  2. ;   Sqrt32
  3. ;
  4. ;     Sqrt32: Integer Square Root of input + remainder
  5. ;       input:    4 bytes in _square
  6. ;       output:   2 bytes in _sqrt,     result
  7. ;                 3 bytes in M+2,       remainder
  8. ;
  9. ;   implements:
  10. ;        R= 0
  11. ;        M= N                               ;N= input
  12. ;        D=2^15 (8000h)                     ;D= 2^(p-1),  p= #bits(result) = #bits(input)/2
  13. ;        for n= 1 to 16                     ;for n= 1 to p
  14. ;        {
  15. ;            T= ((R+R+D) >> 1) << 16        ; T= (2R+D) << (p-1)
  16. ;            if (T <= M) then M=M-T: R=R+D
  17. ;            M= M << 1
  18. ;            D= D/2
  19. ;        }
  20.  
  21.  
  22. _square .db 0,0,0,0
  23. _M4     .db 0
  24. _sqrt   .db 0,0
  25. ;_R     = _sqrt
  26. ;_T     .db 0,0,0,0
  27. _M      = _square
  28. ;_D     .db 0,0
  29.  
  30. sqrt32
  31.     xor a
  32.     ld (_M4),a
  33.     ld b,a
  34.     ld c,a
  35.     ;ld (_sqrt),bc
  36.     ld de,04000h
  37.     ld ixh, 0fh     ; 15 iterations + lastiter = 16
  38. loopsq
  39.     ;ld hl,(_sqrt)
  40.     ld h,b          ; T=[((2*R+D)>>1) << 16]; in fact upperbytes(T) = R + (D >> 1)
  41.     ld l,c
  42.     add hl,de
  43.     ;ld (_T+2),hl
  44.     ;ld (_D),de
  45.     push de
  46.     or a
  47.     jp nz, _sk      ; if bit 0,a =1 => m>t
  48.        
  49.     ;or a           ; CY=0
  50.     ;ld de,(_T+2)
  51.     ex de,hl
  52.     ld hl,(_M+2)
  53.     sbc hl,de;      if m>t  => nc nz nn;  if t=m => z nc nn; if m<t => nz c n; need to jump when t>m
  54.     jp c, _skp1    ; jump if t>m
  55.     jp _skp0
  56.    
  57. _sk ex de,hl
  58.     ld hl,(_M+2)
  59.     sbc hl,de
  60.  
  61. _skp0
  62.     ld (_M+2),hl    ; M = M-T
  63.  
  64.     pop hl          ; R = R+D
  65.     push hl
  66.     add hl,hl
  67.     ;ld de,(_sqrt)
  68.     ;add hl,de
  69.     add hl,bc
  70.     ;ld (_sqrt),hl
  71.     ld b,h
  72.     ld c,l
  73.    
  74. _skp1
  75.     xor a
  76.     ld hl,(_M)      ; M = M*2
  77.     add hl,hl
  78.     ld (_M),hl
  79.     ld hl,(_M+2)
  80.     adc hl,hl
  81.     ld (_M+2),hl
  82.     rla
  83.    
  84.     ;ld de,(_D)     ; D = D/2
  85.     pop de
  86.     srl d
  87.     rr e
  88.  
  89.     dec ixh
  90.     jp nz, loopsq
  91.  
  92. lastiter
  93.     or a
  94.     jp nz, _sk0
  95.  
  96.     ;or a         ; CY=0     in this iteration _T+2=_sqrt
  97.     ;ld de,(_sqrt)
  98.     ;ld hl,(_M+2)
  99.     sbc hl,bc;      if m>t  => nc nz nn;  if t=m => z nc nn;  if m<t => nz c n; need to jump when t>m
  100.     jp c, _sk1    ; jump if t>m
  101.     jp nz, _sk0   ; jump if t<m  (because  t != m   and   t !> m)
  102.     ld a,(_M+1)
  103.     sub 080h
  104.     jp c, _sk1
  105.     jp _s
  106.    
  107. _sk0
  108.     ld a,(_M+1)
  109.     sub 080h
  110.     ;ld (_M+1),a
  111.     ;or a
  112.     ld hl,(_M+2)
  113.     sbc hl,bc
  114. _s  ld (_M+2),hl
  115.     inc bc          ; D=1,   R = R+D => R = R+1
  116. _sk1
  117.     ;ld a,(_M+1)
  118.     sla a           ; M = M*2  (M = M << 1)
  119.     ;ld (_M+1),a
  120.     ld hl,(_M+2)
  121.     adc hl,hl
  122.     ld (_M+2),hl    ; remainder to _M+2
  123.     jp nc, _sR
  124.     ld a,1
  125.     ld (_M+4),a
  126. _sR ld (_sqrt),bc   ; result (BC) to _sqrt; could be saved in _M+0, to have M+0/M+1 = result, M+2/M+3 = remainder
  127.  
  128.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement