Advertisement
fgallego

Z80 16-bits Unsigned Division

May 20th, 2021 (edited)
2,691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Z80 Assembler 2.83 KB | Source Code | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; Z80 16-bits Unsigned Integer Division
  3. ;; Copyright (c) 2021 Francisco J. Gallego-Durán (@FranGallegoBR, fjgallego@ua.es)
  4. ;;
  5. ;; Permission is hereby granted, free of charge, to any person obtaining a copy
  6. ;; of this software and associated documentation files (the "Software"), to deal
  7. ;; in the Software without restriction, including without limitation the rights
  8. ;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. ;; copies of the Software, and to permit persons to whom the Software is
  10. ;; furnished to do so, subject to the following conditions:
  11. ;;
  12. ;; The above copyright notice and this permission notice shall be included in all
  13. ;; copies or substantial portions of the Software.
  14. ;;
  15. ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. ;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. ;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. ;; SOFTWARE.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;; divide16
  25. ;;  Divides 2 16-bit numbers and produces 16-bit Quotient and 16-bit Remainder. Both
  26. ;; numbers must be unsigned integers. This routine assumes Numerator >= Denominator
  27. ;; and Denominator > 0.
  28. ;;
  29. ;; Input:
  30. ;; - HL = Numerator   {N}
  31. ;; - BC = Denominator {D}
  32. ;; Ouput:
  33. ;; - IX = Quotient    {Q}
  34. ;; - HL = Remainder   {R}
  35. ;;
  36. ;; Assembler used: ASZ80
  37. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  38. divide16:
  39.    ld    ix, #0   ;; [4] IX = {Q} = 0
  40.    ld    de, #0   ;; [3] DE = {R} = 0
  41.    ld     a, #16  ;; [2] A = 16 Iterations
  42.    jr    start    ;; [3] Start first iteration
  43.  
  44. cotient_add1:
  45.    inc   ix       ;; [2] ++{Q}
  46. next:
  47.    dec    a       ;; [1]   --Iterations
  48.    ret    z       ;; [2/4] if (Iterations == 0) return;
  49.    ex    de, hl   ;; [1] HL={N}, DE={R}
  50.    add   ix, ix   ;; [4] {Q} = 2{Q} (Each iteration, Quotient doubles)
  51. start:
  52.    add   hl, hl   ;; [3] {N}=2{N} ==> Carry = Leftmost bit of {N}
  53.    ex    de, hl   ;; [1] HL={R}, DE={N}
  54.    rl     l       ;; [2] / {R}=2{R} + Leftmost bit of {N}
  55.    rl     h       ;; [2] \
  56.    sbc   hl, bc   ;; [3] HL = {R}-{D} (Check if {R} > {D})
  57.    jr    nc, cotient_add1 ;; [2/3] if ( {R} > {D} ) { {R} -= {D}; ++{Q} }
  58.  
  59.    ;; {R} <= {D}, Restore previous {R}
  60.    add   hl, bc   ;; [3] HL += {D} --> HL = {R}-{D}+{D} = {R}
  61.    jr    next     ;; [3] Continue processing next bit
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement