mpmc u2 ece_arun

36
.M.M. Arun Prasath., AP/ECE 1 UNIT II 16 BIT MICROPROCESSOR INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING

Upload: arun-prasath

Post on 06-May-2015

1.779 views

Category:

Education


2 download

DESCRIPTION

Microprocessors and Microcontrollers

TRANSCRIPT

Page 1: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 1

UNIT – II

16 BIT

MICROPROCESSOR

INSTRUCTION SET AND

ASSEMBLY LANGUAGE

PROGRAMMING

Page 2: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 2

Features of 8086 Microprocessor

It is a16-bit microprocessor.

It has a 20 bit address bus can access up to 220 Memory

locations (1 MB).

It can support up to 64K I/O ports

It provides fourteen 16 -bit registers.

It has multiplexed address and data bus AD0- AD15 & A16 –

A19.

It is designed to operate in 2 modes, Minimum and Maximum

mode

It can prefetches up to 6 instruction bytes from memory and

queues them in order to speed up instruction execution

It requires +5v power supply

It is a 40 pin dual in line package

Page 3: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 3

Programmers model of 8086

Page 4: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 4

Operand Types of 8086

The 8086 supports the following types of operand

1. Bytes (8-bit)

2. Words (16-bit)

3. Short integers (8-bit)

4. Integers (16-bit)

5. Double words (32-bit)

6. Long integers (32-bit)

7. Strings (Series of alphanumeric characters

defined by ASCII codes)

Page 5: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 5

Operand Addressing

Addressing mode indicates a way of locating data or operands, We can

classify the addressing modes of 8086 into 6 groups:

Register addressing mode

Immediate addressing mode

Memory addressing mode

Port addressing mode

Relative addressing mode

Implied addressing mode

Page 6: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 6

1. Register Addressing Mode:

8086 CPU contains 8 General-Purpose Registers or Working Registers

AX,BX,CX,DX,BP,DI,SI,SP and 4 Segment Registers CS,DS,ES,SS

Source Registers: AX,BX,CX,DX,DI,SI,BP,SP,CS,DS,ES,SS

Destination Registers: AX,BX,CX,DX,DI,SI,BP,SP, DS,ES,SS

Ex.1: MOV AL,CL Ex.2: MOV DS,SI

2. Immediate Addressing Mode: 8 or 16 bit data can be specified as part

of the instruction

Ex: MVI AL,34H MVI SI,1212H

ADDRESSING MODES of 8086

Page 7: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 7

3. Memory addressing mode

One of the operands is in memory

8086 must use

a segment register

a 16-bit offset (effective address)

By default, DS register is used for accessing data from memory

16-bit offset can be specified in many ways

1. Direct Addressing mode

2. Register Indirect or Indirect Addressing mode

3. Base plus index Addressing mode

4. Register Relative Addressing mode

5. Base Relative plus Index Addressing mode

6. String Addressing mode cont…

ADDRESSING MODES of 8086

Page 8: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 8

Ex:

MOV AL, DS

MOV DS, DL

MOV AX, DS

cont…

3. Memory addressing mode

1. Direct addressing mode

ADDRESSING MODES of 8086

Page 9: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 9

3. Memory addressing mode

2. Register Indirect or Indirect Addressing mode

• In this mode, the Effective Address is specified in either a pointer register (BX or BP)

or an index register (SI or DI).

Ex: MOV BX,[CX] ;DS=10000H CX=2000H

;Physical Address: 12000H=30 12001H=40

;BX=4030H BH=40H BL=30H

3. Base plus index Addressing mode

Ex: MOV CX,[BX+DI] ;DS=10000H BX=2000H DI=0030H

;Physical Address: 12030H=20 12031H=10

;CX=1020H CH=10H CL=20H

cont…

ADDRESSING MODES of 8086

Page 10: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 10

3. Memory addressing mode

4. Register Relative Addressing mode

Ex: MOV CX,[BX+0003H] ;DS=10000H BX=1000H

;Displacement=3 Base+Disp=1003H

;11003H=30H 11004H=40H

;CX=4030H CH=40H CL=30H

CX

CH CL

cont…

ADDRESSING MODES of 8086

Page 11: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 11

Ex: MOV AL,[BX+SI+10H]

3. Memory addressing mode

5. Base Relative plus Index Addressing mode

cont…

ADDRESSING MODES of 8086

