Thursday, December 31, 2009

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

No comments:

Post a Comment

- - Important Notice - -





Programming google group is up. If you want to join request personally to any of the managers!




Message from DIUCSP




This blog is open for all students of CS, CSE, CIS of Daffodil International University.


Join This blog to stay on touch with other students. If you have a gmail account then just sign in as the g-mail user and become a follower of the blog. You can then submit comments on our posts.

You will find information on different subjects of our course, discuss problems and have solution. Try to maintain the subjectwise discussion for early response. And remember this blog is not only for the genius or more than average CS, CSE, CIS students with great computer skills so dont feel shy in submitting your simple daily life problems (even the silliest ones) because I believe if you feel shy in learning now then you will feel shy all your life for not learning it at all.


Please be conscious about sharing your personal Data, DaffodilCSE blog moderator is not responsible for any risk you take, If you feel safe post comment as your own will, If you do not want to share your mail, comment anonymously. Thanks




They are with us

There was an error in this gadget

BlogCatalog

Earn from a cool looking gadget..! It works with Google adsense and any other advertising programs!
Grab now !