Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in...

30
Lecture 2 Symbolic Instruction and Addressing 1

Transcript of Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in...

Page 1: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Lecture 2

Symbolic Instruction and Addressing

1

Page 2: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Chapter Outline

Register

Data Transfer Instruction

Basic Arithmetic Instruction

The INT Instruction

2

Page 3: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Registers • Information inside the microprocessor is stored in registers.

• The registers are classified according to the functions they perform

In general there are fourteen 16-bit registers:

• Data registers:

• There are four general data registers.

• They hold data for an operation.

• Address registers:

• They are divided into segment, pointer, and index registers.

• They hold the address of an instruction or data.

• Status register:

• It is called the FLAGS register.

• It keeps the current status of the processor.

3

Page 4: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Registers

AH AL

BH BL

CH CL

DH DL

AX

BX

CX

DX

CS

DS

SS

ES

SI

DI

SP

BP

IP

Data Registers

Segment Registers

Pointer and Index Registers

FLAGS Register

4

Page 5: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Data Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers,

also perform special functions.

•The high and low bytes of these registers can be accessed separately.

• Ex. The high byte of AX is called AH, and the low byte is called AL.

• This arrangement gives us more registers to use when dealing

with byte-size data.

5

Page 6: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Data Registers: AX, BX, CX, DX • AX (Accumulator Register) is the preferred register to use in

arithmetic, logic, and data

transfer instructions

• BX (Base Register) also serves as an address register.

• CX (Count Register) Program loop constructions are facilitated by

the use of CX, which

serves as a loop counter.

•DX (Data Register) is used in multiplication and division.

6

Page 7: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Address Registers - Segment Registers CS, DS, SS, ES • Address registers store addresses of instructions and data in memory.

• These values are used by the processor to access memory locations.

• In the 8086 processor (16-bit processor):

• Memory is a collection of bytes, each memory byte has an

address, starting with 0.

• The processor assigns a 20-bit physical address to its memory

locations thus it is possible to address 2 = 1,048,576 bytes

(one megabyte) of memory.

• The bytes in memory have addresses 00000h to FFFFFh.

20

7

Page 8: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Address Registers - Segment Registers CS, DS, SS, ES • To keep track of the various program segments, the 8086 is

equipped with four segments registers to hold segment numbers:

• CS (Code Segment): contains the code segment number.

• DS (Data segment): contains the data segment number.

• SS (Stack Segment): contains the stack segment number.

• ES (Extra Segment): is used if a program needs to access a

second data segment.

8

Page 9: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Address Registers -

Pointer and Index Registers: SP, BP, SI, DI

• SP (Stack Pointer) register is used in conjunction with SS for

accessing the stack segment.

• BP (Base Pointer) register is used primarily to access data on the

stack. However, unlike SP, BP can be used to access data in the

other segments.

• SI (Source Index) register is used to point to memory locations in

the data segment addressed by DS. By incrementing the contents

of SI, we can easily access consecutive memory locations.

• DI (Destination Index) register performs the same functions as SI.

There is a class of instructions, called string operations, that use DI

to access memory locations addressed by ES. 9

Page 10: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Address Registers - Instruction Pointer (IP)

• IP is updated each time an instruction is executed so that it will point

to the next instruction.

• Unlike other registers, the IP cannot be directly manipulated by an

instruction (i.e. The instruction cannot contain IP as its operand).

10

Page 11: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

11

Data Transfer Instruction

MOV

MOVSX-MOVZX

XCHG

LEA

Page 12: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

MOV Instruction

• The MOV (move) instruction is used to:

• Transfer data between Registers.

• Transfer data between registers and memory locations.

• Move a number directly into a register or memory location.

• Syntax:

MOV destination, source

• Example:

MOV AX, WORD1

MOV AX, BX

MOV AX, 'A'

Before After

0006 0008

AX AX

0008 0008

WORD1 WORD1

Note: any register can be used except CS & IP

12

Page 13: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Legal Combinations of operands for MOV

Destination Operand

General Segment Memory

Source operand register register location Constant

General register yes yes yes no

Segment register yes no yes no

Memory location yes yes no no

Constant yes no yes no

• Illegal: MOV WORD1, WORD2 • Legal: MOV AX, WORD2

MOV WORD1, AX

• Illegal: MOV DS, CS • Legal: MOV AX, CS

MOV DS, AX

13

Page 14: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Type Agreement of Operands • The operands of any two-operand instruction must be of the same

type (i.e. Both bytes or words).

• Illegal: MOV AX, BYTE1

• However, the assembler will accept both of the following:

• MOV AH, 'A' moves 41H into AH

• MOV AX, 'A' moves 0041H into AX

14

Page 15: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

MOVE-and-Fill Instruction (MOVSX-MOVZX)

• The MOVSX (move) instruction is used to:

• Move a byte or word source to a word or doubleword destination.

•Use with signed arithmetic values

• Syntax:

MOVSX destination, source

• Example:

