Eeng410 Lecture9
Eeng410 Lecture9
Eeng410 Lecture9
8086
• Logic Instructions
EENG410: MICROPROCESSORS I
8086
Unsigned Subtraction
• Subtraction of Unsigned Numbers
▪ SUB (Subtract) Instruction
Execution steps:
AL 3F 0011 1111 0011 1111
– BH – 23 – 0010 0011 + 1101 1101 (2’s complement)
AL 1C 0001 1100 1 0001 1100 (CF=0) Step 3
CF=0, ZF=0, AF=1, PF=0, SF=0.
EENG410: MICROPROCESSORS I
8086
Unsigned Subtraction
• Subtraction of Unsigned Numbers
▪ SUB (Subtract) Instruction
• If the CF=0, the result is positive and the destination has the result.
• If the CF=1, the result is negative and the destination has the 2’s complement of
the result. NOT and INC increment instructions can be used to change it.
Ex: ;from the data segment:
DATA1 DB 4CH
DATA2 DB 6EH
RESULT DB ?
;from the code segment:
MOV DH, DATA1 ;load DH with DATA1 value (4CH)
SUB DH,DATA2 ;subtract DATA2 (6E) from DH (4C)
JNC NEXT ;if CF=0 jump to NEXT target
NOT DH ;if CF=1 take the 1’s complement
INC DH ;and increment to get 2’s complement
NEXT: MOV RESULT,DH ;save DH in RESULT
Analysis: Following the 3 steps for “SUB DH,DATA2”
4C 0100 1100 0100 1100
– 6E 0110 1110 2’s comp + 1001 0010
– 22 0 1101 1110 CF=1(Step 3) the result is negative
EENG410: MICROPROCESSORS I
8086
Unsigned Subtraction
• Subtraction of Unsigned Numbers
▪ SBB (Subtract with borrow) Instruction
Note: PTR (Pointer) Directive is used to specify the size of the operand. Among the options for
size are BYTE, WORD, DWORD and QWORD.
Solution: After the SUB, AX = 62FA – 963B = CCBF and the carry flag is set. Since CF=1, when SBB
is executed, AX = 625 – 412 – 1 = 212. Therefore, the value stored in RESULT is 0212CCBF.
EENG410: MICROPROCESSORS I
8086
Unsigned multiplication and Division
• Multiplication of unsigned numbers
▪ byte x byte :
▪ One of the operands must be in AL . The other operand can be either in a register
or in memory.
▪ After the multiplication the result is in AX.
Ex: .DATA
RESULT DW ? ; result is defined in data segment
.CODE
…..
MOV AL,25H ; a byte is moved to AL
MOV BL,65H ; immediate data must be in a register
MUL BL ; AL= AL x BL = 25 x 65
MOV RESULT,AX ; result is saved
EENG410: MICROPROCESSORS I
8086
Unsigned multiplication and Division
• Multiplication of unsigned numbers
▪ byte x byte :
Ex: .DATA
DATA1 DB 25H
DATA2 DB 65H
RESULT DW ?
;from the code segment:
MOV AL,DATA1
MOV BL,DATA2
MUL BL ;register addressing mode
MOV RESULT,AX
or
MOV AL,DATA1
MUL DATA2 ;direct addressing mode
MOV RESULT,AX
EENG410: MICROPROCESSORS I
8086
Unsigned multiplication and Division
• Multiplication of unsigned numbers
▪ word x word :
• One of the operands must be in AX . The other operand can be either in a register
or in memory.
• After the multiplication the lower word is in AX and the higher word is in DX.
Ex: .DATA
DATA3 DW 2278H
DATA4 DW 2F79H
RESULT1 DW 2 DUP?
...
MOV AX,DATA3 ; load first operand into AX
MUL DATA4 ; multiply it by the second operand
MOV RESULT1,AX ; store the lower word of the result
MOV RESULT1+2,DX ; store the higher word of the result
EENG410: MICROPROCESSORS I
8086
Unsigned multiplication and Division
• Multiplication of unsigned numbers
▪ word x byte :
•Similar to word x word, but AL contains byte operand and AH must be zero.
Ex: .DATA
DATA5 DB 6BH
DATA6 DW 12C3H
RESULT3 DW 2 DUP?
...
MOV AL,DATA5 ; AL holds byte operand
SUB AH,AH ; AH must be cleared
MUL DATA6 ; byte in AL multiplied by word operand
MOV BX,OFFSET RESULT3 ; BX points the storage for product
MOV [BX],AX ; AX holds lower word
MOV [BX]+2,DX ; DX holds higher word
Ex: .DATA
A DW 10050
B DW 100
QUOT2 DW ?
REMAIN2 DW ?
.CODE
:
MOV AX,A ;AX holds numerator
SUB DX,DX ;DX must be cleared
MOV BX,B ;BX is used for denominator
DIV BX ;divide AX by BX
MOV QUOT2,AX ;quotient = AX = 64H (100 )
MOV REMAIN2,DX ;remainder = DX = 32H (50)
:
EENG410: MICROPROCESSORS I
8086
Unsigned multiplication and Division
• Division of unsigned numbers
▪ word / byte
• Numerator must be in AX
• Denominator can be in memory or in a register.
• After the division AL will have the quotient and AH will have the remainder
Ex:
Ex: .DATA
QUO DB ?
REM DB ?
.CODE
:
MOV AX,2055 ;AX holds numerator
MOV CL,100 ;BX is used for denominator
DIV CL ;divide AX by CL
MOV QUO,AL ;AL holds the quotient = AL = 14H (20)
MOV REM,AH ;AH holds the remainder = AH = 37H (55)
:
EENG410: MICROPROCESSORS I
8086
Unsigned multiplication and Division
• Division of unsigned numbers
▪ doubleword/word
• Numerator must be in AX and DX, least significant word in AX and most significant
word in DX.
• Denominator can be in memory or in a register.
• After the division AX will have the quotient and DX will have the remainder
Ex: .DATA
DATA1 DD 105432
DATA2 DW 10000
QUOT DW ?
REMAIN DW ?
.CODE
:
MOV AX,WORD PTR DATA1 ;AX holds the lower word
MOV DX,WORD PTR DATA1+2 ;DX holds the higher word of the numerator
DIV DATA2
MOV QUOT,AX ;AX holds the quotient
MOV REMAIN,DX ;DX holds the remainder
:
“Divide Error”: If the denominator is zero (dividing any number by 00) and if the quotient is too large for the
assigned register, “Divide Error” message will be displayed.
EENG410: MICROPROCESSORS I
8086
Logic Instructions
• Logic Instructions
▪ AND Instruction
Solution:
35H 00110101
0FH &00001111
05H 00000101 SF=0, ZF=0, PF=1,CF=OF=0
This operation will AND DH with itself and if the result is zero set ZF=1 jumping to NEXT.
EENG410: MICROPROCESSORS I
8086
Logic Instructions
• Logic Instructions
▪ OR Instruction
Solution: