Communication Theory II - ECED Mansoura...Text Book/References* Textbook: The Intel Microprocessors,...

Preview:

Citation preview

Microprocessor (COM 9323)

Lecture 7: Addressing Modes

Ahmed Elnakib, PhD

Assistant Professor, Mansoura University, Egypt

1March 9nd, 2016

Text Book/References*

Textbook:The Intel Microprocessors, Architecture, Programming and Interfacing, 8th edition,

Barry B. Brey, Prentice Hall, 2009 (Chapter 03)

2

*The presentation is based on the text book (Tables and Figures)

o Explain the operation of each data-addressing mode

o Use the data-addressing modes to form assembly language statements

o Explain the operation of each program memory-addressing mode

o Use the program memory-addressing modes to form assembly and

machine language statements

o Select the appropriate addressing mode to accomplish a given task

o Detail the difference between addressing memory data using real mode

and protected mode operation

o Describe the sequence of events that place data onto the stack or

remove data from the stack

o Explain how a data structure is placed in memory and used with software

3

Objectives

o Efficient software development for the microprocessor requires a

complete familiarity with the addressing modes employed by

each instruction

To improve microprocessor programming skills

To get familiar with assembly language

4

Why Studding Addressing Modes?

o The MOV (move data) instruction is used to describe the data-

addressing modes

o The MOV instruction transfers bytes or words or doublewords of data

between two registers (data addressing modes)

between registers and memory (program memory addressing modes)

5

MOV (move data) Instruction

o The source is to the right and the destination is to the left, next to the opcode

MOV

o The source and destination are often called operands

o An opcode, or operation code, tells the microprocessor which operation to

perform

o This direction of flow: move from right to left

6

MOV Instruction FormatDirection of flow

opcode operands

o Notice that a comma always separates the destination from the source in an

instruction

o Also, note that memory-to-memory transfers are not allowed by any instruction

except for the MOVS instruction

7

MOV Instruction Format (cont’d)Direction of flow

opcode operands

o The MOV AX, BX instruction transfers the word contents of the source register

(BX) into the destination register (AX)

o The source never changes, but the destination always changes

MOV instruction always copies the source data into the destination

The MOV never actually picks up the data and moves it

8

MOV Instruction JobDirection of flow

opcode operands

o The flag register remains unaffected by most data transfer instructions

o The flag bits are normally modified by arithmetic or logic instructions

9

MOV Instruction Format: Flag RegistersDirection of flow

opcode operands

o The data-addressing modes include:

1. Register

2. Immediate

3. Direct

4. Register indirect

5. Base plus index

6. Register-relative

7. Base relative-plus-index

8. scaled-index mode (included in 80386 and above )

9. RIP relative addressing (available to 64-bit Pentium 4 or Core2)

o The program memory-addressing modes include:

1. Program relative

2. Direct

3. Indirect 10

Types of Addressing Mode

all possible

variations of

the data-

addressing

modes

using the

MOV

instruction

11

Data Addressing

Mode

1. Register Addressing

2. Immediate Addressing

3. Direct Addressing

4. Register indirect Addressing

5. Base plus index Addressing

6. Register-relative Addressing

7. Base relative-plus-index Addressing

8. Scaled-index mode Addressing

9. RIP relative Addressing

Data-addressing modes

12

13

Data Addressing Modes:1. Register Addressing

o Register addressing is the most common form of data addressing

and, once the register names are learned, is the easiest to apply

o Register addressing transfers a copy of a byte or word from the

source register to the destination register

o Example: The MOV CX, DX instruction copies the word-sized

contents of register DX into register CX.

CX, DXDX CX

14

o In the 80386 and above, a doubleword can be transferred from

the source register or memory location to the destination

register or memory location

o Example: The MOV ECX, EDX instruction copies the doubleword

sized contents of register EDX into register ECX.

Data Addressing Modes:1. Register Addressing (cont’d)

EDX ECXECX, EDX

15

o In the Pentium 4 operated in the 64-bit mode, any 64-bit register

is also allowed

