home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol123 / mathlib.z80 < prev    next >
Encoding:
Text File  |  1984-04-29  |  2.8 KB  |  117 lines

  1. ; MATHLIB: a 16 Bit Arithmetic Package for Z80
  2. ; Sourced 26 Nov '80 by Trevor Marshall
  3. ;            SYSOP, Thousand Oaks Tech RBBS
  4. ;
  5. ;
  6. MULTIPLY: ; 16 x 16 Bit multiplication
  7. ; (may be truncated to 16 x n bits)
  8. ;
  9. ;result (HL) = multiplicand (DE) x multiplier (BC)
  10. ;
  11. ; The multiplicand is in DE
  12. ; The multiplier is in BC
  13. ; The result will be in HL
  14. ; It overflows safely, but without indication
  15. ; Registers are destroyed
  16. ;
  17. ;    Example: 5 x 3
  18. ;        101
  19. ;         x  011
  20. ;    ----------------
  21. ;        101    (Shifted LSBit=1)
  22. ;           101    (Shifted LSBit=1)
  23. ;          000    (Shifted LSBit=0,no add)
  24. ;    ----------------
  25. ;          01111    (Result)
  26. ;
  27. ;  Multiplier is in BC
  28.     LD    A,16    ; Loop count in A
  29. ;This count must be >= the number of bits used in BC
  30. ;
  31.     LD    HL,0    ;Clear result
  32. ;
  33. ZZMULT:    
  34. ; is multiplier LSBit = 1 ?
  35.     SRL    B    ;Right shift multiplier MSB
  36.             ; 0 -> MSBit, LSBit -> Carry
  37.     RR    C    ;Rotate right multiplier LSB
  38.             ;Carry -> MSBit, LSBit -> Carry
  39.     JR    NC,ZZNOADD ;LSBit not 1, Dont add
  40. ;
  41. ; Could test for overflow by using this here:
  42. ;    CCF    ;Carry will be 1, C -> 0
  43. ;    ADC    HL,DE
  44. ;    JR    C,OVERFLOW.ROUTINE
  45. ; But will use the simpler
  46.     ADD    HL,DE    ;LSBit = 1, so add multiplicand
  47.             ;    to (shifted) result
  48. ;
  49. ZZNOADD:
  50. ;Now we shift left the multiplicand
  51.     SLA    E    ; 0 -> LSBit, MSBit -> Carry
  52.     RL    D    ; Carry -> LSBit, MSBit -> Carry
  53. ;
  54.     DEC    A    ;Loop cntr
  55.     JR    NZ,ZZMULT
  56. ;
  57.     RET    ; ***** DONE *****
  58. ;
  59. ;-------------------------------------------------------
  60. ;
  61. DIVIDE:        ;16 Bit by 16 Bit Integer Division
  62. ;
  63. ;            dividend (BC)
  64. ;    result (BC) = --------------- + remainder (HL)
  65. ;            divisor  (DE)
  66. ;
  67. ; The dividend is in BC, and the result returns in BC
  68. ; The divisor is in DE
  69. ; After the division HL contains the remainder
  70. ;
  71. ; The divisor is successively subtracted from the high 
  72. ;   order bits of the dividend. After each subtraction 
  73. ;   the result is used instead of the initial dividend
  74. ; The result is increased by 1 each time.
  75. ; When the result of the subtraction is negative the 
  76. ;   partial result is restored by adding the divisor 
  77. ;   back to it.
  78. ; The result is simulataneously decremented by 1
  79. ;
  80. ;First check if divisor is 0
  81.     LD    A,D
  82.     OR    E
  83.     JR    Z,DIVIDE.BY.ZERO
  84. ; Dividend is in BC
  85. ;clear result
  86.     LD    HL,0
  87. ;loop counter
  88.     LD    A,16    ;DO NOT TRUNCATE
  89. ;
  90. ;Rotate Dividend left 
  91. ;Carry has been zeroed by the OR E above
  92. ZZDIV1:    RL    C    ;Carry -> LSBit, MSBit -> Carry
  93.     RL    B        ;ditto
  94. ;Rotate Remainder left
  95.     ADC    HL,HL    ;Never sets carry,
  96.             ;  ie RESETS carry
  97. ;Trial subtraction of divisor from result
  98.     SBC    HL,DE    ;Carry -> 0 if no borrow
  99.             ;Carry -> 1 if borrow
  100.     JR    NC,ZZPOS
  101. ;otherwise negative
  102.     ADD    HL,DE    ;Restore dividend
  103. ZZPOS:    CCF    ;Calc Result Bit, Z80 carry peculiar
  104.     DEC    A    ;Loop counter
  105.     JR    NZ,ZZDIV1    ;Loop for 16 Bits
  106. ;
  107.     RL    C    ;Shift in last result bit
  108.     RL    B
  109. ; The result is in BC, the remainder in HL
  110. ;
  111.     RET    ; ***** DONE ****
  112. ;
  113. DIVIDE.BY.ZERO: LD  BC,0FFFFH ;Infinity
  114. ; Output a diagnostic message if desired
  115.     RET
  116. ;
  117.