preload preload preload preload
Showing posts with label Assembly. Show all posts
Showing posts with label Assembly. Show all posts

Sunday, January 24, 2010

Assembly Language Part 3

For 8086 programs to perform useful tasks, there must be a way to make decisions and repeat sections of the code. Inthis section we will discuss the the basic flow control instructions of MASM

In assembly program flow is managed by jumping isntructions. There are 2 types of jump

1) Unconditional
             The JMP instruction causes an unconditional transfer of control. The basic form is-
            JMP label

2)Conditional
            The transfer of control depends on the previous instruction. It is often done by CMP. for example
          CMP operand1,operand2
          Jxx    end

Here the jump Jxx can be any of the following -


Jcc Instructions for Signed Comparisons
Instruction
Description
Condition
Aliases
Opposite
JG
Jump if greater (>)
Sign = Ovrflw or Zero=0
JNLE
JNG
JNLE
Jump if not less than or equal (not <=)
Sign = Ovrflw or Zero=0  
JG
JLE
JGE
Jump if greater than or equal (>=)
Sign = Ovrflw
JNL
JGE
JNL
Jump if not less than (not <)
Sign = Ovrflw
JGE
JL
JL
Jump if less than (<)
Sign Ovrflw
JNGE
JNL
JNGE
Jump if not greater or equal (not >=)
Sign Ovrflw  
JL
JGE
JLE
Jump if less than or equal (<=)
Sign Ovrflw or Zero = 1
JNG
JNLE
JNG
Jump if not greater than (not >)
Sign Ovrflw or Zero = 1
JLE
JG
JE
Jump if equal (=)
Zero = 1
JZ
JNE
JNE
Jump if not equal ()
Zero = 0
JNZ
JE






Jcc Instructions That Test Flags
Instruction
Description
Condition
Aliases
Opposite
JC
Jump if carry
Carry = 1
JB, JNAE
JNC
JNC
Jump if no carry
Carry = 0
JNB, JAE
JC
JZ
Jump if zero
Zero = 1
JE
JNZ
JNZ
Jump if not zero
Zero = 0
JNE
JZ
JS
Jump if sign
Sign = 1
-
JNS
JNS
Jump if no sign
Sign = 0
-
JS
JO
Jump if overflow
Ovrflw=1
-
JNO
JNO
Jump if no Ovrflw
Ovrflw=0
-
JO
JP
Jump if parity
Parity = 1
JPE
JNP
JPE
Jump if parity even
Parity = 1
JP
JPO
JNP
Jump if no parity
Parity = 0
JPO
JP
JPO
Jump if parity odd
Parity = 0
JNP
JPE








Jcc Instructions for Unsigned Comparisons
Instruction
Description
Condition
Aliases
Opposite
JA
Jump if above (>)
Carry=0, Zero=0
JNBE
JNA
JNBE
Jump if not below or equal (not <=)
Carry=0, Zero=0
JA
JBE
JAE
Jump if above or equal (>=)
Carry = 0
JNC, JNB
JNAE
JNB
Jump if not below (not <)
Carry = 0
JNC, JAE
JB
JB
Jump if below (<)
Carry = 1
JC, JNAE
JNB
JNAE
Jump if not above or equal (not >=)
Carry = 1
JC, JB
JAE
JBE
Jump if below or equal (<=)
Carry = 1 or Zero = 1
JNA
JNBE
JNA
Jump if not above (not >)
Carry = 1 or Zero = 1
JBE
JA
JE
Jump if equal (=)
Zero = 1
JZ
JNE
JNE
Jump if not equal ()
Zero = 0
JNZ
JE




 As we have seen earlier that the conditional jump isntruction is often followed by the CMP instruction. Wnen CPU executes the CMP instruction it compares the first operand with the second operand. Then if the condition described by the following jump instruction is true - a change of flow control is occured. You may want to see the example -

       CMP AX,BX
       JL  axislower    ;  If AX is lower than BX then control transferred to axislower

