Escolar Documentos
Profissional Documentos
Cultura Documentos
Architecture of 8086
o While the BIU fetches the opcode, Execution unit executes
the previously decoded instruction concurrently
REGISTER ORGANISATION
Registers of 8086
AH
BH
CH
DH
AL
BL
CL
DL
Segment Registers
Segment Registers
Parity Flag (PF) - this flag is set to 1 when there is even number of
one bits in result, and to 0 when there is odd number of one bits.
Zero Flag (ZF) - set to 1 when result is zero. For non-zero result
this flag is set to 0.
EX:
MOV AX,[BX]
Indexed Addressing Mode
Offset of the operand is stored in one of Index register
EX:
MOV AX,[BX][SI]
Relative Based Indexed
Effective address is formed by adding an 8 or 16 bit
displacement with the sum of contents of any one of base
registers (BX or BP) and any one of Index registers in a
default segment
EX:
MOV AX,50H [BX] [SI]
8086 Microprocessor
Instruction Set of 8086
.code
main proc
; Write the program main code here
main endp
;Other Procedures
end main
Title directive:
The title directive is optional and specifies the title of the
program. Like a comment, it has no effect on the program.
Segment directives:
.stack
.data
constants.
Used to set aside storage for variables.
Constants are defined within this segment in the program
source.
Code Segment:
. code
Meaning
Format
Operation
Flags
affected
Mov
Move
MovD,S
(s)(D)
None
Destination
Source
Memory
Accumulator
Accumulator
Memory
Register
Register
Register
Memory
Memory
Register
Register
Immediate
Memory
Immediate
Seg reg
Reg 16
Seg reg
Mem 16
Reg 16
Seg reg
Memory
Seg reg
NO MOV
Mem
Imm
SegReg
Mem
SegReg
Segreg
EX: MOVAL,BL
Meaning
Format
Exchange XCHGD,S
Destination
Source
Accumulator
Reg 16
Memory
Register
Register
Register
Register
Memory
Operation
Flagsaffected
(s)(D)
None
Meaning
Format
Operation
Flags
affected
LEA
Load
Effective
Address
LEAReg16,EA
EA(Reg16)
None
LDS
Load
Register
AndDS
LDSReg16,MEM32
(MEM32)
(Reg16)
None
(Mem32+2)(DS)
LES
Load
Register
andES
LESReg16,MEM32
(MEM32)
(Reg16)
(Mem32+2)(DS)
None
DATAXDW1000H
DATAYDW5000H
.CODE
LEASI,DATAX
MOVEDI,OFFSETDATAY;THISISMOREEFFICIENT
LEABX,[DI];ISTHESAMEAS
MOVBX,DI;THISJUSTTAKESLESSCYCLES.
LEABX,DI;INVALID!
LDS BX, [DI];
BX
127A
DI
1000
DS
7A
11000
12
11001
00
11002
30
11003
3000
LESsimilartoLDSexceptthatitloadsES
Meaning
Format
Operation
Flags
XLAT
Translate
XLAT
((AL)+(BX)+
(DS)0)
(AL)
None
Arithmetic Instructions
ADD, ADC, INC, AAA, DAA
PackedBCD
Arithmetic Instructions
ADD, ADC, INC, AAA, DAA
Mnemonic
Meaning
Format
Operation
Flags
affected
ADD
Addition
ADDD,S
(S)+(D)(D)
carry(CF)
ALL
ADC
Addwith
carry
ADCD,S
(S)+(D)+(CF)(D)
carry(CF)
ALL
INC
Increment
byone
INCD
(D)+1(D)
ALLbutCY
AAA
ASCII
adjustfor
addition
AAA
Ifthesumis>9,AH
isincrementedby1
AF,CF
DAA
Decimal
adjustfor
addition
DAA
AdjustALfordecimal
PackedBCD
ALL
AL=71h
AH=01,AL=07
AX=3137
Examples:
Ex.1ADDAX,2
ADCAX,2
Ex.2INCBX
INCWORDPTR[BX]
Ex.3ASCIICODE0-9=30-39h
MOVAX,38H;(ASCIIcodefornumber8)
ADDAL,39H;(ASCIIcodefornumber9)AL=71h
AAA;usedforadditionAH=01,AL=07
ADDAX,3030H;answertoASCII0107AX=3137
Ex.4ALcontains25(packedBCD)
BLcontains56(packedBCD)
ADDAL,BL
DAA
25
+56
-------7B81
Example:
.MODEL SMALL
.STACK 64
.DATA
DATA1 DQ 548F9963CE7h
ORG 0010h
DATA2 DQ 3FCD4FA23B8Dh
ORG 0020h
DATA3 DQ ?
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
CLC
MOV SI,OFFSET DATA1
MOV DI,OFFSET DATA2
MOV BX,OFFSET DATA3
MOV CX,04h
BACK: MOV AX,[SI]
ADC AX,[DI]
MOV [BX],AX
ADD SI,2h
ADD DI,2h
ADD BX,2h
LOOP BACK
MOV AH,4Ch
INT 21h; halt
MAIN ENDP
END MAIN
;allocate 8 bytes
; allocate 8 bytes
CF=1
DAS
AAS
AAS
(AL)difference
(AH)decby1
ifborrow
Meaning
Format
Operation
Flags
affected
SUB
Subtract
SUBD,S
(D)-(S)(D)
Borrow(CF)
All
SBB
Subtract
with
borrow
SBBD,S
(D)-(S)-(CF)(D)
All
(D)-1(D)
AllbutCF
DEC
Decrement DECD
byone
NEG
Negate
NEGD
All
DAS
Decimal
adjustfor
subtraction
DAS
ConverttheresultinALto
packeddecimalformat
All
AAS
ASCII
adjustfor
subtraction
AAS
(AL)difference
(AH)decby1ifborrow
CY,AC
Examples:DAS
Borrow1fromAH
Examples:DAS
MOV BL, 28H
MOV AL, 83H
SUB AL,BL; AL=5BH
DAS
; adjust as AL=55H
(BX)=0000000000111010
2s comp = 1111111111000110 = FFC6H
(CF)=1
Example
32-bit subtraction of two 32 bit numbers X and Y that are stored
in the memory as
X = (DS:203h)(DS:202h)(DS:201h)(DS:200h)
Y = (DS:103h)(DS:102h)(DS:101h)(DS:100h)
The result X - Y to be stored where X is saved in the memory
MOV SI, 200h
MOV DI, 100h
MOV AX, [SI]
SUB AX, [DI]
MOV [SI], AX ;save the LS word of result
MOV AX, [SI] +2
SBB AX, [DI]+2
MOV [SI] +2, AX
Ex. 12 34 56 78 23 45 67 89 = EF EE EE EE
MultiplicationandDivision
Multiplicant
Operand
(Multiplier)
Result
Byte*Byte
AL
Register or
memory
AX
Word*Word
AX
Register or
memory
DX :AX
Dword*Dword
EAX
Register or
memory
EAX :EDX
Division
(DIV or IDIV)
Dividend
Operand
(Divisor)
Quotient:
Remainder
Word/Byte
AX
Register or
Memory
AL : AH
Dword/Word
DX:AX
Register or
Memory
AX : DX
Qword/Dword
EDX: EAX
Register or
Memory
EAX : EDX
AX
BL
0085 H
35 H
0085 H
AX
4. IDIV BL BL = 35H =
= 02 (85-02*35=1B)
AH
AL
1B 02
AH
AL
1B
02
Ex2:
00 F 3H
00 F 3H
AX
3.IDIV BL
=
=
= 2 (00F3 2*6F=15H)
BL
AH
AL
15
02
2' S (91H )
6 FH
AH
POS
NEG 2s(02)=FEH 15
NEG
00 F 3H
AX
4. DIV BL
=
= 01(F3-1*91=62)
91
H
BL
AL
FE
AH
AL
62
01
RQ
Ex3:
DX
8713
AX
B000
DX
AX
06FE
B000
F 000 H
3. DIV BL =
= B6DH More than FFH Divide Error.
15 H
Ex4:
AX 1250 H
POS
POS
1250 H
1250 H
1. IDIV BL
=
=
=
=
=
BL
NEG 2' sNEG 2' s (90 H )
70 H
90 H
=29H(Q)(125029*70)=60H(REM)
29H(POS)2S(29H)=D7H
RQ
60H
D7H
RQ
1250 H
AX
20H
2.DIVBL==20H1250-20*90=50H 50H
90 H
BL
AHAL
Logical Instructions
Logical Instructions
Mnemonic
Meaning
Format
Operation
Flags Affected
AND
Logical AND
AND D,S
OR
Logical Inclusive OR
OR D,S
(S)+(D) (D)
XOR
Logical Exclusive OR
XOR D,S
NOT
LOGICAL NOT
NOT D
(S) + (D)(D)
_
(D) (D)
Destination
Source
Register
Register
Memory
Register
Memory
Accumulator
Register
Memory
Register
Immediate
Immediate
Immediate
Destination
Register
Memory
Logical Instructions
AND
Uses any addressing mode except memory-to-memory and
segment registers
Especially used in clearing certain bits (masking)
xxxx xxxx AND 0000 1111 = 0000 xxxx
(clear the first four bits)
Examples:
AND BL, 0FH
AND AL, [345H]
OR
Used in setting certain bits
xxxx xxxx OR 0000 1111 = xxxx 1111
(Set the upper four bits)
XOR
Used in inverting bits
xxxx xxxx XOR 0000 1111 = xxxxxxxx
-Example: Clear bits 0 and 1, set bits 6 and 7, invert bit 5 of
register CL:
AND CL, OFCH ;
1111 1100B
OR CL, 0C0H
;
1100 0000B
XOR CL, 020H ;
0010 0000B
Shift Instructions
Shift Instructions
Mnemonic
Meaning
Format
Operation
Flags Affected
SAL/SHL
Shift arithmetic
Left/shift
Logical left
SAL/SHL D,Count
CF,PF,SF,ZF
AF undefined
OF undefined if count 1
SHR
Shift logical
right
SHR D,Count
CF,PF,SF,ZF
AF undefined
OF undefined if count 1
SAR
Shift arithmetic
right
SAR D,Count
Shift Instructions
Destination
Count
Register
Register
CL
Memory
Memory
CL
Ex.
; Multiply AX by 10
SHL AX, 1
MOV BX, AX
MOV CL,2
SHL AX,CL
ADD AX, BX
SAR CL, 1
if CL initially contains B6H? DBH
SHL AL, CL
if AL contains 75H and CL contains 3? A8H
Rotate Instructions
Rotate Instructions
Mnemonic
Meaning
Format
Operation
Flags Affected
ROL
Rotate left
ROL D,Count
CF
OF undefined if count 1
ROR
Rotate right
ROR D,Count
CF
OF undefined if count 1
RCL
Rotate left
through
carry
RCL D,Count
CF
OF undefined if count 1
RCR
Rotate right
through
carry
RCR D,Count
CF
OF undefined if count 1
Rotate Instructions
Destination
Count
Register
Register
CL
Memory
Memory
CL
(82H)
Example
Write a program that counts the number of 1s in a
byte and writes it into BL
DATA1 DB 97 ; 61h
SUB BL, BL ; clear BL to keep the number of 1s
MOV DL, 8 ; Counter
MOV AL, DATA1
AGAIN: ROL AL,1 ; rotate left once
JNC NEXT ; check for 1
INC BL ; if CF=1 then add one to count
NEXT: DEC DL ; go through this 8 times
JNZ AGAIN ; if not finished go back
NOP
Assembler Directives
Assembler Directives
Assembler Directives
Assembler Directives
Assembler Directives
Assembler Directives
LOCAL
The Labels, Variables, constants or procedures declared
LOCAL are to be used only by that particular module
Same label can serve different purposes for different
modules of a program
NAME: Logical Name of a Module
Used to assign the name to an ALP module
Module can now be referred as the declared name
OFFSET: Offset of a Label
Used with arrays, strings, labels and procedures to decide
their offsets in their default segments
Assembler Directives
ORG: Origin
Directs the assembler to start the memory allotment for the particular
segment, block or code from the declared address in the ORG
statement
ORG 200H
PROC: Procedure
Assembler Directives
EXE.CODE
SEGMENT CODE
EXE.CODE
ENDS
SHORT
JMP
SHORT
LABEL
TYPE
Assembler Directives
GLOBAL
Labels, Variables, Constants or procedures declared
GLOBAL can be used by other modules of the program
ROUTINE
PROC GLOBAL
FAR PTR
Indicates that the label following this directive is not
available within the same segment and the address of the
label is of 32 bits
JMP
FAR PTR
Label
NEAR PTR
Indicates that the label following this directive is in the
same segment and needs only 16 bit to address it
JMP
NEAR PTR Label
Procedure
Subroutines in a ALP
May require input data or constants for their execution
If interactive it may directly accept inputs from input devices
Techniques to pass data to procedures in ALP
Using Global declared variable
Using registers of CPU architecture
Using memory locations (Reserved)
Using Stack
Using PUBLIC & EXTRN
Procedure
MACROS
Process of assigning a label or macro name to a string
MACROS