MOVSX CX,10110000B . CX= 11111111 10110000

15

Page 16: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

MOVE-and-Fill Instruction (MOVSX-MOVZX)

• The MOVZX (move) instruction is used to:

• Move a byte or word source to a word or doubleword destination.

•Use with unsigned arithmetic values

• Syntax:

MOVZX destination, source

• Example:

MOVZX CX,10110000B . CX= 00000000 10110000

16

Page 17: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

XCHG Instruction

• The XCHG (exchange) operation is used to exchange the contents

of:

• Two registers.

• A register and a memory location.

• Syntax:

XCHG destination, source

• Example:

XCHG AH, BL

XCHG AX,WORD1

Before After

AH AL AH AL

BH BL BH BL

1A 00 05 00

00 05 00 1A

17

Page 18: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Legal Combinations of operands for XCHG

Destination Operand

General Memory

Source Operand register location

General register yes yes

Memory location yes no

18

Page 19: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

19

Basic Arithmetic Instruction

ADD

SUB

INC

DEC

Page 20: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

ADD and SUB Instructions • The ADD (add) and SUB (subtract) instructions are used to:

• Add/subtract the contents of:

• Two registers.

• A register and a memory location.

• Add/subtract a number to/from a register or memory location.

• Syntax:

ADD destination, source SUB destination, source

• Examples:

ADD WORD1, AX SUB AX, DX

Before After 01BC 01BC

AX AX 0523 06DF

WORD1 WORD1

Before After 0000 FFFF

AX AX 0001 0001

DX DX

20

Page 21: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Legal Combinations of operands for ADD & SUB

• Illegal: ADD BYTE1, BYTE2

• Legal: MOV AL, BYTE2

ADD BYTE1, AL

Destination Operand

General Memory

Source Operand register location

General register yes yes

Memory location yes no

Constant yes yes

21

Page 22: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

INC and DEC Instructions • INC (increment) is used to add 1 to the contents of a register or

memory location.

• DEC (decrement) is used to subtract 1 from a register or memory

location.

• Syntax:

INC destination DEC destination

• Examples:

INC WORD1 DEC BYTE1

Before After

WORD1 WORD1

0002 0003

Before After

BYTE1 BYTE1

FFFE FFFD

22

Page 23: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

INT Instruction • To invoke a DOS or BIOS routine, the INT (interrupt) instruction is

used.

• Format:

INT interrupt_number

where interrupt_number is a number that specifies a routine.

Page 24: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

INT 21h

• INT 21h may be used to invoke a large number of DOS functions.

• A particular function is requested by placing a function number in

the AH register and invoking INT 21h.

• Some of the functions are:

• INT21h functions expect input values to be in certain registers and

return output values in other registers.

Function number Routine

1 single-key input

2 single-character output

9 character string output

Page 25: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

INT 21h

• To invoke the routine, the following instructions should be executed:

MOV AH,1 ; input key function

INT 21H ; ASCII code in AL

Function 1: Single-Key Input

Input: AH = 1

Output: AL = ASCII code if character key is pressed

= 0 if non-character key is pressed

Page 26: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

INT 21h

• To invoke the routine, the following instructions should be executed:

MOV AH, 2 ; display character function

MOV DL, '?' ; character is '?' (or any other character)

INT 21H ; display character

Function 2: Display a character or execute a control function

Input: AH = 2

DL = ASCII code of the character

Output AL = ASCII code of the character

Page 27: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

INT 21h

• Function 2 may be used to perform control functions.

• If DL contains the ASCII code of a control character, INT 21h

causes the control function to be performed.

• The principal control characters are :

ASCII code (Hex) Symbol Function

07H BEL beep (sounds a tone)

08H BS backspace

09H HT tab

0AH LF line feed (new line)

0DH CR carriage return (start of current line)

Page 28: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

Data Definition + Basic Instructions 17

LEA Instruction

• LEA (Load Effective Address) puts a copy of the source offset

address into the destination.

• Syntax:

LEA destination, source

Where destination is a general register and source is a memory

location

• Example:

MSG DB 41H, 42H, 43H

LEA DX, MSG

puts the offset address of the variable MSG into DX.

28

Page 29: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

INT 21h

• To invoke the routine, the following instructions should be executed:

MOV AX, @DATA

MOV DS, AX

MOV AH, 9 ; display string function

LEA DX, MSG ; get message (Load Effective Address)

INT 21H ; display string

Function 9: Display a string

Input: AH = 9

DX = offset address of string.

The string must end with a '$' character

A program containing a data segment should

begins with these two instructions

Page 30: Lecture 2 Symbolic Instruction and Addressing Registers: AX, BX, CX, DX • These four registers, in addition to being general-purpose registers, also perform special functions. •The

INT 21h

• To invoke the routine, the following instructions should be executed:

MOV AH, 4CH ; DOS exit function

INT 21H ; exit to DOS

Function 4CH: Returning control to DOS

Input: AH = 4CH