The TEST instruction
    The TEST instruction performs an AND operation, but does not store
the result. It only sets the FLAGS register based on what the result would
be (much like how the CMP instruction performs a subtraction but only sets
FLAGS).

TEST DESTINATION, SOURCE


Effect on flags -
SF,ZF, PF - Reflect the result
AF  -  Undefined
CF,OF - 0                    

The test instruction can be used to examine individual bits in a operand. For that case the mask(source) should contain 1s in the desired bit positions and 0s in the rest. See the pseudocode and MASM code -

IF AL is EVEN
   THEN JUMP TO END_X

MASM code-

TEST AL,1
JZ END_X

END_X:
  

If we wish to check the 1st bit we TEST it by 1. (here the mask is 1 i.e = 0000 0001 in binary. And we know that an even number shall have 1 in the 1st bit position.)
The and product should be zero if 1st bit is 0, the AL would then remain same but the ZF (zero flag = 0 if result is zero) is 0. So the JZ instruction shall be executed.

 Further reading:
 The art of Assembly Language             URL:  http://www.arl.wustl.edu/

 The Intel Microprocessor Architecture, Programming & Interfacing
    by  Barry B. Brey

Assembly Language Part 4

Conditional Branching in MASM
We have shown that the jump instructions can be used to implement branches and looops. However these instructions are difficult for beginners to apply as a substitute of high level branching statements.

In high level languages we use these basic structures -

IF-THEN
The if-then structure is the most common type of branching statement used. The pseudocode for IF-THEN is :


IF  CONDITION IS TRUE
     THEN EXECUTE TRUE BRANCH STATEMENTS
END_IF

MASM example:
Suppose we want to code the following pseudocode in MASM

IF AX< 0
    THEN REPLACE AX WITH ITS COMPLIMENTED VALUE
END_IF

The MASM code will be:

CMP AX,0
   JNL END_IF
   NEG AX

END_IF:

IF-THEN-ELSE
The if-then-else structure is the expansion of most common type of branching statement IF-THEN. The pseudocode for IF-THEN-ELSE is :

IF   Condition is TRUE
THEN
Execute true-branch statements
ELSE
Execute false-branch statements
END_IF

Lets explain by applying the following pseudocode in MASM

IF AL<= BL
  THEN
      DISPLAY THE CHARACTER IN AL
  ELSE
      DISPLAY THE CHARACTER IN BL
 END_IF

The MASM code will be -

MOV AH,2
CMP AL,BL
   JNBE ELSE_
   MOV DL,AL
   JMP DISPLAY_
ELSE_:
   MOV DL,BL
DISPLAY_:
   INT 21H

END_IF:


CASE
The case in high-level language is a multiway branch structure that tests a register, variable or expression for particular values or range of values. The pseudocode is :

CASE expression
    value_1: statement_1
    value_2: statement_2
    value_3: statement_3
    value_4: statement_4
    .................................
    value_n: statement_n
END_CASE

Consider the following pseudocode in MASM

IF AX a negative number
   THEN put -1 in BX
IF AX contains 0
   THEN put 0 in BX
IF AX contains a positive number
   THEN put 1 in BX

The MASM code will be -

CMP AX,0
   JL   NEGATIVE_
   JG   POSITIVE
 
   MOV BX,0
   JMP END_CASE

 NEGATIVE_:
   MOV BX,-1
   JMP END_CASE

POSITIVE_:
  MOV BX,1
  JMP END_CASE

END_CASE:


further reading:
http://www.cs.princeton.edu/

Assembly Language Programming and Organization of IBM PC
                                                                    Ytha Yu
                                                                    Charles Marut

Assembly Language Part 5

Looping instructions in Assembly



What comes to your mind after watching the loop at the left? That's it the Head is Touching the Tail at some point.