Page 12: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 12

ADDRESSING MODES of 8086

3. Memory addressing mode

6. String Addressing mode

• Used when string related instructions are executed. It uses index

registers.

• The string instructions automatically assumes SI to point the first byte

or word of the source operand and DI to point the first byte or word of

the destination operand.

Ex: MOVS BYTE

initially:

[DF]=0 [DS]=30000H [SI]=0600H

[ES]=50000H [DI]=0400H

30600H=38H 50400H=45H

after execution:

50400H=38H [SI]=0601H [DI]=0401H

Page 13: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 13

4. Port addressing Two I/O port addressing modes can be used

Direct (e.g. IN AL, 02H)

Indirect (e.g. IN AL, DXH)

5. Relative addressing

Ex: JNC 08H ;IF carry = 0, PC is loaded with current PC contents

+ 8-bit signed value

6. Implied addressing mode No operands are specified

Ex: CLC ;Means clear carry flag

ADDRESSING MODES of 8086

Page 14: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 14

Assembler is a program which converts assembly language to machine language.

Syntax errors, logical errors and other program errors are not found out by the

assembler.

For completing all these tasks the assembler needs some hints from the program,

these hints are given to the assembler using some predefined alphabetical strings called Assembler Directives

1. ASSUME: The directive is used to inform the assembler the names of the logical

segment which are to be assigned to the different segments used in an assembly

language program

2. DB (Define Byte): It defines a byte type variable which occupies 1byte of memory

space.

Assembler Directives of 8086

Page 15: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 15

3. DW (Define Word): It defines a word type variable which occupies 2bytes of

memory space.

4. DD (Define Double Word): It defines a double word type variable which occupies

4bytes of memory space.

5. DQ (Define Quad Word): It defines a quad word type variable which occupies

8bytes of memory space.

6. DT (Define Ten Bytes): It defines a variable which occupies 10bytes of memory

space.

7. END (End of Program): It informs assembler the end of program module.

8. ENDP (End Procedure): It informs assembler the end of procedure (subroutines)

9. ENDM (End Macro): It informs assembler the end of the macro. It used with the

directive MACRO to enclose macro instructions.

10. ENDS (End Segment): It informs assembler that it is the end of a segment.

Assembler Directives of 8086

Page 16: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 16

11. EQU (Equate): It is used to give a name to certain value or symbol which is used

many times in an assembly language program to make programming easy.

12. EVEN (Align or Even memory address): It informs assembler to increment the

content of the location counter to the next memory address, if it is already no at an

even address.

13. EXTRN (External): It informs that the names, procedures and labels following this

directive have already been defined in some other program module.

14. GLOBAL: It can be used in place of public or external directive.

15. GROUP: It informs assembler to form a logical group of the segments named after

this directive.

16. INCLUDE (Include source code from file): It informs assembler to insert a block of

codes from a named file in to the current program module. It is convenient to keep

all data and macros in a file called header file or include file.

Assembler Directives of 8086

Page 17: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 17

17. LABEL: In assembly language program, labels are used to give names to memory

addresses.

18. LENGTH: It is an operator to determine the no. of elements in a data item such as

an array or a string.

19. LOCAL: When certain variables, constants, procedures or labels are to be used in

only one program module, they are declared local using LOCAL directive

20. MACRO: A sequence of instructions to which a name is assigned. Same as

subroutines.

21. NAME: A large program module contain several program modules so specific

name can be given to each program module using NAME directive.

22. OFFSET: It is an operator to determine the offset of a variable or procedure with

respect to the base of the segment which contains the named variable or

procedure.

Assembler Directives of 8086

Page 18: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 18

23. ORG (Originate): The ORG directive directs the assembler to set the location

counter at memory address specified after the directive.

24. PROC (Procedure): It indicate the start of a procedure. The type of the procedure

FAR or NEAR is to be specified after the directive.

NEAR is used to call a procedure which is within the program module and FAR is

used to call from some other program module.

25. PTR (Pointer): It indicates the type (BYTE, WORD or DW) of a variable or label.

26. PUBLIC: a large program usually consist of several program modules. The

variables, constants, procedures and labels defined in a program module may be

used in some other program module. So such items are to be declared public using

PUBLIC directive.

27. RECORD: It defines the bit pattern within a byte or word

28. SEG(Segment): It is an operator used before a variable, procedures and label

Assembler Directives of 8086

