• No results found

UNSIGNED DIVISION DIV -Divide

In document Lyla b das3.pdf (Page 32-35)

2) ADC –Add with carry

3.4.5 UNSIGNED DIVISION DIV -Divide

Usage: DIV source

This instruction divides AX or DX –AX by the source ,where the source can be a register or a memory location ,but not an immediate number. All the conditional flags are affected ,but undefined –hence they don’t give any interpretation or information about the result.

The destination depends on the size of the operand .There are two ways of performing division

i) Divide a word by a byte .

Here ,the dividend must be a word placed in AX and the source must be a byte . The result of division causes AL to contain the quotient ,and AH to contain the remainder.

Example

DIV BL ;divide AX by the byte in BL

DIV BYTE PTR [BX] ; divide the word in AX by the byte pointed by BX DIV DIG ; divide the word in AX by the byte in DIG

See this division MOV AX, 0C678H

MOV CL ,0F9H

This program segment will cause AH (remainder) =0CH and AL (quotient) = CCH Dividing a byte by a byte

This is just a special case of a division of a word by a byte.

In this case ,convert the dividend byte to a word by loading the dividend in AL and 0 in AH .Thus AX will be the word that acts as the dividend.

ii)Divide a double word by a word.

In this case ,the dividend has to be in AX and DX (the upper word in DX).

The divisor should be a word . The result of this division causes the quotient to be in AX and the remainder to be in DX.

Examples

DIV BX ;divide the dword in DX-AX by the word in BX DIV WORD PTR [SI] ;divide the dword in DX-AX by the word pointed ;by SI

DIV ANGLE ; divide the dword in DX-AX by the word in ANGLE Dividing a word by a word

Similar to the previous case ,if we want a word by word division ,extend the word in AX to be a double word by loading 0 in DX to get the dividend to be a double word in AX and DX.

Divide by Zero Error

For division ,if the divisor is zero ,the quotient becomes undefined In attempting such a division ,the 8086 will exit from this program and generate an interrupt .This state is said to a ‘divide by zero error ‘.An interrupt generated by an error ,is also termed as an exception. But division by zero is not the only condition to cause such an error . If the quotient register is too small to accommodate the quotient ,then also this happens .For example ,if the dividend is a large number and the divisor is very small ,such a condition is possible .

MOV AX,09876H MOV CL,25H DIV CL

The above program segment will give a quotient of 41E H ,which obviously cannot be accommodated in AL . If this problem had been foreseen , the solution would be to convert the dividend to a double word, and the divisor to a word. .Then the quotient will

be in AX ,which will be big enough for it ..When such an error occurs ,program execution is aborted and the assembler displays the message ‘divide overflow error’.

EXAMPLE 3-20

.MODEL SMALL

.DATA

FIRST DB 89H SECOND DB 0CAH AVG DB ?

.CODE

.STARTUP

MOV AL,FIRST ;copy FIRST to AL MOV AH,0 ;zero-extend MOV BL,SECOND ;copy SECOND to BL MOV BH,0 ;zero-extend

ADD AX,BX ;add the zero extended words MOV CL,02 ;load divisor to CL

DIV CL ;divide AX by CL MOV AVG,AL ;save quotient to AVG

.EXIT END

Example 3-20 gives a complete program for finding the average of two bytes stored in memory locations labeled FIRST and SECOND .These are copied to registers AL and BL and zero extended to convert them to words. Addition of the numbers is then done as words. This sum ,which is the dividend is in the AX register. The divisor 2 is copied to the CL register. After division ,the quotient is available in AL and the remainder in AH .For this problem ,only the quotient is important ,and this is saved to location AVG.

Now we will see a very useful and interesting application of the division operation.

This program converts a 16 bit hexadecimal number to decimal and displays the decimal number on the console after converting it to ASCII form.

To see the logic of this program ,let us take a number ,say ,246 .

i) Divide this by 10 to get a quotient of 24 and a remainder of 6 .Push the remainder on to the stack .

ii) Next ,divide the quotient again by 10 to get 2 as the quotient and 4 as remainder .Push this remainder also on to the stack.

iii) One more division causes the quotient to be zero .This is the stopping condition in the division loop..

iv) The number of division operations done is counted along with all these steps. Now pop out the remainders from the stack .This will be in the reverse order of the push operations .Thus 2 will be popped out first and 6 last .This can be displayed in this order.

v) For displaying ,each digit must be converted to its corresponding ASCII .This is done by adding 30H to each digit .

Now ,examine Example 3.21 the program that does the conversion of any 16 bit hexadecimal number to decimal and then displays it. Here, the hexadecimal ,the number

is placed in the data segment. Note that the division here is accomplished as a double

Table 3.6 gives the complete list of logical instructions and the function performed Instruction format Function Performed Flags affected 1 AND dest,src Logical AND of the two operands 3 XOR dest ,src Performs a bitwise exclusive OR of the

operands returning the result in the

operands updating the flags register without saving the result.

CF OF PF SF ZF (AF undefined)

Table 3.6 : List of Logical instructions and functions performed by them

In document Lyla b das3.pdf (Page 32-35)

Related documents