home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol123 / mathlib.asm < prev    next >
Encoding:
Assembly Source File  |  1984-04-29  |  3.0 KB  |  130 lines

  1. ; MATHLIB: a 16 Bit Arithmetic Package for 6805
  2. ; Sourced Feb '81 by     Trevor Marshall
  3. ;            SYSOP, Thousand Oaks, Tech RBBS
  4. ;
  5. ;MULTIPLY:  16 x 16 Bit multiplication
  6. ;
  7. ; RESULT,RESULT+1 = MPLICAND,MPLICAND+1
  8. ;                         x MPLIER,MPLIER+1
  9. ; MPLIER, MPLICAND may be destroyed
  10. ;COUNT:    DS    1    ;loop counter
  11. ;RESULT: DS    2
  12. ;MPLICAND DS    2
  13. ;MPLIER    DS    2
  14. ;
  15. ;    Example: 5 x 3
  16. ;        101
  17. ;         x  011
  18. ;    ----------------
  19. ;        101    (Shifted LSBit=1)
  20. ;           101    (Shifted LSBit=1)
  21. ;          000    (Shifted LSBit=0,no add)
  22. ;    ----------------
  23. ;          01111    (Result)
  24. ;
  25. MULTIPLY: LDA    #16    ;initialize count
  26.     STA    COUNT
  27.     CLR    RESULT+1 ;and result
  28.     CLR    RESULT
  29. ;
  30. ; is multiplier LSBit = 1 ?
  31. ZZMULT:    SHR    MPLIER ;right shift multIplier MSB
  32. ;            ; 0 -> MSBit, LSBit -> Carry
  33.     RRC    MPLIER+1 ;shift right multiplier LSB
  34. ;            ;Carry-> MSBit, LSBit -> Carry
  35.     BNC    ZZNOADD ;LSBit not 1, don't add
  36. ;
  37. ; Could test for overflow here:
  38. ; (.Z80) CCF    ;Carry will be 1, C -> 0
  39. ;    ADC    HL,DE
  40. ;    JR    C,OVERFLOW.ROUTINE
  41. ; But will add without carry
  42.     LDA    MPLICAND+1 ;LSBit=1, so add multiplicand
  43. ;            ;to (shifted) result
  44.     ADD    RESULT+1
  45.     STA    RESULT+1
  46.     LDA    MPLICAND
  47.     ADC    RESULT
  48.     STA    RESULT
  49. ;
  50. ZZNOADD: SHL    MPLICAND+1 ; 0 -> LSBit, MSBit -> Carry
  51.     RLC    MPLICAND   ;Carry -> LSBit, MSBit -> Carry
  52.     DEC    COUNT
  53.     BNZ    ZZMULT
  54.     RET             ; ***** DONE *****
  55. ;
  56. ;
  57. ;
  58. ;DIVIDE: ;16 Bit by 16 Bit Integer Division
  59. ;
  60. ;                        (DIVIDEND,DIVIDEND+1)
  61. ; DIVIDEND,DIVIDEND+1  =----------------------- + REM,REM+1
  62. ;                        (DIVISOR,DIVISOR+1)
  63. ; The dividend & divisor are stored in memory with
  64. ;   the MSByte first
  65. ; Their location is via EQU statements
  66. ;COUNTER:  EQU    $39    ;A scratch location for loop count
  67. ;DIVIDEND: EQU    $3A    ;2 locs at top of user RAM
  68. ;RESULT:   EQU    $    ;Result replaces dividend
  69. ;DIVISOR:  EQU    $3C    ; "
  70. ;REM:       EQU    $3E    ; "
  71. ;
  72. ; The divisor is successively subtracted from the high 
  73. ;   order bits of the dividend. After each subtraction 
  74. ;   the result is used instead of the initial dividend
  75. ; The result is increased by 1 each time.
  76. ; When the result of the subtraction is negative the 
  77. ;   partial result is restored by adding the divisor 
  78. ;   back to it.
  79. ; The result is simulataneously decremented by 1
  80. ;
  81. ;First check if divisor is 0
  82. DIVIDE:    LDA    DIVISOR
  83.     XOR    DIVISOR+1
  84.     BZ    DIVBY0
  85. ;clear result
  86.     CLRA
  87.     STA    REM
  88.     STA    REM+1
  89. ;loop counter
  90.     LDA    #16
  91.     STA    COUNTER
  92. ;
  93. ;Rotate Dividend left 
  94.     CLRC
  95. ZZDIV1:    RLC    DIVIDEND+1
  96.     RLC    DIVIDEND
  97. ;Rotate Remainder left and collect carry result
  98.     RLC    REM+1
  99.     RLC    REM
  100.     LDA    REM+1
  101.     SUB    DIVISOR+1
  102.     STA    REM+1    ;Modify to reflect result
  103.     LDA    REM
  104.     SBC    DIVISOR
  105.     STA    REM    ;    "
  106.     BNC    ZZPOS
  107. ;otherwise negative
  108.     LDA    REM+1    ;Restore dividend
  109.     ADD    DIVISOR+1
  110.     STA    REM+1
  111.     LDA    REM
  112.     ADC    DIVISOR
  113.     STA    REM
  114. ZZPOS:    BNC    ZZPOS1 ;If carry is clear set it
  115.     CLRC           ;if set, clear it
  116.     BR    ZZPOS2 ;Thus complement the carry
  117. ZZPOS1:    SETC
  118. ZZPOS2:    DEC    COUNTER
  119.     BNZ    ZZDIV1
  120.     RLC    DIVIDEND+1 ;Shift in last result bit
  121.     RLC    DIVIDEND
  122.     RET             ; ***** DONE ****
  123. ;
  124. DIVBY0    LDA    $FF    ;infinity
  125.     STA    DIVIDEND+1
  126.     STA    DIVIDEND
  127. ; Output a diagnostic message if desired
  128.     RET
  129. ;
  130.