Page 19: Mpmc u2 ece_arun

.M.M. Arun Prasath., AP/ECE 19

29. SEGMENT (Logical Segment): It indicates the beginning of a logical segment. The

SEGMENT and ENDS directives are used to enclose a logical segment containing

code or data.

30. SHORT: It is an operator. It informs assembler that only one byte for the

displacement is required to code a jump instruction.

31. SIZE: it is an operator to determine the number of bytes allocated to a data item.

It differs from the LENGTH operator as it determines the no. of elements in a data

item.

32. STRUCK or STRUC (Structure Declaration): It defines the beginning of a data

structure which is a collection of primary data types such as DB, DW, DD is

declared.

33. TYPE: It is an operator that determines the type of a variable. It actually

determines the no. of bytes allocated to the variable and substitutes the no. in

place of the variable before which TYPE appears.

Assembler Directives of 8086

Page 20: Mpmc u2 ece_arun

8086 INSTRUCTION SET It is classified into 7 Categories.

They are:

1. DATA TRANSFER INSTRUCTIONS

2. ARITHMETIC INSTRUCTIONS

3. LOGICAL INSTRUCTIONS

4. CONTROL INSTRUCTIONS

5. PROCESSOR CONTROL INSTRUCTIONS

6. STRING MANIPULATION INSTRUCTIONS

7. INTERRUPT CONTROL INSTRUCTIONS

.M.M. Arun Prasath., AP/ECE 20

Page 21: Mpmc u2 ece_arun

1.DATA TRANSFER INSTRUCTIONS

GENERAL PURPOSE BYTE OR WORD TRANSFER

INSTRUCTIONS

MOV Instruction - MOV destination, source

Example: MOV DL,[BX] : Copy byte from memory at BX to DL

PUSH Instruction - PUSH source

Example: PUSH BX

POP Instruction - POP destination

Example: POP DX

XCHG Instruction - Exchange XCHG destination, source

Example: XCHG AX, DX

XLAT/XLATB Instruction - Translate a byte in AL with byte pointed in BX

Example: MOV AL, [BX+AL] .M.M. Arun Prasath., AP/ECE 21

Page 22: Mpmc u2 ece_arun

SIMPLE INPUT AND OUTPUT PORT TRANSFER INSTRUCTION

IN Instruction - Copy data from a port IN accumulator, port

Example: IN AL,0C8H

OUT Instruction - Output a byte or word to a port – OUT port, accumulator AL or

AX. Example: OUT 3BH, AL

SPECIAL ADDRESS TRANSFER INSTRUCTIONS

LEA Instruction - Load Effective Address

Syntax – LEA register, source

Example: LEA BX, PRICE ;Load BX with offset of PRICE in DS

LDS Instruction - Load register and DS with words from memory

Syntax – LDS register, memory address of first word

Example: LDS BX, [4326] ; copy the contents of the memory at 4326H

in DS to BL, contents of the 4327H to BH.

LES Instruction - Load register and ES with words from memory

Syntax – LES register, memory address of first word .M.M. Arun Prasath., AP/ECE 22

1.DATA TRANSFER INSTRUCTIONS

Page 23: Mpmc u2 ece_arun

FLAG TRANSFER INSTRUCTION

LAHF Instruction - Load Register AH From Flags

copies the value of SF, ZF, AF, PF, and CF, into bits of 7, 6, 4, 2, 0 respectively of

AH register.

SAHF instruction - Store AH Register into FLAGS

SAHF instruction transfers the bits 0-7 of AH of SF, ZF, AF, PF, and CF, into the

Flag register.

PUSHF Instruction - Push flag register on the stack

This instruction decrements the SP by 2 and copies the word in flag register

to the memory location pointed by SP.

POPF Instruction - Pop word from top of stack to flag – register. .M.M. Arun Prasath., AP/ECE 23

1.DATA TRANSFER INSTRUCTIONS

Page 24: Mpmc u2 ece_arun

2.ARITHMETIC INSTRUCTIONS

ADDITION INSTRUCTIONS

ADD Instruction - ADD destination, source

Example: ADD DX, BX

ADC Instruction - Add with carry

Example: ADC CL,BL

INC Instruction - Increment - INC destination

Example: INC BL

AAA Instruction - ASCII Adjust after Addition

We can add two ASCII numbers directly and use AAA after addition

so as to get result directly in BCD. (Works with AL only)

