Microprocessors Chapter 3 Addressing Modes prepared by Dr. Mohamed A. Shohla Chapter Overview • • • • • • • جام ع ة ال م نوف ي ة Register Addressing Immediate Addressing Direct Data Addressing Register Indirect Addressing Base-Plus-lndex Addressing Register Relative Addressing Base Relative-Plus-lndex Addressing Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3-2 Data-Addressing Modes • Register addressing • Transfers a copy of a byte or word from the source register or memory location to the destination register or memory location. • Immediate addressing Direct addressing • • Register indirect addressing • Transfers the source-immediate byte or word of data into the destination register or memory location. Moves a byte or word between a memory location and a register. The instruction set does not support a memory-to-memory transfer, except for the MOVS instruction. Transfers a byte or word between a register and a memory location addressed by an index or base register. The index and base registers are BP, BX, DI, and SI. • Base-plus-index addressing • 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). • Register relative addressing • Moves a byte or word between a register and the memory location addressed by an index or base register plus a displacement. • Base relative-plusindex addressing • Transfers a byte or word between a register and the memory location addressed by a base and an index register plus a displacement. • Scaled-index addressing • Is available only in the 80386 through the Pentium 4 microprocessor. The second register of a pair of registers is modified by the scale factor of 2X, 4X, or 8X to generate the operand memory address. • جام ع ة ال م نوف ي ة • Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3-3 The MOV instruction MOV AX , BX Destination جام ع ة ال م نوف ي ة Source Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3-4 Register Addressing • Register addressing is the most common form of data addressing and, once the register names are learned, is the easiest to apply. • Examples of the register-addressed instructions. Assembly Language MOV MOV MOV MOV MOV MOV MOV MOV MOV جام ع ة ال م نوف ي ة AL, BL CH, CL AX, CX SP, BP BX, ES ECX, EBX ESP, EDX ES, DS BL, DX Size 8-bits 8-bits 16-bits 16-bits 16-bits 32-bits 32-bits — — Operation Copies BL into AL Copies CL into CH Copies CX into AX Copies BP into SP Copies ES into BX Copies EBX into ECX Copies EDX into ESP Not allowed (segment-to-segment) Not allowed (mixed sizes) Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3-5 Register Addressing Example : MOV BX, CX The effect of executing the MOV BX, CX instruction at the point just before the BX register changes. Note that only the rightmost 16 bits of register EBX change. جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3-6 Immediate Addressing • The term immediate implies that the data immediately follow the hexadecimal opcode in the memory. Also note that immediate data are constant data, while the data transferred from a register are variable data. • Examples of the immediate-addressed instructions. A s s e m b ly L a n g u a g e MOV MOV MOV MOV MOV MOV MOV MOV MOV جام ع ة ال م نوف ي ة BL, 44 AX, 44H CH, 100 A L , 'A ' A X , 'A B ' CL, 11001110B EBX, 12340000H E S I, 1 2 EAX, 100B S ize 8 -b its 1 6 -b its 8 -b its 8 -b its 1 6 -b its 8 -b its 3 2 -b its 3 2 -b its 3 2 -b its O p e ra tio n C o p ie s C o p ie s C o p ie s C o p ie s C o p ie s C o p ie s C o p ie s C o p ie s C o p ie s a 4 4 d e cim a l (2 C H ) in to B L a 0 0 4 4 H in to A X a 1 0 0 d e cim a l (6 4 H ) in to C H a n A S C II A in to A L a n A S C II B A in to A X a 1 1 0 0 1 1 1 0 b in a ry in to C L a 1 2 3 4 0 0 0 0 H in to E B X a 1 2 d e cim a l in to E S I a 1 0 0 b in a ry in to E A X Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3-7 Immediate Addressing Example: MOV EAX, 13456H The operation of the MOV EAX,13456H instruction. This instruction copies the immediate data (13456H) into EAX. جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3-8 Direct Data Addressing • Direct addressing with a MOV instruction transfers data between a memory location, located within the data segment, and the AL (8-bit), AX (16-bit), or EAX (32-bit) register. • Examples of the Direct addressed instructions. Assembly Language جام ع ة ال م نوف ي ة Size MOV CH, DOG 8-bits MOV CH, [1000H] 8-bits MOV ES, DATA6 16-bits MOV DATA7, BP 16-bits MOV NUMBER, SP 16-bits MOVDATA1, EAX 32-bits MOV EDI, SUM1 32-bits Operation Copies the byte contents of data segment memory location DOG into CH Copies the byte contents of data segment offset address 1000HintoCH Copies the word contents of data segment memory location DATA6 into ES Copies BP into data segment memory location DATA7 Copies SP into data segment memory location NUMBER Copies EAX into data segment memory location DATA1 Copies the doubleword contents of data segment memory location SUM1 into EDI Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3-9 Direct Addressing Example : MOV AL, [1234H] The operation of the MOV AL,[1234H] when DS = 1000H. جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 10 Example DATA1 DATA2 DATA3 DATA4 جام ع ة ال م نوف ي ة .MODEL SMALL .DATA DB 10H DB 0 DW 0 DW 0AAAAH ; select SMALL model ; indicate start of DATA segment ; place 10H in DATA1 ; place 0 in DATA2 ; place 0 in DATA3 ; place AAAAH in DATA4 .CODE .STARTUP MOV AL, DATA1 MOV AH, DATA2 MOV DATA3, AX MOV BX, DATA4 ; indicate start of CODE segment ; indicate start of program ; copy DATA1 to AL ; copy DATA2 to AH ; save AX at DATA3 ; load BX with DATA4 .EXIT END ; exit to DOS ; end file Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 11 Register Indirect Addressing • • جام ع ة ال م نوف ي ة Register indirect addressing allows data to be addressed at any memory location through an offset address held in any of the following registers: BP, BX, DI, and SI. Examples of the Register indirect addressed instructions. Assembly Language MOV CX, [BX] Size Operation 16-bits MOV [BP], DL 8-bits MOV [DI], BH 8-bits MOV [DI], [BX] — MOV AL, [EDX] 8-bits MOV ECX, [EBX] 32-bits Copies the word contents of the data segment memory location address by BX into CX Copies DL into the stack segment memory location addressed by BP Copies BH into the data segment memory location addressed by Dl Memory-to-memory moves are not allowed except with string instructions Copies the byte contents of the data segment memory location addressed by EDX into AL Copies the doubleword contents of the data segment memory location addressed by EBX into ECX Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 12 Register indirect Addressing Example : MOV AX, [BX] The operation of the MOV AX,[BX] instruction when BX = 1000H and DS = 0100H. Note that this instruction is shown after the contents of memory are transferred to AX. جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 13 Base-Plus-lndex Addressing • Base-plus-index addressing is similar to indirect addressing because it indirectly addresses memory data. In the 8086 through the 80286, this type of addressing uses one base register (BP or BX), and one index register (DI or SI) to indirectly address memory. • • Examples of the Base-Plus-Index addressed instructions. A s s e m b ly L a n g u a g e جام ع ة ال م نوف ي ة S iz e M O V C X ,[B X + D I] 1 6 -b its M O V C H ,[B P + S I] 8 -b its M O V [B X + S I],S P 1 6 -b its M O V [B P + D I],A H 8 -b its O p e ra tio n C o p ie s th e w o rd c o n te n ts o f th e d a ta s e g m e n t m e m o ry lo c a tio n a d d re s s b y B X p lu s D l in to C X C o p ie s th e b yte c o n te n ts o f th e s ta c k s e g m e n t m e m o ry lo c a tio n a d d re s s e d b y B P p lu s S I in to C H C o p ie s S P in to th e d a ta s e g m e n t m e m o ry lo c a tio n a d d re s s e s b y B X p lu s S I C o p ie s A H in to th e s ta c k s e g m e n t m e m o ry lo c a tio n a d d re s s e d b y B P p lu s D l Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 14 Base-Plus-lndex Addressing Example : MOV DX, [BX + DI] An example showing how the base-plus-index addressing mode functions for the MOV DX,[BX+DI] instruction. Notice that memory address 02010H is accessed because DS = 0100H, BX = 100H, and DI = 0010H. جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 15 Example .MODEL SMALL .DATA ARRAY DB 16 DUP (?) DB 29H DB 30 DUP (?) .CODE .STARTUP MOV BX, OFFSET ARRAY MOV DI, 10H MOV AL, [BX+DI] MOV DI, 20H MOV [BX+DI], AL .EXIT END جام ع ة ال م نوف ي ة ; select SMALL model ; start of DATA segment ; setup ARRAY ; sample data at element 10H ; start of CODE segment ; start of program ; address ARRAY ; address element 10H ; get element 10H ; address element 20H ; save in element 20H ; exit to DOS ; end of file Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 16 Register Relative Addressing • • Register relative addressing is similar to base-plus-index addressing and displacement addressing. In register relative addressing, 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, Dl, or SI). Examples of the register Relative addressed instructions. A s s e m b ly L a n g u a g e S iz e O p e ra tio n M O V A X , [D I+ 1 0 0 H ] 1 6 -b its M O V A R R A Y [S I], B L 8 -b its M O V L IS T [S I+ 2 ], C L 8 -b its M O V D I,S E T _ IT [B X ] 1 6 -b its M O V A R R A Y [E B X ],E A X 3 2 -b its C o p ie s th e w o rd c o n te n ts o f th e d a ta s e g m e n t m e m o ry lo c a tio n a d d re s s e d b y D l p lu s 1 0 0 H in to A X C o p ie s B L in to th e d a ta s e g m e n t m e m o ry lo c a tio n a d d re s s e d b y A R R A Y p lu s S I C o p ie s C L in to th e d a ta s e g m e n t m e m o ry lo c a tio n a d d re s s e d b y s u m o f L IS T , S I, a n d 2 C o p ie s th e w o rd c o n te n ts o f th e d a ta s e g m e n t m e m o ry lo c a tio n a d d re s s e d b y th e s u m o f S E T _ IT a n d B X in to D l M o v e s E A X in to th e d a ta s e g m e n t m e m o ry lo c a tio n a d d re s s e d b y th e s u m o f A R R A Y a n d EBX جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 17 Register Relative Addressing MOV AX, [BX + 1000H] The operation of the MOV AX,[BX+1000H] instruction, when BX = 0100H and DS = 0200H. جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 18 Register relative addressing used to address an element of ARRAY. The displacement addresses the start of ARRAY, and Dl accesses an element. جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 19 Example .MODEL SMALL .DATA ARRAY DB 16 DUP (?) DB 29H DB 30 DUP (?) .CODE .STARTUP MOV DI, 10H MOV AL, ARRAY[DI] MOV DI, 20H MOV ARRAY[DI], AL .EXIT END جام ع ة ال م نوف ي ة ; select SMALL model ; start of DATA segment ; setup ARRAY 00 ] ; sample data at element 10H ; start of CODE segment ; start of program ; address element 10H ; get element 10H ; address element 20H ; save in element 20H ; exit to DOS ; end of file Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 20 Base Relative-Plus-lndex Addressing Assembly Language Size Operation MOV DH, [BX+DI+20H] 8-bits Copies the byte contents of the data segment memory location addressed by the sum of BX, Dl, and 20H into DH. MOV AX, FILE[BX+DI] 16-bits Copies the word contents of the data segment memory location addressed by the sum of FILE, BX, and Dl into AX. MOV LIST[BP+DI], CL 8-bits Copies CL into the stack segment memory location addressed by the sum of LIST, BP, and Dl جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 21 Base Relative-Plus-lndex Addressing MOV AX, [BX+SI+100H] • An example of base relative-plus-index addressing using a MOV AX, [BX+SI+100H] instruction. • Note: DS = 1000H. جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 22 Chapter 3 Questions and Answers جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 23 What do the following MOV instructions accomplish? (a) MOV AX, BX (b) MOV BX, AX (c) MOV BL, CH (d) MOV ESP, EBP (e) MOV AX, CS Instruction Accomplish (a) MOV AX, BX Copy the contents of register BX into register AX (b) MOV BX, AX Copy the contents of register AX into register BX (c) MOVBL, CH Copy the contents of register CH into register BL (d) MOV ESP, EBP Copy the contents of register EBP into register ESP (e) MOV AX, CS Copy the contents of register CS into register AX جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 24 List the 8-bit , 16-bit, and 32-bit registers that are used for register addressing. Registers جام ع ة ال م نوف ي ة 8-bit AH, AL, BH, BL, CH, CL, DH, DL 16-bit AX, BX,CX, DX, SP, BP, DI, SI 32-bit EAX, EBX,ECX, EDX, ESP, EBP, EDI, ESI Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 25 Select an instruction for each of the following tasks: (a) copy EBX into EDX (b) copy BL into CL (c) copy SI into BX (d) copy DS into AX (e) copy AL into AH Task Instruction (a) copy EBX into EDX MOV EDX, EBX (b) copy BL into CL MOV CL, BL (c) copy SI into BX MOV BX, SI (d) copy DS into AX MOV AX, DS (e) copy AL into AH MOV AH, AL جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 26 Select an instruction for each of the following tasks: (a) move a 12H into AL (b) move a 123AH into AX (c) move a CDH into CL (d) move a 1000H into SI (e) move a 1200A2H into EBX Task Instruction (a) move a 12H into AL MOV AL, 12H (b) move a 123AH into AX MOV AX, 123H (c) move a CDH into CL MOV CL, CDH (d) move a 1000H into SI MOV SI, 1000H (e) move a 1200A2H into EBX MOV EBX, 1200A2H جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 27 Suppose that DS = 0200H, BX= 0300H, and DI = 400H. Determine the memory address accessed by each of the following instructions, assuming real mode operation: (a) MOV AL, [1234H] (b) MOV EAX, [BX] (c) MOV [DI], AL (a) Starting address of the segment (DS) 2000H + displacement (1234H) + 1234H Memory location address (b) Starting address of the segment (DS) + displacement (BX) + Memory location address (c) Starting address of the segment (DS) + displacement (DI) + Memory location address جام ع ة ال م نوف ي ة 3234H 2000H 0300H 2300H 2000H 0400H 2400H Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 28 Choose an instruction that requires BYTE PTR, WORD PTR, DWORD PTR. BYTE PTR MOV BYTE PTR [DI], 33 WORD PTR MOV WORD PTR [DI], 33 DWORD PTR MOV DWORD PTR [DI], 33 جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 29 Suppose that DS = 1000H, SS = 2000H, BP = 1000H, and DI = 0100H. Determine the memory address accessed by each of the following instructions, assuming real mode operation: (a) MOV AL, [BP+DI] (b) MOV CX, [DI] (c) MOV EDX, [BP] (a) Starting address of the segment (SS) + Base (BP) + Index (DI) Memory location address 20000H + 1000H + 0100H 21100H (b) Starting address of the segment (DS) + displacement (DI) 10000H + 0100H Memory location address (c) Starting address of the segment (SS) + Base (BP) Memory location address جام ع ة ال م نوف ي ة 10100H 20000H + 1000H 21000H Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 30 Suppose that DS = 1200H, BX = 0100H, and SI = 0250H. Determine the address accessed by each of the following instructions, assuming real mode operation: (a) MOV [100H], DL (b) MOV [SI+100H], EAX (c) MOV DL, [BX+100H] (a) Starting address of the segment (DS) + displacement (100H) Memory location address 12000H + 100H 12100H (b) Starting address of the segment (DS) + register SI (0250H) + displacement (100H) Memory location address 12000H + 0250H + 100H 12350H (c) Starting address of the segment (SS) + register BX (0100H) + displacement (100H) Memory location address 20000H + 0100H + 0100H 20200H جام ع ة ال م نوف ي ة Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 31 Suppose that DS = 1100H, BX = 0200H, LIST = 0250H, and SI = 0500H, determine the address accessed by each of the following instructions, assuming real mode operation: (a) MOV LIST[SI], EDX (b) MOV CL, LIST[BX+SI] (c) MOV CH, [BX+SI] (a) Starting address of the segment (DS) + address of List (0250H) + register SI (0500H) Memory location address (b) Starting address of the segment (DS) + address of List (0250H) + register BX (0200H) + register SI (0500H) Memory location address (c) Starting address of the segment (DS) + register BX (0200H) + register SI (0500H) Memory location address جام ع ة ال م نوف ي ة + + 11000H 0250H 0500H 11750H + + + 11000H 0250H 0200H 0500H 11950H + + 11000H 0200H 0500H 11700H Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 32 Suppose that DS = 1300H, SS = 1400H, BP = 1500H, and SI = 0100H. Determine the address accessed by each of the following instructions, assuming real mode operation: (a) MOV EAX, [BP+200H] (b) MOV AL, [BP+SI-200H] جام ع ة ال م نوف ي ة (a) Starting address of the segment (SS) + register BP (1500H) + displacement (200H) Memory location address 14000H + 1500H + 200H 15700H (b) Starting address of the segment (SS) + register BP (1500H) + register SI (0100H) - displacement (200H) Memory location address 14000H + 1500H + 0100H 200H 15400H Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 33 Suppose that DS = 1300H, SS = 1400H, BP = 1500H, and SI = 0100H. Determine the address accessed by each of the following instructions, assuming real mode operation: (a) MOV EAX, [BP+200H] (b) MOV AL, [BP+SI-200H] جام ع ة ال م نوف ي ة (a) Starting address of the segment (SS) + register BP (1500H) + displacement (200H) Memory location address 14000H + 1500H + 200H 15700H (b) Starting address of the segment (SS) + register BP (1500H) + register SI (0100H) - displacement (200H) Memory location address 14000H + 1500H + 0100H 200H 15400H Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 3 - 34