o Example: The MOV RDX, RCX instruction that transfers a copy of the quadword contents of register RCX into register RDX

Data Addressing Modes:1. Register Addressing (cont’d)

RDX, RCXRCX RDX

16

Microprocessor Registers used with Register Addressing

8-bit 16-bit 32-bit 64-bit

AH, AL, BH, BL, CH, CL, DH, and DL

AX, BX, CX, DX EAX, EBX, ECX, EDX, RAX, RBX, RCX,

RDX

SP, BP, SI, and DIESP, EBP, EDI, and

ESIRSP, RBP, RDI, RSI

*CS, ES, DS, SS, FS, and GS

R8 through R15

Data Addressing Modes:1. Register Addressing (cont’d)

* With register addressing, some MOV instructions and the PUSH and POP instructions also use the 16-bit segment register names (CS, ES, DS, SS, FS, and GS).

17

Recall: The Intel Programming Model

General PurposeRegisters

Stack pointer

18

Recall: The Intel Programming Model (cont’d)

General PurposeRegisters

19

Recall: The Intel Programming Model (cont’d)

Segment Registers

20

o It is important for instructions to use registers that are the same sizeo Never mix an 8-bit register with a 16-bit register, an 8-bit register with

a 32-bit register, or a l6-bit register with a 32-bit register because this is not allowed by the microprocessor and results in an

error when assembledo Likewise never mix 64-bit registers with any other size register

Data Addressing Modes:1. Register Addressing (cont’d)

RDX, RCXRCX RDX

21

o This is even true when a MOV AX, AL (MOV EAX, AL) instruction may seem to make sense Of course, the MOV AX, AL or MOV EAX, AL instructions are not

allowed because the registers are of different sizes o Note that a few instructions, such as SHL DX, CL, are exceptions to this

rule, as indicated in later chapters of the book

Data Addressing Modes:1. Register Addressing (cont’d)

RDX, RCXRCX RDX

22

o It is also important to note that none of the MOV instructions affect the flag bits

o The flag bits are normally modified by arithmetic or logic instructions

Data Addressing Modes:1. Register Addressing (cont’d)

RDX, RCXRCX RDX

23

o How many 8-bit subset of the MOV instruction?

- combinations are 64 different

variations (how?)

Data Addressing Modes:1. Register Addressing (cont’d)

Examples of register addressing instructions

24

o A segment-to-segment register MOV instruction is about the

only type of register MOV instruction not allowed

o The code segment register is not normally changed by a

MOV instruction because the address of the next instruction

is found by both IP/EIP and CS

If only CS were changed, the address of the next instruction

would be unpredictable

Therefore, changing the CS register with a MOV instruction is not allowed.

Data Addressing Modes:1. Register Addressing (cont’d)

RDX, RCXRCX RDX

25

Data Addressing Modes:1. Register Addressing (cont’d)

o The effect of executing the MOV BX, CX instruction at the point just

before the BX register changes

The source register’s contents do not change, but the destination

register’s contents do change

Only the rightmost 16 bits of register EBX change

The operation of the

MOV BX, CX

instruction

26

Data Addressing Modes:1. Register Addressing (cont’d)

o This instruction moves (copies) a l234H from register CX into register BX

This erases the old contents (76AFH) of register BX, but the contents

of CX remain unchanged

o The contents of the destination register or destination memory location

change for all instructions except the CMP and TEST instructions

The operation of the

MOV BX, CX

instruction

27

Data Addressing Modes:1. Register Addressing (cont’d)

o Example shows a sequence of assembled instructions that copyvarious data between 8-, 16-, and 32-bit registers

As mentioned, the act of moving data from one register to another changes only the destination register, never the source

Example:

sequence of

assembled

instructions

28

Data Addressing Modes:1. Register Addressing (cont’d)

o The last instruction in this example (MOV CS,AX) assembles without error, but causes problems if executed

o If only the contents of CS change without changing IP The next step in the program is unknown and therefore causes

the program to not working correctly

Example:

sequence of

assembled

instructions

29