DAA Instruction - Decimal Adjust Accumulator

.M.M. Arun Prasath., AP/ECE 24

Page 25: Mpmc u2 ece_arun

SUBTRACTION INSTRUCTION

SUB Instruction - Subtract two numbers

Example: SUB CX, BX

SBB Instruction - Subtract with borrow SBB destination, source

Example: SBB CX, BX

DEC Instruction - Decrement destination register or memory.

NEG Instruction - Form 2’s complement – Syntax: NEG destination

CMP Instruction - Compare byte or word and sets the flag

Syntax: CMP destination, source.

AAS Instruction - ASCII Adjust for Subtraction

DAS Instruction - Decimal Adjust after Subtraction

.M.M. Arun Prasath., AP/ECE 25

2.ARITHMETIC INSTRUCTIONS

Page 26: Mpmc u2 ece_arun

MULTIPLICATION INSTRUCTION

MUL Instruction - Multiply unsigned bytes or words

Syntax: MUL source Example: MUL BL

IMUL Instruction - Multiply signed number

Syntax: IMUL source Example: IMUL BH

AAM Instruction - ASCII adjust after Multiplication

DIVISION INSTRUCTION

DIV Instruction - Unsigned divide Syntax: DIV source

Example: DIV BH ; AX / BH

; AX =Quotient & AH =Remainder.

IDIV Instruction - Divide by signed byte or word Syntax: IDIV source

Example: IDIV BL ; Signed word in AX is divided by signed byte in BL

AAD Instruction - ASCII adjust before Division

CBW Instruction - Convert signed Byte to signed word

CWD Instruction - Convert Signed Word to Signed Double word

( CBW and CWD works only with AL,AX and DX registers.) .M.M. Arun Prasath., AP/ECE 26

2.ARITHMETIC INSTRUCTIONS

Page 27: Mpmc u2 ece_arun

3.LOGICAL INSTRUCTIONS

LOGICAL INSTRUCTION

NOT Instruction - Invert each bit of operand

Ex: NOT BX ; Complement contents of BX register.

AND Instruction - AND corresponding bits of two operands

Ex: AND BH, CL ; AND byte in CL with byte in BH ;result in BH

OR Instruction - Logically OR corresponding of two operands

Ex: OR AH, CL ; CL is OR’ed with AH, result in AH. XOR Instruction - Exclusive XOR destination, source

Ex: XOR BX, CX ; Exclusive OR CX with BX and Result BX

TEST Instruction – AND operand to update flags

Ex: TEST AL, BH ; AND BH with AL. no result is stored .

Update PF, SF, ZF .M.M. Arun Prasath., AP/ECE 27

Page 28: Mpmc u2 ece_arun

SHIFT INSTRUCTION

SAL/SHL Instruction - Shift operand bits left, put zero in LSB(s)

Ex: ; CF = 0, BX = 11100101 11010011

SAL BX, 1 ; Shift BX contents by 1 bit position towards left

; CF = 1, BX = 11001011 10100110

SHR Instruction - Shift operand bits right, put zero in MSB

Ex: ; SI = 10010011 10101101 , CF = 0

SHR SI, 1 ; Result: SI = 01001001 11010110 and CF = 1

SAR Instruction - Shift operand bits right, new MSB = old MSB

Ex: ; AL = 00011101 = +29 decimal, CF = 0

SAR AL, 1 ; Shift signed byte in AL towards right

; AL = 00001110 = +14 decimal, CF = 1

.M.M. Arun Prasath., AP/ECE 28

3.LOGICAL INSTRUCTIONS

Page 29: Mpmc u2 ece_arun

ROTATE INSTRUCTION

ROL Instruction - Rotate all bits of operand left, MSB to LSB

Ex: ;CF =0, BX = 10111011 01110101

ROL BX, 1 ;Result: CF =1, BX = 01110110 11101011

ROR Instruction - Rotate all bits of operand right, LSB to MSB

Ex: ;CF =0, BX = 00111011 01110101

ROR BX, 1 ;Result: CF =1, BX = 10011101 10111010

RCL Instruction - Rotate operand around to the left through CF

Ex: ;CF=0, BH = 10110011

RCL BH, 1 ;Result : BH =01100110, CF = 1

RCR Instruction - Rotate operand around to the right through CF

Ex: ;CF = 1, BL = 00111000

