CS-202 Microprocessor and Assembly Language...CS-202 Microprocessor and Assembly Language Lecture 4...

Post on 25-Apr-2020

8 views 0 download

Transcript of CS-202 Microprocessor and Assembly Language...CS-202 Microprocessor and Assembly Language Lecture 4...

CS-202 Microprocessor and Assembly Language

Lecture 4Addressing Modes — I

Dr Hashim AliSpring - 2020

Department of Computer Science and EngineeringHITEC University Taxila

1

Addressing Modes in Microprocessor (8086/88)

• Addressing mode provides different ways for access an address to given data to a processor.

• When 8086 executes an instruction, it performs the specified function on data. Operated data is stored in the memory location. There are various techniques to specify address of data. These techniques are called Addressing Modes.

• Types of Addressing Modes:-

• Data Addressing Modes

• Program Memory Addressing Modes

• Stack Memory Addressing Modes

2

• Data-Addressing Modes:

• This mode is related to data transfer operation, that is, data is transferred either from the memory to internal registers of 8086 processors or from one register to another register.• Example: MOV AX, DX

• Program Memory Addressing Modes:

• This mode involves program memory addresses during various operations.• Example: JMP AX, in this instruction, the code execution control jumps to the

current code segment location addressed by the contents of AX register.

• Stack Memory Addressing Modes:

• This mode involves stack registry operations.• Example: PUSH AX, this instruction copies the contents of AX register to the stack.

3

Data-Addressing Modes

• An addressing mode means the method by which an operand can be specified in a register or a memory location.

• 8086/88 provide seven Addressing Modes:1. Register Addressing2. Immediate Addressing3. Direct Data Addressing4. Register Indirect Addressing5. Base-Plus-Index Addressing6. Register Relative Addressing7. Base Relative-Plus-Index Addressing

4

Opcode and Operand in Microprocessor

• An opcode is a short of “operation code”

• An opcode is a singe instruction can be executed by the CPU.

• In machine language it is a binary or hexadecimal value such as B7 loaded into the instruction register.

• In assembly language mnemonic form an opcode is a command such as MOV or ADD or JMP.

• Example:MOV AX, 1000H ; MOV is the opcode ; AX (register) is an operand

• Operands are manipulated by the opcode. In this example, the operands are the register AX and the value 1000H.

5

8-bit 8-bit 8-bitOpcode Operand1 Byte 1 or 2 Byte

Data-Addressing Modes

• MOV instruction is a common and flexible instruction.

• provides a basis for explanation of data-addressing modes

• Figure 3–1 illustrates the MOV instruction and defines the direction of data flow.

• Source is to the right and destination the left, next to the opcode MOV.

• an opcode, or operation code, tells the microprocessor which operation to perform

6

Figure 3–1  The MOV instruction showing the source, destination, and direction of data flow.

• The MOV AX, BX instruction transfers the contents of source register (BX) into the destination register (AX).

• The source never changes, but the destination always changes.

• MOV copies the source data into the destination.

• The source and destination are often called operands.

7

Figure 3–28086–Core2 data-addressing modes.

8

1. Register Addressing

• The most common form of data addressing.• Once register names learned, easiest to apply.

• The microprocessor contains these 8-bit register names used with register addressing: AH, AL, BH, BL, CH, CL, DH, and DL. • 16-bit register names: AX, BX, CX, DX, SP, BP, SI, and DI.

• Register addressing transfers a copy of a byte or word from the source register or contents of a memory location to the destination register or memory location.• Ex: MOV CX, DX

9

• Important for instructions to use registers that are the same size. • Never mix an 8-bit with a 16-bit register, an 8- or a 16-bit register with a 32-bit

register• This is not allowed by the microprocessor and results in an error when assembled.

• Use of registers to hold the data to be manipulated

• Memory is not accessed when this addressing mode is executed

• Example:MOV BX, DX ; copy the contents of DX into BXMOV ES, AX ; copy the contents of AX into ESADD AL, BH ; add the contents of BH to contents of AL

• Source and destination registers must have the same size

10

11

2. Immediate Addressing

• Transfers the source, an immediate byte or word of data, into the destination register or memory location

