Pages

Search in This Blog

Sunday, 15 January 2012

DIVISION INSTRUCTIONS


DIVISION INSTRUCTIONS

  • DIV Instruction - Unsigned divide-Div source
When a double word is divided by a word, the most significant word of the double word must be in DX and the least significant word of the double word must be in AX. After the division AX will contain the 16 –bit result (quotient ) and DX will contain a 16 bit remainder. Again , if an attempt is made to divide by zero or quotient is too large to fit in AX ( greater than FFFFH ) the 8086 will do a type of 0 interrupt .

Example:
DIV CX                                   ; (Quotient) AX= (DX:AX)/CX 
                                             ; (Reminder) DX=(DX:AX)%CX
For DIV the dividend must always be in AX or DX and AX, but the source of the divisor can be a register or a memory location specified by one of the 24 addressing modes.
If you want to divide a byte by a byte, you must first put the dividend byte in AL and fill AH with all 0’s . The SUB AH,AH instruction is a quick way to do. If you want to divide a word by a word, put the dividend word in AX and fill DX with all 0’s. The SUB DX,DX instruction does this quickly.

Example:
                                           ; AX = 37D7H = 14, 295 decimal and BH = 97H = 151 decimal
DIV BH                                 ;AX / BH
                                           ; AX = Quotient = 5EH = 94 decimal and AH = Remainder = 65H = 101 
 decimal.

  • IDIV Instruction - Divide by signed byte or word IDIV source
This instruction is used to divide a signed word by a signed byte or to divide a signed double word by a signed word.  If source is a byte value, AX is divided by register and the quotient is stored in AL and the remainder in AH.  If source is a word value, DX:AX is divided by register,and the quotient is stored in AL and the remainder in DX.

Example:
IDIV BL                                 ;Signed word in AX is divided by signed byte in BL

  • AAD Instruction - ASCII adjust before Division
ADD converts unpacked BCD digits in the AH and AL register into a single binary number in the AX register in preparation for a division operation. Before executing AAD, place the Most significant BCD digit in the AH register and Last significant in the AL register. When AAD is executed, the two BCD digits are combined into a single binary number by setting AL=(AH*10)+AL and clearing AH to 0.

Example:
MOV AX,0205h                    ;The unpacked BCD number 25
AAD                                    ;After AAD , AH=0 and AL=19h (25).
After the division AL will then contain the unpacked BCD quotient and AH will contain the unpacked BCD remainder.

Example:
                                         ;AX=0607 unpacked BCD for 67 decimal CH=09H.
AAD                                   ;Adjust to binary before division AX=0043 = 43H =67 decimal.
DIV CH                               ;Divide AX by unpacked BCD in CH, AL = quotient = 07 unpacked BCD, AH = remainder = 04 unpacked BCD

  • CBW Instruction - Convert signed Byte to signed word
CBW converts the signed value in the AL register into an equivalent 16 bit signed value in the AX register by duplicating the sign bit to the left. This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be the sign extension of AL.

Example:
                                        ; AX = 00000000 10011011 = - 155 decimal
CBW                                 ; Convert signed byte in AL to signed word in AX.
                                        ; Result in AX = 11111111 10011011 and  = - 155 decimal

  • CWD Instruction - Convert Signed Word to - Signed Double word
CWD converts the 16 bit signed value in the AX register into an equivalent 32 bit signed value in DX: AX register pair by duplicating the sign bit to the left.
The CWD instruction sets all the bits in the DX register to the same sign bit of the AX register. The effect is to create a 32- bit signed result that has same integer value as the original 16 bit operand.

Example:
Assume AX contains C435h. If the CWD instruction is executed, DX will contain FFFFh since bit 15 (MSB) of AX was 1. Both the original value of AX (C435h) and resulting value of DX : AX (FFFFC435h) represents the same signed number.

Example:
                                   ;DX = 00000000 00000000 and AX = 11110000 11000111 = - 3897 decimal
CWD                            ;Convert signed word in AX to signed double word in DX:AX
                                   ;Result DX = 11111111 11111111 and AX = 11110000 11000111 = -3897 decimal.

No comments:

Post a Comment