RCR BL, 1 ;Result: BL = 10011100, CF =0 .M.M. Arun Prasath., AP/ECE 29

3.LOGICAL INSTRUCTIONS

Page 30: Mpmc u2 ece_arun

4.CONTROL INSTRUCTIONS

UNCONDITIONAL TRANSFER INSTRUCTIONS

CALL – call a procedure

RET – return execution from procedure

JMP – unconditional jump to specified destination

.M.M. Arun Prasath., AP/ECE 30

Page 31: Mpmc u2 ece_arun

CONDITIONAL TRANSFER INSTRUCTIONS

JA/JNBE - Jump if above/Jump if not below or not equal

JAE/JNB - jump if above or equal/Jump if not below

JB/JNAE - jump if below/jump if not above or not equal

JBE/JNA - jump if below or equal/jump if not above

JC – jump if carry

JE/JZ – jump if equal/jump if zero

JG/JNLE – jump if greater/jump if not less or not equal

JGE/JNL - jump if greater or equal/jump if not less than

JL/JNGE- jump if less/jump if not greater or not equal

Cont… .M.M. Arun Prasath., AP/ECE 31

4.CONTROL INSTRUCTIONS

Page 32: Mpmc u2 ece_arun

Cont…

CONDITIONAL TRANSFER INSTRUCTIONS

JLE/JNG - jump if less or equal/jump if not greater

JNC – jump if no carry

JNE/JNZ - jump if not equal/jump if not zero

JNO – jump if no overflow

JNP/JPO - jump if no parity/jump if parity odd

JNS – jump if no signed

JO- jump if overflow

JP/JPE - jump if parity/jump if parity even

JS- jump if sign .M.M. Arun Prasath., AP/ECE 32

4.CONTROL INSTRUCTIONS

Page 33: Mpmc u2 ece_arun

ITERATION CONTROL INSTRUCTIONS

LOOP – decresae CX, jump to label if CX not equal to 0. Count is loaded

in CX register

LOOPE/LOOPZ – decresae CX, jump to label if CX equal/decresae CX,

jump to label if CX not zero.

LOOPNE/LOOPNZ - decresae CX, jump to label if CX not

equal/decresae CX, jump to label if CX not zero.

JCXZ – jump if CX is zero.

.M.M. Arun Prasath., AP/ECE 33

4.CONTROL INSTRUCTIONS

Page 34: Mpmc u2 ece_arun

5.PROCESSOR CONTROL INSTRUCTIONS These instructions are used to change the process of the Microprocessor.

FLAG SET/CLEAR

INSTRUCTIONS

STC - set carry flag

CLC - clear carry flag

CMC - complement carry flag

STD - set direction flag

CLD - clear direction flag

STI - set interrupt enable flag

CLI- clear interrupt enable

flag

EXTERNAL HARDWARE

SYNCHRONIZATION INSTRUCTIONS

HLT - halt the system

WAIT - idle condition

ESC - When 8086 fetches an ESC instruction,

the coprocessor decodes the instruction and

carries out the action specified by the 6 bit

code. In case of 8086 it treats ESC instruction as

an NOP. The coprocessor treats all of the 8086

instruction as an NOP except ESC.

LOCK - bus lock signal

NOP - no operation .M.M. Arun Prasath., AP/ECE 34

Page 35: Mpmc u2 ece_arun

6.STRING CONTROL INSTRUCTIONS

• The string instructions function easily on blocks of memory. help for easy

program writing and execution. They can speed up the manipulating code.

REP/REPE/REPZ/REPNE/REPNZ - Repeat String instruction until

specified condition exist

MOVS/MOVSB/MOVSW - Move string byte or string word

Syntax: MOVS destination, source

COMPS/COMPSB/COMPSW – compare string

SCAS/SCASB/SCASW – Scan string byte or a string word.

LODS/LODSB/LODSW - This instruction copies a byte from a string

location pointed to by SI to AL or a word from a string location pointed to by

SI to AX. If DF is cleared to 0, SI will automatically incremented to point to the

next element of string.

STOS/STOSB/STOSW- Store byte or word in string. .M.M. Arun Prasath., AP/ECE 35

Page 36: Mpmc u2 ece_arun

7.INTERRUPT CONTROL INSTRUCTIONS

INT Interrupt type

INTO Interrupt on overflow

IRET Interrupt return

.M.M. Arun Prasath., AP/ECE 36