1 / 18

Division….. Saj Alam and Samya Zain

Division….. Saj Alam and Samya Zain. Monday November 27, 2006. Division. A = A  B = Q + R B A= Dividend n A = Number of digits of Dividend (A)

norina
Télécharger la présentation

Division….. Saj Alam and Samya Zain

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Division…..Saj Alam and Samya Zain Monday November 27, 2006

  2. Division A = A  B = Q + R B A= Dividend nA= Number of digits of Dividend (A) B = Divisor nB= Number of digits of Divisor (B) Q = Quotient nQ= Number of digits of Quotient (Q) nQ = nA -nB if nA nB

  3. 5 nQ = 1 nQ = 2 10 53 nB = 2 nA = 2 nB = 2 nA = 3 50 3 R= 1 R= 2 Simple Division Examples Example 1: Example 2: 24 22 540 44 100 88 12 nQ = nA -nB if nA nB

  4. Binary Number System • System Digits:  0 and 1 • Bit (short for binary digit):  A single binary digit • LSB (least significant bit):  The rightmost bit • MSB (most significant bit):  The leftmost bit • 53 = 110101 • MSB LSB

  5. = = = = = = = Binary To Decimal Conversion

  6. Algorithms STRAIGHT SUBTRACT SUBTRACT WITH SHIFT 5910 = 111011 1110 = 1011 59 = (Q=5) + (R=4) 11 111011 1011 Q = 0 +1 1 1 = 510 R= 110000 Q = 1 +1 111011 1011 = 5910 1011 = 1110 Q = 10 +1 100101 R= 1011 1011 R= 11010 Q = 11 +1 = 410 1011 1111 R= Q = 100 +1 1011 R= Q = 101 100 • Intuitively simple • Tedious for large numbers • SLOW if divisor is small compared to the dividend • COMPLEX • FAST 0 001111 100 There are 2 subtractions There are 5 subtractions

  7. Straight Shift Algorithm Step1: Subtract divisor from dividend, record the remainder and set quotient to 1. Step2: If the remainder is greater than the divisor, subtract divisor from the remainder, add one to the quotient and record the new remainder. Step3: Repeat step 2 until remainder is less than the divisor. The final value of the quotient and remainder is the answer.

  8. Subtract with Shift Algorithm Step1:Write the divisor and the dividend with MSB lined up. Step2: From the first n MSB of dividend, subtract the divisor and enter “1” as the MSB of the quotient. NOTE: IF the divisor is less than the first n bits of the dividend, then use the first (n+1) bits of the divisor and enter “1” as the MSB of the quotient. Record the remainder from this step. Step3: Next form a number using remainder and the next MSB of the dividend as the LSB of the number. Step3a: If this number is less than the divisor, enter “0” as the next MSB of the quotient and form a new number using the next MSB of the dividend as the LSB of the number. Step3b: If this number is greater then the divisor, then subtract the divisor and enter “1” as the next MSB of the quotient. Step4: Repeat Step 3, until all bits in the dividend are used up.

  9. CF AX BX DX CX CF  AX Dividend BX Divisor DX Bit count CX  CF 00 00 FF FF FF FF 00 10H  16- Bit Division Dividend = FFFF Divisor = FFFF

  10.  AX SUB AX, AX ;  AX ;  CF Dividend BX MOV BX , DIVIDEND Divisor DX MOV DX, DIVISOR Bit count(17)CX MOV CX, 17 ; number of bits NC =  CF AX  (AX – DX) DX  AX  CF =1 DX  AX  CF =0 nxt_bit: SUB AX, DX NEXT: Check CF JNC SHIFT ; skip addition step if bit = 0 CF = 1 CF = 0 AX  (AX + DX) ADD AX, DX ; add MCAND to product SHIFT: CF = 1 0; sub not done CF = 0 1; sub done Rotate AX, BX left 1 bit CX  CX - 1 RCL BX, 1 ; shift the product 1 bit left CF = 1; no change RCL AX, 1 DEC CX ; change counter down by 1 JCXZ DONE ; if CX = 0, all bits are done JMP nxt_bit ; otherwise go on to next bit NO Is CX = 0 ? MOV PRODL, BX ; store low word MOV PRODH, AX ; store high word RET YES Store final product in memory

  11. ;;****************************************************************************;;**************************************************************************** TITLE MAIN MODULE DIV16 (UNSIGNED) PAGE 57,80 ;;---------------------------------------------------------------------------- ;; Date created: 12/01/88 ;; Author : M. S. Alam ;; Revised : 12/09/96 ;;----------------------------------------------------------------------------- ;; In this program, we divide a 16-bit number in registers BX ;; by an 16-bit number in DX . The quotient is returned in BX and the ;; remainder in AX ;; Here the numbers are loaded from memory ;; Some examples to try ;; 0000H / FFFFH = 0000H REM = 0 ;; FFFFH / FFFFH = 0001H REM = 0 ;; FFFFH / FFFH = 65535 / 4095 = 0010H REM = FH ;; FFFFH / FFH = 65535 / 255 = 0101H REM = 0 ;; FFFFH / FH = 65535 / 15 = 1111H REM = 0 ;; EFF1H / FH = 0FFFH REM = 0 ;; FE01H / FFH = 00FFH REM = 0 ;; FFFFH / 0AH = 1999H REM = 5 ;;**************************************************************************** IF1 INCLUDE \PHY353\MACLIB\STRING.MAC ENDIF ;;**************************************************************************** SSEG SEGMENT PARA STACK 'STACK' DB 64 DUP ('STACK') SSEG ENDS ;***************************************************************************** DSEG SEGMENT PARA PUBLIC 'DATA' ; Start of data segment DIVIDEND DW 0FFFH ; FFFh/FFh = 10h + F (rem) DIVISOR DW 0FFH QUOTIENT DW ? REMAINDER DW ? DSEG ENDS ; End of data segment ;**************************************************************************** CSEG SEGMENT PARA PUBLIC 'CODE' ; Start of Code Segment ASSUME CS:CSEG, DS:DSEG DIV16 PROC FAR DOS_RET_SETUP INIT_DATA_SEG DSEG

  12. START: MOV BX, DIVIDEND ;; Dividend MOV DX, DIVISOR ;; Divisor SUB AX, AX ;; Init AX and set CF = 0 MOV CX, 17 ;; Number of bits in dividend + 1 ;; Note 17 left shifts are needed ;; to get the 16th bit out ;; If AX ge DX, subtract and then shift L NXT_BIT: SUB AX, DX JNC SHIFT ;; Shift for next bit ;; If AX lt DX then restore original number and shift ADD AX, DX ;; Add back ;; CF=0 if AX ge DX and CF=1 if AX lt DX SHIFT: CMC ;; Complement CF RCL BX, 1 RCL AX, 1 DEC CX JCXZ DONE JMP NXT_BIT ;; All bits of dividend shifted DONE: RCR AX,1 ;; Undo the last shift ;; to get the correct remainder MOV QUOTIENT, BX MOV REMAINDER, AX RET DIV16 ENDP CSEG ENDS END DIV16

  13. **************************************************************************************************************************************** Author: M. Sajjad Alam (University at Albany, SUNYA) Date : Nov 6, 1996 -------------------------------------------------------------------- Debug session of program to divide a 16 bit number by another 16 bitnumber. The numbers are stored in the data segment at dividend anddivisor. The program returns the quotient in BX and remainder in AX.The result is stored in memory at QUOTIENT AND REMAINDER. --------------------------------------------------------------------  C:\PHY353\DIV16>debug div16_m.exe -u 283C:0000 1E PUSH DS 283C:0001 2BC0 SUB AX,AX 283C:0003 50 PUSH AX 283C:0004 50 PUSH AX 283C:0005 B83B28 MOV AX,283B 283C:0008 8ED8 MOV DS,AX 283C:000A 58 POP AX 283C:000B 8B1E0000 MOV BX,[0000] 283C:000F 8B160200 MOV DX,[0002] 283C:0013 B91100 MOV CX,0011 283C:0016 2BC0 SUB AX,AX 283C:0018 2BC2 SUB AX,DX 283C:001A 7302 JNB 001E 283C:001C 03C2 ADD AX,DX 283C:001E F5 CMC 283C:001F D1D3 RCL BX,1 283C:0021 D1D0 RCL AX,1 283C:0023 49 DEC CX 283C:0024 E302 JCXZ 0028 283C:0026 EBF0 JMP 0018 283C:0028 D1D0 RCL AX,1 283C:002A 891E0400 MOV [0004],BX 283C:002E A30600 MOV [0006],AX 283C:0031 CB RETF --------------------------------------------------------------------------------------------------------- Here we step through one complete cycle. We start at the point wherethe dividend and divisor have been loaded in BX and DX. The Counteris loaded with 17 rather than 16, since we have to shift 16 bits andperform the last subtraction. ---------------------------------------------------------------------------- -------------------------------

  14. -g16 AX=0000 BX=0FFFCX=0011DX=00FFSP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0016 NV UP EI PL ZR NA PE NC 283C:0016 2BC0 SUB AX,AX ---------------------------------------------------------------------- AX is initialized and the CF=0 or set to NC ---------------------------------------------------------------------- AX=0000BX=0FFFCX=0011DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0018 NV UP EI PL ZR NA PE NC 283C:0018 2BC2 SUB AX,DX -t ---------------------------------------------------------------------- We subtract the divisor from AX and check whether [AX} > [BX]. Here the carry flag =1 or set to CY, so the answer is no. Therefore, we will add DX back to AX and try to shift a digit left from register BX. ----------------------------------------------------------------------- AX=FF01BX=0FFFCX=0011DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=001A NV UP EI NG NZ AC PO CY 283C:001A 7302 JNB 001E -t AX=FF01BX=0FFFCX=0011DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=001C NV UP EI NG NZ AC PO CY 283C:001C 03C2 ADD AX,DX -t ----------------------------------------------------------------------- Before we ready to shift a digit left from BX, we need to complement the carry flag from CY to NC, i.e. CF=0. While we are shifting a digit from the divisor in BX, we will shift a '0' to the low bit of BX; this is the LSBof the quotient. ------------------------------------------------------------------------ AX=0000BX=0FFFCX=0011DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=001E NV UP EI PL ZR AC PE CY 283C:001E F5 CMC -t AX=0000BX=0FFFCX=0011DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=001F NV UP EI PL ZR AC PE NC 283C:001F D1D3 RCL BX,1 -t AX=0000BX=1FFECX=0011DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0021 NV UP EI PL ZR AC PE NC 283C:0021 D1D0 RCL AX,1 ------------------------------------------------------------------------ Since the MSB of the divisor in BX is 0, AX remains unchanged. ------------------------------------------------------------------------

  15. ------------------------------------------------------------------------------------------------------------------------------------------------------------ AX=0000BX=1FFECX=0011DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0023 NV UP EI PL ZR AC PE NC 283C:0023 49 DEC CX -t ------------------------------------------------------------------------ The counter is decremented by 1 since we have completed a shift. ------------------------------------------------------------------------ AX=0000BX=1FFECX=0010DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0024 NV UP EI PL NZ NA PO NC 283C:0024 E302 JCXZ 0028 -t ------------------------------------------------------------------------ We then check the contents of CX to see if it is '0'. This is the first shift so this is not true. We, therefore, go the next instruction. This will send us to the beginning of the loop at location offset :0018 ------------------------------------------------------------------------ JMP 0018 -t ----------------------------------------------------------------------- We will now jump to the near the end of loop by using -g26. We then do one more step to come back to the end of loop. Doing another step brings us to the beginning of the loop. ----------------------------------------------------------------------- AX=0000BX=1FFECX=0010DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0018 NV UP EI PL NZ NA PO NC 283C:0018 2BC2 SUB AX,DX -g26 AX=0000BX=3FFCCX=000FDX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0026 NV UP EI PL NZ AC PE NC 283C:0026 EBF0 JMP 0018 -t AX=0000BX=3FFCCX=000FDX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0018 NV UP EI PL NZ AC PE NC 283C:0018 2BC2 SUB AX,DX -g26 ---------------------------------------------------------------------- We repeat the above procedure until the '1' in the highest bit of BX starts coming into the least bit of AX. ----------------------------------------------------------------------

  16. AX=0001BX=FFE0CX=000CDX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0026 NV UP EI PL NZ NA PE NC 283C:0026 EBF0 JMP 0018 -t AX=0001BX=FFE0CX=000CDX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0018 NV UP EI PL NZ NA PE NC Ò283C:0018 2BC2 SUB AX,DX -g26 AX=0003BX=FFC0CX=000BDX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0026 NV UP EI PL NZ NA PO NC 283C:0026 EBF0 JMP 0018 -t AX=0003BX=FFC0CX=000BDX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0018 NV UP EI PL NZ NA PO NC 283C:0018 2BC2 SUB AX,DX -g26 AX=0007BX=FF80CX=000ADX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0026 NV UP EI PL NZ NA PE NC 283C:0026 EBF0 JMP 0018 -t AX=0007BX=FF80CX=000ADX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0018 NV UP EI PL NZ NA PE NC 283C:0018 2BC2 SUB AX,DX -g26 AX=000FBX=FF00CX=0009DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0026 NV UP EI PL NZ NA PE NC 283C:0026 EBF0 JMP 0018 -t AX=000FBX=FF00CX=0009DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0018 NV UP EI PL NZ NA PE NC 283C:0018 2BC2 SUB AX,DX ----------------------------------------------------------------------- We will continue the above procedure until We have shifted FF from [BX] to [AX] as shown below. ----------------------------------------------------------------------- AX=00FFBX=F000CX=0005DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0026 NV UP EI PL NZ NA PE NC 283C:0026 EBF0 JMP 0018 -t ----------------------------------------------------------------------- We have now lined up the most significant bits of the dividend and divisor. -----------------------------------------------------------------------

  17. AX=00FFBX=F000CX=0005DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0018 NV UP EI PL NZ NA PE NC 283C:0018 2BC2 SUB AX,DX -t AX=0000BX=F000CX=0005DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=001A NV UP EI PL ZR NA PE NC 283C:001A 7302 JNB 001E -t AX=0000BX=F000CX=0005DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=001E NV UP EI PL ZR NA PE NC 283C:001E F5 CMC -t ------------------------------------------------------------------------- The first real division has take place, the carry flag was NC since the subtraction was valid. We need to complement this to CF=1. This is the least significant bit of the quotient. This will be shifted from CF to the least significant position BX to become the first significant bit of the quotient. ------------------------------------------------------------------------- AX=0000BX=F000CX=0005DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=001F NV UP EI PL ZR NA PE CY 283C:001F D1D3 RCL BX,1 -t AX=0000BX=E001CX=0005DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0021 NV UP EI PL ZR NA PE CY 283C:0021 D1D0 RCL AX,1 -t ------------------------------------------------------------------------ You can now see the LSB of BX is the highest significant bit of the quotient. In the meantime the next bit of the divisor has been shifted into AX. ------------------------------------------------------------------------ AX=0001BX=E001CX=0005DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0023 NV UP EI PL ZR NA PE NC 283C:0023 49 DEC CX -t AX=0001BX=E001CX=0004DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=283B ES=2817 SS=2827 CS=283C IP=0024 NV UP EI PL NZ NA PO NC 283C:0024 E302 JCXZ 0028 -t ---------------------------------------------------------------------- We keep shifting until we again have F in register AX; for each shift we shift a '0' left into AX. Four more shifts will achieve this. Note that BX now contains the quotient. But the remainder in AX is incorrect since we have shifted it to the left once too often. We correct his by doing rotate circular right to get the correct value of the remainder. ----------------------------------------------------------------------

  18. AX=001EBX=0010CX=0000DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=288B ES=2867 SS=2877 CS=288C IP=0028 NV UP EI PL ZR NA PE NC 288C:0028 D1D8 RCR AX,1 -t AX=000FBX=0010CX=0000DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=288B ES=2867 SS=2877 CS=288C IP=002A NV UP EI PL ZR NA PE NC 288C:002A 891E0400 MOV [0004],BX DS:0004=0000 -t ----------------------------------------------------------------------- Above we now have the remainder in AX and quotient in BX. We are now ready to start storing the answers in the data segment of the memory. ----------------------------------------------------------------------- AX=000FBX=0010CX=0000DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=288B ES=2867 SS=2877 CS=288C IP=002E NV UP EI PL ZR NA PE NC 288C:002E A30600 MOV [0006],AX DS:0006=0000 -t AX=000FBX=0010CX=0000DX=00FF SP=013C BP=0000 SI=0000 DI=0000 DS=288B ES=2867 SS=2877 CS=288C IP=0031 NV UP EI PL ZR NA PE NC 288C:0031 CB RETF -----------------------------------------------------------------------

More Related