home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / emulate / systems / micro / 51div.asm
Encoding:
Assembly Source File  |  1994-06-17  |  3.7 KB  |  102 lines

  1.  
  2.                         released to Compuserve (and public domain) 1-19-93
  3.                         by Scott Mitchell Jennings (author)
  4.  
  5.  
  6. This is a simple(?) sixteen bit divide routine for the 8048 and 8051
  7. class of microcontrollers.  Note that Multiplication and Division are
  8. not true compliments of one another the way Addition and Subtraction
  9. are.  For example, division by zero is invalid; but there is no
  10. corresponding condition in Multiplication.  Hence, a sixteen bit
  11. multiply routine can make extensive use of an eight bit multiply
  12. instruction, whereas a sixteen bit divide routine cannot make use of
  13. an eight bit divide instruction.
  14.  
  15.  
  16. For speed, all intermediate data are kept in the "R" registers, and
  17. no use of addressable RAM or STACK space is needed.  All the
  18. registers (of one bank) are used.
  19.  
  20.                     -  Scott Mitchell Jennings /kd6gou
  21.                        PO Box 107
  22.                        Penn Valley, CA    95946
  23.                        916-477-6449
  24.  
  25.  
  26.  
  27. ;
  28. ; On Entry:
  29. ;            R0:R1  contains the divisor   (low byte in R0)
  30. ;            R2:R3  contains the dividend  (low byte in R2)
  31. ;
  32. ;
  33. ; On Exit:
  34. ;            R4:R5  contains the quotient  (low byte in R4)
  35. ;            R6:R7  contains the remainder (low byte in R6)
  36. ;
  37.  
  38.  
  39.  
  40.  
  41.                     MOV      A, R1    ;*************
  42.                     CPL      A        ;**         **
  43.                     MOV      DPH, A   ;** negate  **
  44.                     MOV      A, R0    ;**   the   **  you'll need to change
  45.                     CPL      A        ;** divisor **  only this code here
  46.                     MOV      DPL, A   ;**         **  to adapt to the 8048
  47. ;                                     ;**   in    **  class of uPs
  48.                     INC      DPTR     ;**  R0:R1  **
  49.                     MOV      R0, DPL  ;**         **
  50.                     MOV      R1, DPH  ;*************
  51.  
  52.  
  53.                     CLR      A
  54.                     MOV      R4, A
  55.                     MOV      R5, A  ;R4:R5=quotient
  56.                     MOV      R6, A
  57.                     MOV      R7, A  ;R6:R7=remainder                    
  58.  
  59.                     INC      R4  ;quotent=1 (done when shifted into CY)
  60.  
  61. DIVIDE_LOOP:        XCH      A, R2   ;****************************
  62.                     RLC      A       ;**                        **
  63.                     XCH      A, R2   ;**                        **
  64.                     XCH      A, R3   ;**                        **
  65.                     RLC      A       ;**     shift one bit      **
  66.                     XCH      A, R3   ;**                        **
  67. ;                                    ;** from dividend  (R2:R3) **
  68.                     XCH      A, R6   ;**                        **
  69.                     RLC      A       ;** into remainder (R6:R7) **
  70.                     XCH      A, R6   ;**                        **
  71.                     XCH      A, R7   ;**                        **
  72.                     RLC      A       ;**                        **
  73.                     XCH      A, R7   ;****************************
  74.  
  75.                     MOV      A, R6
  76.                     ADD      A, R0
  77.  
  78.                     MOV      A, R7
  79.                     ADDC     A, R1
  80.                     JNC      DIV_ZERO  ;branch if borrowed
  81.  
  82.  
  83.                     MOV      A, R6
  84.                     ADD      A, R0
  85.                     MOV      R6, A
  86.  
  87.                     MOV      A, R7
  88.                     ADDC     A, R1
  89.                     MOV      R7, A
  90.  
  91. DIV_ZERO:           XCH      A, R4
  92.                     RLC      A
  93.                     XCH      A, R4
  94.                     XCH      A, R5
  95.                     RLC      A
  96.                     XCH      A, R5
  97.                     JNC      DIVIDE_LOOP
  98.  
  99.  
  100.                     ;done.
  101.  
  102.