In assembly a loop is a sequence of instructions that is repeated like the loop to the left.
The final instruction (Tail) jumps (touches) to the first instruction (Head).

The basic form of looping in Assembly is

REPEAT UNTIL
REPEAT
      STATEMENTS
UNTIL CONDITION

For example if we want to read all input characters given by the user until he/she inputs

    MOV AH,1
REPEAT_:
    INT 21H
    CMP AL,' '
    JNE REPEAT_ 

Some other frequently used looping structures are-


FOR LOOP
In these kind of lop structures the instructions are repeated for known number of times. pseudocode -

FOR loop_count times DO
     statements
END_FOR

In assembly we use the instruction LOOP to implement for loop. The general form is-

LOOP header_label

Here header label is the name or label of the instruction block to be executed in the loop. The LOOP instruction uses CX as the counter so it has to be defined first. When the program executes LOOP it first checks if CX = 0 if true it continues to execute the next instructions. If the CX !=0 then control is transferred to the "header_label" , at the same time it also decreases the value of CX by 1. 

Let us implement it in the program to print 100 '*' in assembly.

MOV CX,80
MOV AH,2
MOV DL,'*'

TOP:                            ; header_label
   INT 21H
   LOOP TOP              



WHILE LOOP
These kind of loops depends on a condition. pseudocode -


WHILE   condition DO
   statements
END_WHILE

The condition is checked at the top of the loop. If the condition is satisfied the the statements are executed. If false  the program skips to whatever follows. It is possible that the condition is never satisfied (even at the initial stage) in that case the body is never executed.

Lets count the number of character in the input line.

MOV DX,0
MOV AH,1
INT 21H

WHILE_:
     CMP AL,13
     JE END_WHILE
     INC  DX
     INT 21H
     JMP WHILE_

END_WHILE:


The basic difference between WHILE and REPEAT loop is that using WHILE loop gives opportunity to skip the whole process even at the initial stage if the condition is false.
On the other hand the statements of REPEAT has to be done atleast once. REPEAT is useful on other cases as it leads to shorter coding.

Further reading:
Assembly Language Programming and Organization of IBM PC
                                                                    Ytha Yu
                                                                    Charles Marut
http://www.cs.princeton.edu/

Assembly Language Part 6

Logic Instructions

The ability to manipulate the individual bits of a Data is one of the key advantages of assembly language. In order to do so we use Logic instructions. Let us first remind the basic ligic instructions for now,



AND 
 0 0 -> 0
 1 0 -> 0
 0 1 -> 0
 1 1 -> 1

OR 
 0 0 -> 0
 1 0 -> 1
 0 1 -> 1
           1 1 -> 1            



XOR 
 0 0 -> 0
 1 0 -> 1
 0 1 -> 1
 1 1 -> 0


NOT

 0 -> 1
 1 -> 0
 

Now lets see where we use these functions in Assembly -

AND   ->  To clear
OR     ->  To set
XOR   ->  To compliment desired bit positions
NOT   ->  To compliment all bits

AND (Used to Clear )

The form is
AND DESTINATION, SOURCE

Suppose we want to clear the sign bit of AL then the code will be

AND AL,7Fh                            ;

we know that the sign bit is the MSB so if we AND it with the value which has all 1s except of the MSB as 0 then

AL   =   10101100
Msk =   01111111

-----------------------------
rslt  =   00101100


OR (used to set)

The form is
OR DESTINATION, SOURCE

Suppose we want to set the sign bit of AL then the code will be

OR AL,80h                            ;

we know that the sign bit is the MSB so if we OR it with the value which has all 0s except of the MSB as 1 then

AL   =   00101100
Msk =   10000000
-----------------------------
rslt  =   10101100

XOR (Used to compliment selected bits )

The form is
XOR DESTINATION, SOURCE

Suppose we want to change the sign bit of AL then the code will be

XOR AL,80h                            ;