Data Addressing Modes:1. Register Addressing (cont’d)

What is this?

Example:

sequence of

assembled

instructions

30

Data Addressing Modes:1. Register Addressing (cont’d)

o The hexadecimal number at the far left is the offset address of the instruction or data This number is generated by the assembler

Example:

sequence of

assembled

instructions

Offset address

31

Data Addressing Modes:1. Register Addressing (cont’d)

o The number or numbers to the right of the offset address are the machine-coded instructions or data Also generated by the assembler

Example:

sequence of

assembled

instructions

Machine-codedInstruction

32

Example: if the instruction MOV AX,0 appears in a file and it is assembledo It appears in offset memory location 0100 in the Exampleo Its hexadecimal machine language form is B8 0000o The B8 is the opcode in machine language and the 0000 is the 16-bit-wide data with a

value of zeroo When the program was written, only the MOV AX,0 was typed into the editor

short assembly

language

program

that places

0000H into the

16-bit registers

AX, BX, and CX.

Data Addressing Modes:1. Register Addressing (cont’d)

Data-addressing modes

33

1. Register Addressing

2. Immediate Addressing

3. Direct Addressing

4. Register indirect Addressing

5. Base plus index Addressing

6. Register-relative Addressing

7. Base relative-plus-index Addressing

8. Scaled-index mode Addressing

9. RIP relative Addressing

34

Data Addressing Modes: 2. Immediate Addressing

22H ALAL, 22H

o The term immediate implies that the data immediately follow the

hexadecimal opcode in the memory

o Immediate data are constant data, whereas the data transferred from a

register or memory location are variable data

o The MOV immediate instruction transfers a copy of the immediate data into

a register or a memory location

35

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o Figure shows the operation of a MOV EAX,13456H instruction

o This instruction copies the 13456H from the instruction, located in the

memory immediately following the hexadecimal opcode, into register EAX

o As with the register addressing MOV instruction, the source data overwrites the destination data

36

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o Immediate addressing transfers the source, an immediate byte, word, doubleword, or quadword of data, into the destination register or memory location

o Example: The MOV AL, 22H instruction copies a byte-sized 22H into register AL.

22H ALAL, 22H

37

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o In the 80386 and above, a doubleword of immediate data can be transferred into a register or memory location

o Example: The MOV EBX, 12345678H instruction copies a doubleword-sized l2345678H into the 32-bit-wide EBX register

12345678H EBXEBX, 12345678H

38

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o In 64-bit operation of the Pentium 4 or Core2, only a MOV immediate instruction allows access to any location in the memory using a 64-bit linear address.

o Example: The MOV RBX, 0123456789ABCDEFH instruction copies a quadword-sized 0123456789ABCDEFH into the 64-bit-wide RBX register

0123456789ABCDEFH RBX

RBX,0123456789ABCDEFH

39

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o The symbolic assembler portrays immediate data in

many ways

o The letter H appends hexadecimal data

o If hexadecimal data begin with a letter, the assembler requires that the data start with a 0

o F2 is presented as 0F2H in assembly language

F2H ALAL, 0F2H

40

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o In this text, we present the immediate MOV instruction

as MOV AX,3456H not MOV AX,#3456H as some other

assemblers do

3456H AXAX, 3456H

41

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o Some assemblers (though not in MASM, TASM, or this text), hexadecimal data are represented with an ’h, as in MOV AX,#’h1234 MASM (MACRO assembler) is a trademark of Microsoft Corporation

1234H AXAX, 01234H

42

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o Decimal data are represented as is and require no special codes or adjustments

o Example is the 100 decimal in the MOV AL,100 instruction

100 ALAL, 100

43

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o An ASCII-coded character or characters may be depicted in the immediate form if the ASCII data are enclosed in apostrophes

o Example: The MOV BH, ‘A’ instruction, which moves an ASCII-coded letter A [41H] into register BH

41H BHBH, ‘A’

44

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o Binary data are represented if the binary number is followed by the letter B, or, in some assemblers, the letter Y