• The source operand is a constant

• The operand comes immediately after the opcode

• For this reason, this addressing mode executes quickly

• Immediate addressing mode can be used to load information into any of the registers except the segment registers and flag registers.

12

13

• Example: MOV AX, 2550H ; move 2550H into AX MOV CX, 625 ; load the decimal value 625 into CX MOV BL, 40H ; load 40H into BL

• The data must first be moved to a general-purpose register and then to the segment register.

• Example:MOV AX, 2550H MOV DS, AX MOV DS, 0123H ; illegal instruction!

14

• In symbolic assembly language, the symbol # precedes immediate data in some assemblers.

- MOV AX,#1234H instruction is an example.

• Most assemblers do not use the # symbol, but represent immediate data as in the MOV AX,1234H instruction.

- An older assembler used with some Hewlett-Packard logic development does, as may others.

- In this text, the # is not used for immediate data.

15

• The symbolic assembler portrays immediate data in many ways.

• The letter H appends hexadecimal data.

• If hexadecimal data begin with a letter, the assembler requires the data start with a 0.

- to represent a hexadecimal F2, 0F2H is used in assembly language.

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

- An example is the 100 decimal in the MOV AL,100 instruction.

16

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

- Ex: MOV AL,’A’

• Binary data are represented if the binary number is followed by the letter B.

- in some assemblers, the letter Y

- Ex: MOV CL,11001110B

17

Parts of Assembly Language

18

• Each statement in an assembly language program consists of four parts or fields.

• The leftmost field is called the label.

• used to store a symbolic name for the memory location it represents

• All labels must begin with a letter or one of the following special characters: @, $, -, or ?.

• a label may any length from 1 to 35 characters

• The label appears in a program to identify the name of a memory location for storing data and for other purposes.

19

• The next field to the right is the opcode field.

- designed to hold the instruction, or opcode

- the MOV part of the move data instruction is an example of an opcode • Right of the opcode field is the operand field.

- contains information used by the opcode- the MOV AL,BL instruction has the opcode MOV and operands AL and BL

• The comment field, the final field, contains a comment about the instruction(s). - comments always begin with a semicolon (;)

3. Direct Data Addressing

20

• Moves a byte or word between a memory location and a register.

• The data is in some memory location(s) and the address of the data in memory comes immediately after the instruction

- usually a 3-byte long instruction

• This address is the offset address

• Example: MOV AL, [8088] ; move content of DS:8088 into AL

• The physical address is calculated by combining the contents of offset location 2400 with DS

21

• Example: Find the physical address of the memory location and its contents after the execution of the following, assuming that DS = 1512H.

MOV AL, 3BH

MOV [3518], AL• Solution:

- First 3BH is copied into AL

- Then in line two, the contents of AL are moved to logical address DS:3518 which is 1512:3518.

- Shifting DS left and adding it to the offset gives the physical address of 18638H (15120H + 3518H = 18638H).

- After the execution of the second instruction, the memory location with address 18638H will contain the value 3BH.

Few More Examples

22

4. Register Indirect Addressing

23

• Transfers a byte or word between a register and a memory location addressed by an index or base register.

• The address of the memory location where the operand resides is held by a register.

• The registers used for this purpose are SI, DI, and BX.• They must be combined with DS (by default) in order to

generate the 20-bit physical address.- The register BP uses the stack segment (SS) by default.

24

• For example:

- MOV AL, [BX]

- MOV AL, [BP]

- MOV AL, [SI]

- MOV AL, [DI]

• Segment override prefix symbols can be used if you wish to access data in different segments.

- MOV AL, CS:[BX]

- MOV AL, DS:[BP]

- MOV AL, SS:[SI]

- MOV AL, ES:[DI]

25

• Example:

• The physical address is calculated as

• The same rules apply when using register SI or DI.

• Example:

MOV AX, [BX] ; moves into AX the contents of the memory location pointed to by DS:BX, 1000:1234

1000x10+1234=11234H

MOV CL, [SI]

MOV [DI], AH

; move contents of DS:SI into CL

; move contents of AH into DS:DI

26

• Example:

- Assume that DS = 1120H, SI = 2498H, and AX = 17FEH Show the contents of memory locations after the execution of

• Solution:

- The contents of AX are moved into memory locations with logical address DS:SI and DS:SI + 1 (Two bytes because of AX)

- The physical address starts at DS (shifted left) + SI = 13698. According to the little endian convention,

- Low address 13698H contains FE, the low byte,

- High address 13699H will contain 17, the high byte.

MOV [SI], AX

; move contents of AX into DS:SI

Few Examples

27

5. Base-Plus-Index Addressing

28

• Transfers a byte or word between a register and the memory location addressed by a base register (BP or BX) plus an index register (DI or SI).

• Combining based and indexed addressing modes.• One base register and one index register are used.

• Examples:

- Physical Address = DSx10 + BX+DI

- Physical Address = DSx10 + BX+SI

- Physical Address = SSx10 + BP+DI

- Physical Address = SSx10 + BP+SI

MOV [BX+DI], CL ; move contents of CL into DS:BX+DI

MOV CH, [BX+SI] ; move contents of the DS:BX+SI into CH

MOV AH, [BP+DI] ; move contents of the SS:BP+SI into AH

MOV [BP+SI], AL ; move contents of AL into SS:BP+SI

Few Examples

29

• Moves a byte or word between a register and the memory location addressed by an index or base register plus a displacement.

• The data in a segment of memory are addressed by adding the displacement to the contents of a base or an index register (BP, BX, DI, or SI).

• Examples:

- Physical Address = DSx10 + BX+4

- Physical Address = DSx10 +SI+5

- Physical Address = DSx10 + DI+1

- Physical Address = SSx10 + BP+2

6. Register Relative Addressing

30

MOV AX, [BX+4] ; move contents of DS:BX+4 into AX

MOV CH, [SI+5] ; move contents of the DS:SI+5 into CH

MOV AH, [DI+1] ; move contents of the DS:DI+1 into AH

MOV [BP+2], AL ; move contents of AL into SS:BP+2

31

• Example: • Assume that DS = 4500, SS = 2000, BX = 2100, SI = 1486, DI = 8500, BP= 7814, and AX = 2512.

Show the exact physical memory location where AX is stored in each of the following. All values are in hex.

• Solution: Physical Address = segment reg. x 10 + (offset reg.) + displacement

1- MOV [BX+20], AX2- MOV [SI+10], AX3- MOV [DI+4], AX4- MOV [BP+12], AX

1- DS:BX+202- DS:SI+103- DS:DI+44- SS:BP+12

location 47120 = (12) and 47121 = (25) location 46496 = (12) and 46497 = (25 ) location 4D504 = (12) and 4D505 = (25) location 27826 = (12) and 27827 = (25)

Few Examples

32

7. Base Relative-Plus-Index Addressing

33

• This type of addressing mode often addresses a two-dimensional array of memory data.

• The data in a segment of memory are addressed by adding the displacement to the contents of a base and an index register (BP, BX, DI, or SI).

• The base relative-plus-index addressing mode is similar to the base-plus-index addressing mode, but adds a displacement besides using a base register and an index register to form the memory address.

34

• Examples:

- Physical Address = DSx10 + BX+DI+1H

- Physical Address = DSx10 + BX+SI+10H

- Physical Address = SSx10 + BP+DI+3H

- Physical Address = DSx10 + BX+DI+FILE

MOV [BX+DI+1], AX ; move contents of AX into DS:BX+DI+1

MOV AX, [BX+SI+10] ; move contents of the DS:BX+SI+10 into AX

MOV AH, [BP+DI+3] ; move contents of the SS:BP+SI+3 into AH

MOV AX, FILE[BX+DI] ; move contents of the DS:FILE+BX+DI into AX

Few Examples

35

Offset Registers For Various Segments

• The following Table provides a summary of the offset registers that can be used with the four segment registers of the 8086/8088.

36

Segment Register CS DS ES SS

Offset Register IP SI, DI, BX SI, DI, BX SP, BP

Easy Way to Remember 8086/88 Addressing Modes

37

• Total 17 different memory addressing modes:-

- DISP, [BX], [BP], [SI], [DI], [BX+DISP], [BP+DISP],[SI+DISP], [DI+DISP], [BX+SI], [BX+DI], [BP+SI], [BP+DI],[BX+SI+DISP], [BX+DI+DISP], [BP+SI+DISP], and [BP+DI+DISP]

‣ DISP = Displacement

Few Examples

38

Data-Addressing Modes

• An addressing mode means the method by which an operand can be specified in a register or a memory location.

• 8086/88 provide seven Addressing Modes: 1. Register Addressing 2. Immediate Addressing 3. Direct Data Addressing 4. Register Indirect Addressing 5. Base-Plus-Index Addressing 6. Register Relative Addressing 7. Base Relative-Plus-Index Addressing

39

Direct Data Addressing

40

Example: Declare two byte size data, two word size data in data segment. Demonstrate direct data addressing mode.

.MODEL SMALL ; choose small model0000 .DATA ; start data segment0000 DATA1 DB 10H ; place 10H into DATA10001 DATA2 DB 0 ; place 00H into DATA20002 DATA3 DW 0 ; place 0000H into DATA30004 DATA4 DW 0AAAAH ; place AAAAH into DATA4

0000 .CODE ; start code segment.STARTUP ; start program

0017 MOV AL, DATA1 ; copy DATA1 into AL001A MOV AH, DATA2 ; copy DATA2 into AH001E MOV DATA3, AX ; copy AX into DATA30021 MOV BX, DATA4 ; copy DATA4 into BX

.EXIT ; exit to DOSEND ; end program

Memory Addresses

DS

CS

Register Indirect Addressing

41

Example: Use Register Indirect addressing to write a program which refer to tabular data located in the memory system. you must create a table of information that contains 50 samples taken from memory location 0000:046C.

.MODEL SMALL ; choose small model

.DATA ; start data segmentDATAS DW 50 DUP(?) ; setup array of 50 words

.CODE ; start code segment

.STARTUP ; start programMOV AX, @DATAMOV DS, AX ; address segment 0000 with DS MOV BX, OFFSET DATAS ; address DATAS array with BXMOV CX, 50 ; load counter with 50

AGAIN: MOV AX, [046CH] ; get clock valueMOV [BX], AX ; save clock value in DATASINC BX ; increment BX to next element INC BXLOOP AGAIN ; repeat 50 times.EXIT ; exit to DOSEND ; end program

Base-Plus-Index Addressing

42

Example: Write a program which moves array element 10H into array element 20H.

.MODEL SMALL ; choose small model

.DATA ; start data segment

ARRAY DB 40 DUP(?) ; setup array of 40 bytes

.CODE ; start code segment

.STARTUP ; start program

MOV BX, OFFSET ARRAY ; address ARRAY array with BX

MOV DI, 10H ; address element 10H

MOV AL, [BX+DI] ; get element 10H

MOV DI, 20H ; address element 20H

MOV [BX+DI], AL ; save in element 20H

.EXIT ; exit to DOS

END ; end program

Base Relative-Plus-Index Addressing

43

Example: Write a program that copies element 0 of record A into element 2 of record C by using the base relative-plus-index mode of addressing. FILE contains four records and each record contains 10 elements. THIS BYTE statement is used to define the label FILE and RECA as the same memory location.

.MODEL SMALL ; choose small model

.DATA ; start data segmentFILE EQU THIS BYTE ; assign FILE to this byte RECA DB 10 DUP(?) ; 10 bytes for record A RECB DB 10 DUP(?) ; 10 bytes for record BRECC DB 10 DUP(?) ; 10 bytes for record CRECD DB 10 DUP(?) ; 10 bytes for record D

.CODE ; start code segment

.STARTUP ; start programMOV BX, OFFSET RECA ; address RECA with BXMOV SI, 0 ; address element 0 MOV AL, FILE[BX+SI] ; get element 0 of RECA

OF FFMOV BX, OFFSET RECC ; address of RECCMOV DI, 0MOV [BX+DI+2], AL ; save in element 2 of RECC .EXIT ; exit to DOSEND ; end program