DIV, IDIV 

Using Division Instructions:

The DIV instruction divides unsigned numbers, and IDIV divides signed numbers. Both return a quotient and a remainder. Table 4.1 summarizes the division operations. The dividend is the number to be divided, and the divisor is the number to divide by. The quotient is the result. The divisor can be in any register or memory location but cannot be an immediate value.

Size of Operand Dividend Register Size of Divisor Quotient Remainder
16 bits AX 8 bits AL AH
32 bits DX:AX 16 bits AX DX
64 bits (80386and 80486)EDX:EAX 32 bits EAX EDX

Table: Division Operations

Unsigned division does not require careful attention to flags. The following examples illustrate signed division, which can be more complex.

signed division
       .DATA
mem16   SWORD   -2000
mem32   SDWORD  500000
        .CODE
        .
        .
        .
; Divide 16-bit unsigned by 8-bit
        mov     ax, 700               ; Load dividend      700
        mov     bl, 36                ; Load divisor DIV    36
        div     bl                    ; Divide BL       ------
                                      ; Quotient in AL      19
                                      ; Remainder in AH          16

; Divide 32-bit signed by 16-bit
        mov     ax, WORD PTR mem32[0] ; Load into DX:AX
        mov     dx, WORD PTR mem32[2] ;                 500000
        idiv    mem16                 ;              DIV -2000
                                      ; Divide memory   ------
                                      ; Quotient in AX    -250
                                      ; Remainder in DX           0

; Divide 16-bit signed by 16-bit
        mov     ax, WORD PTR mem16    ; Load into AX     -2000
        cwd                           ; Extend to DX:AX
        mov     bx,-421               ;               DIV -421
        idiv    bx                    ; Divide by BX     -----
                                      ; Quotient in AX       4
                                      ; Remainder in DX        -316

If the dividend and divisor are the same size, sign-extend or zero-extend the dividend so that it is the length expected by the division instruction. See “Extending Signed and Unsigned Integers,” later in this unit.