2. TOPIC:
MULTIPLICATI
ON AND
DIVISION
Group member:
M Hamza Nasir
(12063122-067)
M Usaman Ali
(12063122-086)
Syed Farhan Abbas
(12063122-009)
M Faran Ali
(12063122-055)
Ateeb Saeed
(12063122-094)
University Of Gujrat
5. MUL INSTRUCTION
īNote that the product is stored in a register (or
group of registers) twice the size of the
operands.
īThe operand can be a register or a memory
operand
9. IMUL INSTRUCTION
īSuppose AX contains 1 and BX contains FFFFh
Mov AX, 1h
Mov BX, FFFFh
IMUL BX ;
Decimal product=-1,
Hex Product =FFFFFFFFh
DX = FFFFh, AX=FFFFh CF/OF=0
DX is a sign extension of AX for this CF/OF=0
10. IMUL INSTRUCTION
īIMUL sets the Carry and Overflow flags if the
high-order product is not a sign extension of
the low-order product
Mov al, 48
Mov bl, 4
Imul bl ;AX = 00C0h, OF = 1
AH is not a sign extension of AL, so the
Overflow flag is set
11. IMUL INSTRUCTION
Suppose AX contains FFFFh and BX contains FFFFh
Mov AX, FFFFH
Mov BX, FFFFh
IMUL BX
Decimal product=1
Hex Product = 00000001 h
DX = 0000h, AX=0001h CF/OF=0
DX is a sign extension of AX for this CF/OF=0
12. IMUL INSTRUCTION
īSuppose AX contains 0FFFh.and BX contains 0FFFh
Mov AX, 0FFFH
Mov BX, 0FFFh
IMUL BX
Decimal product=16769025
Hex Product = 00FFE001 h
DX = 00FFh, AX=E001h CF/OF=0
DX is a not sign extension of AX for this CF/OF=1
13. IMUL INSTRUCTION
īSuppose AL contains 80h.and BL contains FFh
Mov AL, 80H
Mov BL, FFh
IMUL BL
Decimal product=128,
Hex Product = 0080 h
AH = 00h, AL=80h CF/OF=01
DX is a not sign extension of AX for this CF/OF=1
14. APPLICATION OF MUL AND IMUL
īTranslate the high level language assignment
statement
īA=5ÃA-12ÃB
īLet A and B be word variables, and suppose
there is no overflow.
īUse IMUL for multiplication
17. DIVIDE AND IDIVIDE
īWhen division is performed we obtain two
results
īThe quotient and
īThe remainder.
īSimilarly like Multiplication there are separate
instructions for unsigned and signed division
18. CONT..
Syntax:
DIV divisor
IDIV divisor
Byte Form:
The divisor is eight bit register or memory byte
The 16 â bit dividend is assumed to be in AX. After
division 8-bit quotient is in AL and 8-bit remainder in
AH.
19. CONT..
Word Form:
The divisor is a 16-bit register or memory word
The 32-bit dividend is assumed to be in DX:AX
After division, the 16-bit quotient is in AX and 16-
bit remainder is in DX
Effect on flags:
All status flags are undefined
20. CONT..
Divide Overflow:
īIt is possible that the quotient s too big to fit
in the specified destination(AL or AX).
īThis Happens Because the divisor is much
smaller than the dividend.
īWhen this Happens the program terminates
and system displays message âdivide overflowâ
21. CONT..
Examples:
EXAMPLE 9.8:
Suppose DX contains 0000h , AX contains 0005h, and BX
contains 0002h.
EXAMPLE 9.9:
Suppose DX contains 0000h , AX contains 0005h and BX
contains FFFEh.
EXAmple 9.10:
Suppose AX contains 00FBh and BL contains FFh.
22. DECIMAL INPUT AND
OUTPUT
īComputer represent every thing in binary
īBut it is convenient for user to represent
input and output in decimal
īIf we input 21543 character string then it
must to converted internally
īConversely on output the binary contents of
R/M must be converted to decimal equivalent
before being printed
23. DECIMAL INPUT
īConvert a string of ASCII digits to the binary
representation of decimal equivalent
īFor input we repeatedly multiply AX by 10
Algorithm (First version):
Total=0
Read an ASCII
REPEAT
convert character to number
Total=total*10+value
Read a character
Until character is carriage return
24. CONT..
Example: input of 123
Total =0
Read â1â
Convert â1â to 1
Total=10*0 +1=1
Read â2â
Convert â2â to 2
Total=10*1 +2=12
Read â3â
Convert â3â to 3
Total=10*12 +3=123
25. CONT..
īRange: -32768 to 32767
īOptional sign followed by string of digits
& carriage return
īOutside â0â to â9â
ījumps to new line and ask for input
again
27. CONT..
Else
Convert character to binary value
Total=10*total+value
End_if
Read a character
Until character is carriage return
If negative =true
Then
total=-total
43. Decimal Output
Algorithm for Decimal Output:
īIf AX < 0 /*AX holds output value */
īTHEN
īPrint a minus sign
īReplace AX by its twos complement
īEnd_IF
īGet the digits in AXâs decimal representation
īConvert these digits into characters and print
them
44. CONT..
To see what line 6 entitles, suppose the contents of
AX, expressed in decimal is 24168. To get the digits
in decimal representation , we can proceed as
follows,
īDivide 24618 by 10, Quotient= 2461,
remainder=8
īDivide 2461 by 10, Quotient= 246, remainder=1
īDivide 246 by 10 , Quotient=24, remainder=6
īDivide 24 by 10, Quotient=2, remainder=4
īDivide 2 by 10, Quotient=0, remainder=2
45. CONT..
LINE 6:
Cout =0 /*will count decimal digit */
REPEAT
divide quotient by 10
Push remainder on the stack
Count= count +1
UNTILL
Quotient=0
46. CONT..
LINE 7:
FOR count times DO
Pop a digit from the stack
Convert it to a character
Output the character
END_FOR
47. CONT..
Program Listing PMG9_1.ASM
.MODEL SMALL
.STACK 100H
.CODE
OUTDEC PROC
;prints AX as a signed decimal integer
;input: AX
;output: none
PUSH AX ;save registers
PUSH BX
PUSH CX
PUSH DX
48. CONT..
;if AX < 0
OR AX,AX ;AX < 0?
JGE @END_IF1 ;NO >0
;then
PUSH AX ; save number
MOV DL,â-â ;get â-â
MOV AH,2 ;print character function
INT 21H ;print â-â
POP AX ;get Ax back
NEG AX ;AX= -AX
@END_IF1:
49. CONT..
;get decimal digits
XOR CX,CX ;CX counts digits
MOV BX,10D ;BX has divisor
@REPEAT1:
XOR DX,DX ;prepare high word of dividend
DIV BX ;AX=quotient, DX=remainder
PUSH DX ;save remainder on stack
INC CX ;count = count +1
;until
OR AX,AX ;quotient = 0?
JNE @REPEAT ;no, keep going
50. CONT..
;convert digits to character and print
MOV AH,2 ;print character function
;for count time do
@PRINT_LOOP
POP DX ;digit in DL
OR DL,30H ;convert to character
INT 21H ;print digit
;end_for
POP DX ; restore registers
POP CX
POP BX
POP AX
OUTDEC ENDP