we know that the sign bit is the MSB so if we XOR it with the value which has all 0s except of the MSB which is 1 then


AL   =   10101100
Msk =   10000000
-----------------------------
rslt  =   00101100

Here you may have noticed that destination bit is complimented if that bit position of the Mask is 1 and unchanged if it is 0. (XOR x , 0 - x) (XOR  x,1)= x'



NOT (Used to compliment all bits )

The form is
NOT DESTINATION

Suppose we want to invert all the bits of AL then the code will be

NOT AL                            ;

You can see that NOT takes only one argument.

Suppose AL = 10110010 , after executing the NOT instruction it will be 01001101


Problem (Convert ASCII value to number)
When we enter a character in Assembly the equivalent ASCII value is stored in the memory. Say we pressed 3 then the ASCII value 33h will be saved in AL. we have to convert it to 3...

If we AND a value with 0000  1111 then the relust will be 30 less than the original value

AL   =   0011 0011   =33
Msk =   0000 1111
------------------------------
             0000 1111   =3

so the code will be

AND AL, 0Fh

Assembly Language Part 7

Shift Instructions in Assembly

The shift and rotate instructions shift the bits of the operand by one or more bit positions. The shifting can be either left or right. The basic shift instructions are -

SHL (shift left)

The SHL instruction shifts the bit of the destination to the left. The format for calling the SHL is-

SHL destination,steps


Applying this the MSB is shifted to the carry flag and LSB is filled with 0 from right. The bits of the middle is shifted to 1 bit left when steps are 1


              CF  <-  [<-][<-][<-][<-][<-][<-][<-][<-]   <- 0
                           7     6    5    4    3    2    1    0

Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHL DH, CL



                           [1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ]  
                           7     6    5    4    3   2   1   0

after first shift

    CF [ 1 ]   <-   [0 ][0 ][0 ][1 ][0 ][1 ][0 ][ 0 ]  <- 0  
                           7     6    5    4    3   2   1   0

2nd shift

    CF [ 0 ]   <-   [0 ][ 0 ][ 1 ][ 0 ][ 1 ][ 0 ][ 0 ][0 ]  <- 0  
                           7    6     5    4    3     2    1    0

After 3rd and the final shift
    CF [ 0 ]   <-   [0 ][ 1 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 0 ]  <- 0  
                           7    6    5     4    3     2    1    0



SHR (shift right)

The SHR works like the reverse function of SHL. It shifts the bits of the destination operand to the right. The format is

SHR destination, steps

If we apply SHR the MSB is filled with 0 and the LSB is moved to CF and the bits between are mvoved to the same direction.

               0  ->  [->][->][->][->][->][->][->][->]   -> CF
                           7    6    5    4    3    2    1    0


Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHR DH, CLworks as follow -


                           [1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ]  
                           7     6    5    4    3   2   1   0

after first shift

               0  ->  [ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 1 ]  ->   [0] CF  
                           7     6    5    4    3    2     1    0

then the 2nd
               0  ->  [ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ]  ->   [1]CF  
                           7     6    5    4    3    2     1    0


Finally the 3rd

                0  ->  [ 0 ][0 ][0 ][1 ][0 ][0 ][0 ][ 1 ]  ->      [0]CF  
                           7    6   5    4   3   2    1   0


SAR (shift arithmetic right)

The only difference between SHR and SAR is that apllying SAR the MSB retains the original value.

SAR destination, steps


If we apply SAR the MSB is unchaged and it is also copied to the next bit to the right of MSB,  the LSB is moved to CF and the bits between are mvoved to the
same direction.


                         [^>][->][->][->][->][->][->][->]   -> CF
                           7      6    5    4    3    2    1    0


Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHR DH, CLworks as follow -



                           [1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ]  
                           7     6    5    4    3   2   1   0

after first shift

                         [ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 1 ]  ->   [0] CF  
                           7     6    5    4    3    2     1    0

