Multiplication & division instructions microprocessor 8086
Hamayun14373Liaqat
24,623 views
51 slides
Feb 03, 2015
Slide 1 of 51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
About This Presentation
this Presentation is prepared to demonstrate Multiplication and Division Instruction In microprocessor 8086
Size: 1.44 MB
Language: en
Added: Feb 03, 2015
Slides: 51 pages
Slide Content
Topic: Multiplication 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
Multiplication MUL(unsinged) IMUL(singed)
MUL Instruction (Unsigned Multiply)
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
MUL Instruction
MUL Examples Mov al, 5h Mov bl , 10h Mul bl ; AX = 0050h, CF = 0 No overflow - the Carry flag is 0 because the upper half of AX is zero
IMUL Instruction (Signed Multiply) same syntax uses the same operands as the MUL instruction preserves the sign of the product Opcode = IMUL
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
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
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
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
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
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
Solution: Mov AX,5 ;AX=5 IMUL A ;AX=5*A MOV A,AX ;A=5*A MOV AX,12 ;AX=12 IMUL B ;AX=12*B SUB A,AX ;5*A-12*B
DIVIDE DIV(unsinged) IDIV(singed)
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
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.
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
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”
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 .
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
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
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
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
Cont.. Algorithm(second version): total=0 Negative=false Read a character Case character of ‘ - ’: negative=true read a character ‘ + ’: read a character End_case Repeat If character is not between ‘0’ to ‘9’ Then Go to beginning
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
Cont.. Program(source code): INDEC PROC ;READ NUMBER IN RANGE -32768 TO 32767 PUSH BX PUSH CX PUSH DX @BEGIN: ;total =0 XOR BX,BX ;BX hold total ;negative = false
Cont.. XOR CX,CX ; CX hold sign ;read char MOV AH,1 INT 21H ;case char of CMP AL,'-' ; minus sign JE @MINUS ; yes,set sign CMP AL,'+' ; plus sign JE @PLUS ; yes,get another char
Cont.. JMP @REPEAT2 ; start processing char @MINUS: MOV CX,1 @PLUS: INT 21H ;end case @REPEAT2: ;if char. is between '0' and '9' CMP AL,'0' ;char >='0'? JNGE @NOT_DIGIT ;illegal char.
Cont.. CMP AL, '9' ; char<='9' ? JNLE @NOT_DIGIT ;then convert char to digit AND AX,000FH PUSH AX ;save number ;total =total *10 +digit MOV AX,10 MUL BX POP BX ;retrieve number ADD BX,AX ; total =total *10 +digit
Cont.. ;read char MOV AH,1 INT 21H CMP AL,0DH ;CR JNE @ REPEAT2 ;no keep going ;until CR MOV AX,BX ;store number in AX ;if negative OR CX,CX ;negative number
Cont.. Jz @EXIT ; no,exit ;then NEG AX ; yes,negate ;end if @EXIT : ;retrieve registers POP DX POP CX POP BX RET
Cont.. ;here if illegal char entered @ NOT_DIGIT: MOV AH,2 MOV DL,0DH INT 21H MOV DL,0AH INT 21H JMP @BEGIN INDEC ENDP
Cont.. Output:
Input Overflow AX:FFFFh In decimal:65535 Range:-32768 to 32767 Anything out of range called input overflow For example: Input:32769 Total=327690
Cont.. Algorithm: total=0 Negative=false Read a character Case character of ‘ - ’: negative=true read a character ‘ + ’: read a character
Cont.. End_case Repeat If character is not between ‘0’ to ‘9’ Then Go to beginning Else Convert character to binary value Total=10*total
Cont.. If overflow Then go to beginning Else Total =total*10 +value If overflow Then g o to beginning
Cont.. End_if End_if End_if Read a character Until character is carriage return If negative =true Then total= - total
Cont.. Code: ; total =total *10 +digit MOV AX,10 MUL BX CMP DX,0 JNE @ NOT_DIGIT POP BX ADD BX,AX JC @ NOT_DIGIT
Cont.. Output:
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
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
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
Cont.. LINE 7: FOR count times DO Pop a digit from the stack Convert it to a character Output the character END_FOR
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
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:
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
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