o Example: The MOV EAX, 100B instruction, which moves an the binary number 100 into register EAX

100B EAXEAX, 100B

45

Data Addressing Modes: 2. Immediate Addressing (cont’d)

Examples of

immediate

addressing

using the MOV instruction

The ASCII characters

are stored as BA, so

exercise care when

using word-sized pairs

of ASCII characters.

46

Data Addressing Modes: 2. Immediate Addressing (cont’d)

Examples of

immediate

addressing

using the MOV instruction

The ASCII characters

are stored as BA, so

exercise care when

using word-sized pairs

of ASCII characters.

47

Data Addressing Modes: 2. Immediate Addressing (cont’d)

short assembly

language

program

that places

0000H into the

16-bit registers

AX, BX, and CX.

o This is followed by instructions that use register addressing to copy the contents of AX into registers SI, DI, and BP

48

Data Addressing Modes: 2. Immediate Addressing (cont’d)

short assembly

language

program

that places

0000H into the

16-bit registers

AX, BX, and CX.

o The .MODEL TINY statement directs the assembler to assemble the program into a single code segment

o The .CODE statement or directive indicates the start of the code segmento The .STARTUP statement indicates the starting instruction in the program

49

Data Addressing Modes: 2. Immediate Addressing (cont’d)

short assembly

language

program

that places

0000H into the

16-bit registers

AX, BX, and CX.

o The .EXIT statement causes the program to exit to DOo The END statement indicates the end of the program fileo This program is assembled with MASM and executed with CodeView4 (CV) to view its

execution

50

Data Addressing Modes: 2. Immediate Addressing (cont’d)

short assembly

language

program

that places

0000H into the

16-bit registers

AX, BX, and CX.

o To store the program into the system use the DOS EDIT program, Windows NotePad or Programmer’s WorkBench (PWB)

o Note that a TINY program always assembles as a command (.COM) program

51

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o The assembler generated the machine code and addresses, and stored the program in a file with the extension .LST.

short assembly

language

program

that places

0000H into the

16-bit registers

AX, BX, and CX.

52

o Each statement in an assembly language program consists of

four parts or fields

o The leftmost field is called the label

o It is used to store a symbolic name for the memory location

that it represents

Data Addressing Modes: 2. Immediate Addressing (cont’d)

If the program is assembled and the list (.LST) file is viewed, it appears as follows:

(.LST) file

53

o All labels must begin with a letter or one of the following

special characters: @, $, -, or ?

o A label may be of any length from 1 to 35 characters

o The label appears in a program to identify the name of a

memory location for storing data and for other purposes

that are explained as they appear.

Data Addressing Modes: 2. Immediate Addressing (cont’d)

If the program is assembled and the list (.LST) file is viewed, it appears as follows:

54

o The next field to the right is called the opcode field

o It is designed to hold the instruction, or opcode.

o The MOV part of the move data instruction is an example of an

opcode

Data Addressing Modes: 2. Immediate Addressing (cont’d)

If the program is assembled and the list (.LST) file is viewed, it appears as follows:

55

o To the right of the opcode field is the operand field, which

contains information used by the opcode

o For example, the MOV AL,BL instruction has the opcode MOV

and operands AL and B

o Note that some instructions contain between zero and three

operands

Data Addressing Modes: 2. Immediate Addressing (cont’d)

If the program is assembled and the list (.LST) file is viewed, it appears as follows:

o The final field, the comment field, contains a comment about

an instruction or a group of instructions.

o A comment always begins with a semicolon (;).

Data Addressing Modes: 2. Immediate Addressing (cont’d)

If the program is assembled and the list (.LST) file is viewed, it appears as follows:

56

o Programs are also written using the inline assembler in some Visual C++ programs

o Example shows a function in a Visual C++ program that includes some code written with the inline assembler

Data Addressing Modes: 2. Immediate Addressing (cont’d)

o This function adds 20H to the number returned by the function

o Notice that the assembly code accesses C++ variable temp and all of the assembly code is placed in an _asm code block

57

Questions

58

Recommended