then the 2nd
                        [ 1 ][ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ]  ->   [1]CF  
                           7     6    5    4    3    2     1    0



Finally the 3rd

                0  ->  [ 1 ][ 1 ][ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ]  ->  [0]CF  
                           7     6    5     4    3     2     1   0

These shifting instructions are handy for multiplication and division. As you may notice that the difference of subsequent binary digits change as if they are multuplied by 2
            16     8    4     2     1  . . . .
So if we shift the bits to the right it is simply multiplied by 2 !

                    0 1 1 1 b    =  7d

after 1 SHL

                    1 1 1 0 b    =  14d !


Similarly shifting the bits right divides them by 2



                    0 1 0 0 b    =  4d

after 1 SHR


                    0 0 1 0 b    =  2d  !


so if we want to divide the binary number by 4 all we need to do is to shift it right 2 times.


ROR and ROL (rotate right and rotate left)

The ROR and ROL not only shift the bits to certain direction but also move the LSB to MSB or MSB to LSB



ROR

ROL

As you can see that there is a loop. For the case of ROR the LSB is also copied to CF. and for the case of ROL the MSB is copied to CF


RCR and RCL (rotate right /left through carry )

In the case of rotate throug carry the loop is also gone through CF. i.e for the case of RCL the MSB is moved to CF, the CF is moved to LSB and LSB is moved to LSB +1 , and so on

 RCL



RCR


Further reading :
The Intel Microprocessor Architecture, Programming and Interfacing
                                                                     Barry B. Brey

Assembly Language Part 8

Stack operations in Assembly

A stack is a one dimensional data structure. The elements of the stack is added and removed from one end. That is why it is also known as
Last-In-First-Out or LIFO system. That end of the stack is called TOP.

You can have the Idea by watching the stack of papers to the right. It is only possible to add or remove new paper holders from the TOP.

The add operation on a stack is called push and the remove operation on the stack is called pop.

As we mentioned earlier the operations are only done at the top.



Stack Operations Push and Pop

PUSH ( add element to top)
The push instruction adds a new element to the top. The syntax is

PUSH source  ; no destination defined because destination is always TOP

A few things to notice is that the PUSH operation works as follows
   1)  A copy of the source content is copied to the address specified by the TOP     

   2)  SP or TOP is decreased





                                             Push                          Pop

POP ( remove element to top)
The pop instruction removes the top element. The syntax is


POP destination  ; no source defined because source is always TOP



A few things to notice is that the PUSH operation works as follows
   1)  SP or TOP is increased    

   2) The content of the TOP is copied to the destination                                     


The PUSH and the POP instructions take 16 bit registers only. So
PUSH DL
and
PUSH 3
are both illegal

PUSHF ( a variation of PUSH)
There are no operand of PUSHF. It just copies the FLAGS register to the stack.

POPF ( a variation of POP)
There are no operand of POPF either , it does the reverse of PUSHF. Executing it restores the FLAGS register from the stack TOP.

Example ( Reversing a line of text )
The program shall take one character at a time and PUSH it to the stack until its a carriage return. When "Enter" is pressed the characters are POPed out and printed.


CODE:

.MODEL SMALL
.STACK 100H
.CODE
 MAIN PROC

        MOV AH, 2
        MOV DL, ‘?’
        INT 21H

 XOR CX, CX

        MOV AH, 1
        INT 21H


WHILE:
        CMP AL, 0DH
        JE END_WHILE
        PUSH AX
        INC CX
        INT 21H
     JMP WHILE
 

END_WHILE:
 

        MOV AH, 2
        MOV DL, 0DH
        INT 21H
        MOV DL, 0AH
        INT 21H
    JCXZ EXIT


TOP:
     POP DX
     INT  21H
     LOOP TOP

EXIT:
      MOV  AH, 4CH
      INT 21H

MAIN  ENDP
END MAIN

Assembly Language Part 9

Procedures in Assembly

The Idea of procedure is to divide the main problem into sub problems and acquire the solution by solving the individual sub problems and combining through calling them from a mother problem.

The Idea is similar to functions() as used in High level languages. When a procedure call one another the control is transferred from the caller to the called, when the called procedure is done it usually returns the control to the caler function immediately after the calling statement.

The basic form of the procedure is

name PROC type
;  body of the procedure
RET
name ENDP

There are 2 types of procedures. NEAR and FAR. A procedure is called near if the procedure and the main program is in the same segment. If the procedure is in other segment then the proc is called FAR.

To invoke a procedure we write the following command


CALL name_of_proc


How is control returned?
The control is returned from the procedure to caller program when the RET instruction is executed. Every procedure should have a RET instruction in some place.

MAIN PROC
   ..............
   ..............
 CALL PROC_1
   statement_x
   ..............
   ..............




PROC_1 PROC
   ..............
   ..............
   RET

In the program mentioned above in a stage of execution of MAIN PROC the statement CALL PROC_1 will occur. This will transfer the control of the program to PROC_1 and the instructions in PROC_1 shall continue to execute. When the instruction RET is reached the control is returned to MAIN PROC and the program shall resume to execute from statement_x

Example ( factorial ! )
Let us describe the application of proc by finding the factorial

.MODEL SMALL
.STACK 100H
.DATA
        MSG1   DB    “This is from procedure_1$”
        MSG2  DB  0DH, 0AH, “This is from main procedure$”
.CODE
MAIN PROC
        MOV AX, @DATA
        MOV DS, AX

CALL PROCEDURE_1


        LEA DX,MSG2
        MOV AH,9

        INT 21H

     MOV AH, 4CH
     INT 21H
MAIN ENDP

PROCEDURE_1 PROC
        LEA  DX, MSG1
        MOV AH, 9
        INT 21H
  RET
PROCEDURE_1 ENDP

END MAIN

Macro in Assembly

The Idea of Macro is somewhat similar of procedure, divide the main problem into sub problems and acquire the solution by solving the individual sub problems. But macro at all is not like a procedure. It is just a block of program that has a name. The syntax for defining macro is -

macro_name      MACRO  operand1,operand2, . . . .
       statements
ENDM

Inside the Program we call the macro just by -

macro_name (operands)

When a macro is invoked, the program control does not jump as it does in the case of procedure. Instead the block of program defined by the macro name is copied to the place where it occurs before executing.

Example: Factorial using MACRO

.model small
fact macro 
    mov cx,var1
    xor ax,ax
    mov ax,1
    top:
    mul cx
    loop top   
    
    call outdec
 endm
    
.stack 100h
.data 
msg1 db 0ah,0dh,"Factorial is=$"  
msg2 db "Enter a number:$"  
;msg3 db 0ah,0dh,"your entered value is out of range!!$" 
var1 dw 0
;limit dw 0
.code    
main proc
    mov ax,@data
    mov ds,ax 
    lea dx,msg2
    mov ah,9
    int 21h
    call indec
    mov var1,ax  
    lea dx,msg1
    mov ah,9
    int 21h
    fact
    mov cx,ax
    mov ah,4ch
    int 21h
 
   main endp
    include outdec.asm
    include indec.asm   
end main

Assembly Language Part 10

Multiplication instructions in MASM

The process of multiplying and dividing is different for signed and unsigned numbers. So they use different instructions. The intructions are also of byte and word form. When we multiply 2 numbers of byte form the result will be word form or 2 bytes. When we multiply 2 number of word form the result is double word. The basic multiplication instructions are -

MUL (Multiply)
MUL (Multiply) is used for multiplying unsigned numbers. The form of invokation is-

MUL source

The instruction shall multiply the value of AL/AX with source and store it in DX:AX. The source may be a register or a memory byte but not a constant number. To invoke a byte form of multiplication the source should be 8bit and the destination should be in AL. For word form of multiplication the source is 16bit and the destination is stored in AX. In this case the result is stored in AX(lower word) and DX(upper word)


