Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Z80 16-bits Unsigned Integer Division
- ;; Copyright (c) 2021 Francisco J. Gallego-Durán (@FranGallegoBR, fjgallego@ua.es)
- ;;
- ;; Permission is hereby granted, free of charge, to any person obtaining a copy
- ;; of this software and associated documentation files (the "Software"), to deal
- ;; in the Software without restriction, including without limitation the rights
- ;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- ;; copies of the Software, and to permit persons to whom the Software is
- ;; furnished to do so, subject to the following conditions:
- ;;
- ;; The above copyright notice and this permission notice shall be included in all
- ;; copies or substantial portions of the Software.
- ;;
- ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- ;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- ;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- ;; SOFTWARE.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; divide16
- ;; Divides 2 16-bit numbers and produces 16-bit Quotient and 16-bit Remainder. Both
- ;; numbers must be unsigned integers. This routine assumes Numerator >= Denominator
- ;; and Denominator > 0.
- ;;
- ;; Input:
- ;; - HL = Numerator {N}
- ;; - BC = Denominator {D}
- ;; Ouput:
- ;; - IX = Quotient {Q}
- ;; - HL = Remainder {R}
- ;;
- ;; Assembler used: ASZ80
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- divide16:
- ld ix, #0 ;; [4] IX = {Q} = 0
- ld de, #0 ;; [3] DE = {R} = 0
- ld a, #16 ;; [2] A = 16 Iterations
- jr start ;; [3] Start first iteration
- cotient_add1:
- inc ix ;; [2] ++{Q}
- next:
- dec a ;; [1] --Iterations
- ret z ;; [2/4] if (Iterations == 0) return;
- ex de, hl ;; [1] HL={N}, DE={R}
- add ix, ix ;; [4] {Q} = 2{Q} (Each iteration, Quotient doubles)
- start:
- add hl, hl ;; [3] {N}=2{N} ==> Carry = Leftmost bit of {N}
- ex de, hl ;; [1] HL={R}, DE={N}
- rl l ;; [2] / {R}=2{R} + Leftmost bit of {N}
- rl h ;; [2] \
- sbc hl, bc ;; [3] HL = {R}-{D} (Check if {R} > {D})
- jr nc, cotient_add1 ;; [2/3] if ( {R} > {D} ) { {R} -= {D}; ++{Q} }
- ;; {R} <= {D}, Restore previous {R}
- add hl, bc ;; [3] HL += {D} --> HL = {R}-{D}+{D} = {R}
- jr next ;; [3] Continue processing next bit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement