51
Addressing modes •Addressing modes is the way of representing the location of an operand •Three types •Data-Addressing Modes 8086 through 80286: register, immediate, direct, register indirect, base-plus-index, register relative,and base relative-plus-index 80386 and above: scaled-index MOV( Move data) instruction –moves byte, word or double word •Program Memory-Addressing Modes CALL and JUMP instructions program relative, direct, and indirect •Stack Memory-Addressing Modes PUSH and POP instructions

module2.2-Addressing modes

Embed Size (px)

Citation preview

Page 1: module2.2-Addressing modes

Addressing modes

•Addressing modes is the way of representing the location of an operand •Three types•Data-Addressing Modes

8086 through 80286: register, immediate, direct, register indirect, base-plus-index, register relative,and base relative-plus-index

80386 and above: scaled-indexMOV( Move data) instruction –moves byte, word or double word

•Program Memory-Addressing ModesCALL and JUMP instructionsprogram relative, direct, and indirect

•Stack Memory-Addressing ModesPUSH and POP instructions

Page 2: module2.2-Addressing modes

Data Addressing Mode

MOV (move data) instruction: transfer bytes/words/ doublewords of data between registers, or between register and memory

– MOVS: memory-to-memory transfer for strings

• opcode (operation code): MOV

• operands: AX and BX

Page 3: module2.2-Addressing modes

8086-P4 Data-addressing Modes

Page 4: module2.2-Addressing modes

1. Register Addressing• Transfer a copy of a byte or word or double word from the source

register or memory location to the destination register or memory location.

• Eg. MOV CX,DX MOV ECX,EDX• Most common form• Instructions must use registers of same size• None of the MOV instruction effect the flag bits.• Segment to segment register move is not allowed• Changing CS register with a mov instruction is not allowed.• While using mov instruction the source register content do not

change but destination changes.• The content of destination changes for all instruction except the

CMP and TEST instructions

Page 5: module2.2-Addressing modes

Examples of register addressing

Page 6: module2.2-Addressing modes

Operation of MOV BX,CX instruction

Page 7: module2.2-Addressing modes

A sequence of assembled instructions

Page 8: module2.2-Addressing modes

2. Immediate Addressing• Transfer the source-immediate byte or word or double word of data in to

the destination register.• Eg. MOV CX,22H MOV ECX,1233456H• Immediate implies the data immediately follow the hexadecimal opcode in

the memory• In some assemblers # symbol precedes immediate data• The letter H is appended with hexa decimal data

Page 9: module2.2-Addressing modes

Examples of immediate addressing

Page 10: module2.2-Addressing modes

Programming

• Assemblers

• Intel ASM

• Microsoft MSAM – MACRO assembler

• Borland TASM – Turbo assembler

Page 11: module2.2-Addressing modes

Simple Program

Page 12: module2.2-Addressing modes

Program Explanation• .MODEL Tiny - direct the assembler to assemble the program in to

a single code segment.• .code- start of the code segment• .startup - indicates the starting instruction in the program• .exit- causes the program to exit to dos• END – indicates the end of the program.• Tiny program always assembles as a command (.com) program.• The program places 0000H into the 16 bit registers AX, BX.CX

(immediate addressing), then copy the contents of AX to SI , DI and BP (register addressing).

• Each statement have 4 parts1) label – used to store a symbolic name for memory location2) opcode – holds the instruction3) operand – contains information used by opcode (can have beween 0 and 3 operands)4) comment – comment about the instruction, alwaya begin with a semicolon

• Assembler genetaes a .lst file which includes machine code, address and the program

Page 13: module2.2-Addressing modes

3. Direct Data Addressing• Moves a byte or word or double word between a memory location and

register.• Does not support memory to memory transfer except for MOVS instruction• Eg mov cx, list• Two basic forms

1) Direct addressing – applies to a mov between memory location and AL,AX.EAX2) Displacement addressing – applies almost any instruction.

• The sddress is formed by adding the displacement to the DS or an alternate segment address.

Page 14: module2.2-Addressing modes

• Direct addressing• 3 byte long instruction• Eg MOV AL,DATA• DTA is a symbolic name , actual location is 1234H• The effective address is formed by adding 1234 and

segment address.

Page 15: module2.2-Addressing modes

• Displacement addressing• 4 byte long instruction• In 386 and above it can be upto 7 bytes long.

Page 16: module2.2-Addressing modes

Program

Page 17: module2.2-Addressing modes

Program explanation

• MODEL SMALL – allows one data segment and one code segment

• .DATA –informs data segment begins

• Assembles as .exe file

• .startup – loads data segment register with segment address

Page 18: module2.2-Addressing modes

4. Register Indirect Addressing

Transfer a byte , word or double word between register and memory location addressed by index or base register

The index and base registers are BX,BP,SI,DI

Eg MOV AX,[BX] MOV AL,[ECX]

[] – denote indirect addressing

Page 19: module2.2-Addressing modes

Operation of register indirect addressing

Page 20: module2.2-Addressing modes

Examples

Page 21: module2.2-Addressing modes

• Data segment is used by default with register indirect addressing

• If BP register address memory stack segment is used

• Special assembler directive –BYTE PTR, WORD PTR, DWORD PTR – indicates the size of memory data address by memory pointer ptr.

• Eg. Mov BYTE PTR[DI],10H• Used only with instructions that address memory

location through pointer or index register with immediate data.

Page 22: module2.2-Addressing modes

An array (TABLE) containing 50 bytesthat are indirectly addressed through register BX

Page 23: module2.2-Addressing modes
Page 24: module2.2-Addressing modes

5. Base-Plus-Index Addressing

Transfer a byte , word or double word between register and memory location addressed by base register(BP or BX) plus index register (DI or SI).

Eg. Mov [BX+DI],CLThe base register often holds the beggining

location of the memory array while the index register holds the relative position of the element in the array

Page 25: module2.2-Addressing modes

Locating data with base-plus-index addressing

Page 26: module2.2-Addressing modes

Examples

Page 27: module2.2-Addressing modes

Locating Array data using base plus index addressing

Page 28: module2.2-Addressing modes

Example program

Page 29: module2.2-Addressing modes

6. Register Relative Addressing

Moves a byte , word or double word between register and memory location addressed by an index or base register plus a displacement

• BX, DI, or SI addresses the DS segment and• BP addresses the SS segment• Displacement can be added or subtracted• Displacement can be an offset address.• MOV AX, [BX+1000H]

Page 30: module2.2-Addressing modes

Operation

Page 31: module2.2-Addressing modes

Examples

Page 32: module2.2-Addressing modes

Addressing array data with register relative

Register relative addressing used to address an element of ARRAY.The displacement addresses the start ofARRAY, and DI accesses an element.

Page 33: module2.2-Addressing modes

Program addressing array data using program relative addressing

Page 34: module2.2-Addressing modes

7. Base Relative-Plus-Index Addressing

Transfer a byte , word or double word between register and memory location addressed by a base register(BP or BX) and an index register (DI or SI) plus a displacement.

Eg. MOV AX, [BX+SI+100H]

Page 35: module2.2-Addressing modes

Operation

Page 36: module2.2-Addressing modes

Examples

Page 37: module2.2-Addressing modes

Addressing Arrays with base relative plus indexed

Page 38: module2.2-Addressing modes
Page 39: module2.2-Addressing modes

8. Scaled-Index Addressing

• Uses two 32 bit registers to access memory the second register is multiplied by a scale factor

• The scaling factor can be 1X,2X,4X or 8X.

• Eg. MOV AX,[EDI+2*ECX]

Page 40: module2.2-Addressing modes
Page 41: module2.2-Addressing modes
Page 42: module2.2-Addressing modes

Data structures• Used to specify how information is stored in the memory array.• STRUC assembly directive and ENDS statement is used to define

a structure.• Eg defining a structure

INFO STRUCNAMES DB 32 DUP (?)STREET DB 32 DUP (?)CITY DB 16 DUP (?)INFO ENDSNAME1 INFO <‘bob smith’ , ‘Cheruthuruthy’, thrissur’>The literals are surrounded with apostrophes and the entire field is

surrounded with <> symbol.Use a structure name and field name to select a field from the

structure.Eg. Name1.names

Page 43: module2.2-Addressing modes

Program Memory-Addressing Modes

Used with JMP and CALL instructions

Three distinct forms are

• Direct Program Memory Addressing

• Relative Program Memory Addressing

• Indirect Program Memory Addressing

Page 44: module2.2-Addressing modes

Direct Program Memory Addressing

• The instruction for direct program memory addressing store the address with the opcode

• Used by high level languages, BASIC – GOTO and GOSUB

• Eg . The address is stored following the opcode in the memory

• Inter segment jump means jump to any memory location with in the memory system.

• Direct jump is often called far jump• CALL instruction uses direct program addressing

Page 45: module2.2-Addressing modes

Relative Program Memory Addressing

• Relative means relative to instruction pointer• Eg.

• A one byte (+127 and -128) displacement is used in short jump and 2 byte (+/- 32k) displacement is used for near jumps and calls

• These jumps are considered to be intra segment jumps• Means any where with in the current code segment.• Allows forward memory reference or reverse memory reference• 32 bit (+/- 2G)displacement is also allowed.

Page 46: module2.2-Addressing modes

Indirect Program Memory AddressingUse any 16 bit register, any relative registerIf a 16 bit register is used then the jump is nearEg. JMP BXIf a relative register holds the address it is called indirect-indirect or double indirect jumpEg . Mov BX,4 JMP TABLE[BX]

Page 47: module2.2-Addressing modes
Page 48: module2.2-Addressing modes

Stack Memory-Addressing Modes

• Stack holds data temporarily and stores return addresses for procedures– The stack memory is LIFO memory– Use PUSH instruction to place data onto stack– Use POP instruction to remove data from stack– Call instruction uses stack to hold return address– RET instruction remove the return address from the stack

• The stack memory is maintained by two registers: SP or ESP, and SS

• Always operates on word of data (double word with 386 and above)• When pushed –high order 8 bits placed in sp-1and low order 8 bits

placed in sp-2 location, sp is then decremented by 2.• Stack memory address is calculated by adding sp and (ss*10)• When poped the low order bits are removed form sp and high order

from sp+1 then sp is incremented by 2

Page 49: module2.2-Addressing modes
Page 50: module2.2-Addressing modes

Example Push and pop instructions

Page 51: module2.2-Addressing modes

Example Stack program