Example 
Byte form of MUL

Suppose AL contains 80h and BL contains 0ffh

MUL BL

Dec Product      Hex Product      AH      AL       CF/OF
128                          7F80            7F       80           1*


Word form of MUL


Suppose AX contains FFFh and BX contains FFFFh


MUL BX


Dec Product     Hex Product     DX        AX     CF/OF
4294836225       FFFE0001      FFFE     0001      1*


IMUL (Integer Multiply)
IMUL is used for multiplying signed numbers. The form of invokation is-

IMUL source

The instruction shall multiply the value of AX /ALwith source and store it in AX/DX:AX. The source may be a register or a memory byte but not a constant number. To invoke a byte form of multiplication the source should be 8bit and the destination should be in AL. For word form of multiplication the source is 16bit and the destination is stored in AX. In this case the result is stored in AX(lower word) and DX(upper word)


Example 
Byte form of IMUL

Suppose AL contains 80h and BL contains 0ffh

MUL BL

Dec Product   Hex Product    AH    AL      CF/OF
128                    0080            00      80          1*

Word form of IMUL


Suppose AX contains FFFh and BX contains FFFFh


IMUL BX


Dec Product     Hex Product     DX           AX       CF/OF
1                           00000001     0000        0001        0*


*Effect of MUL and IMUL in CF and OF
 after invoking MUL and IMUL the flags SF,ZF,AF,PF are unchanged. The flags CF and OF are changed as-
After MUL CF/OF                                = 0 if upper half of the result is 0
                                                               = 1 other wise

After IMUL CF/OF                               = 0 if the upper half of the result is the sign extension of the lower half
                                                               = 1 otherwise


For both case the CF/OF =1 means that the result is too big to fit in the lower half of destination.


Division instructions in MASM

The process of dividing is also different for signed and unsigned numbers. So they use different instructions. The intructions are also of byte and word form. When we divide a number of word form with a divider of byte form the result is also a byte form. We can also divide a 32 bit dividend by a 16 bit divisor. In this case the dividend is in DX:AX. The instructions are -

DIV (Divide)
DIV is used for unsigned division. The form is-

DIV divisor

The instruction shall divide the value of AX/DX:AX with divisor and store the quotient in AL/AX and remainder in AH/DX. The divisor may be a register or a memory location but not a constant number.


Example 
Word form of DIV

Suppose AX contains 00FBh and BL contains FFh

DIV BL

Dec Quotient         Dec Remainder     AH           AL           
0                                  251                 FBh          00h               

Double Word form of DIV


Suppose DX contains 0000h and AX contains 0005h and BX contains 0002h


DIV BX


Dec Quotient    Dec Remainder     AH        AL           
2                               1               0002h     0001h               


IDIV (Integer Divide)
IDIV is used for dividng signed numbers. The form is-

IDIV  divisor

The instruction shall divide the signed value of AX/DX:AX with divisor and store the quotient in AL/AX and remainder in AH/DX. The divisor may be a register or a memory location but not a constant number.


Example 
Word form of IDIV

Suppose AX contains 00FBh and BL contains FFh

IDIV BL

Dec Quotient      Dec Remainder      AH        AL           
Dvide overflow**         **                     **            **               

Double Word form of IDIV

Suppose DX contains 0000h and AX contains 0005h and BX contains FFFEh


IDIV BX

Dec Quotient      Dec Remainder     AH              AL           
-2                             1                 FFFEh       0001h           

**Division overflow
It is possible that overflow might occur when diving, because the quotient and reminder are set to be half of the dividend by default. In any case were the divisor is so much small that can cause a larger result causes overflow. In the example above the dividend is FBh = 251 and the divisor is -1 so the quotient should be -251 which cannot be fit in AL