Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization & Assembly Language Lecture 18 Addressing Modes Study from Chapter 10 (10.1,10.2.1, 10.2.2, example 10.4 not included) Introduction Addressing Modes Register Indirect Mode Based and Indexed Addressing Modes Addressing Modes 2 The way an operand is specified is known as its addressing mode. Addressing modes: Register mode Immediate mode Direct mode Register Indirect Based Indexed Based Indexed An operand is a register (ex. INC AX) An operand is a constant (ex. ADD A, 5) An operand is a variable(ex. ADD A, 5) Address memory operands indirectly Used with two dimensional arrays Used with one dimensional arrays Addressing Modes 3 The offset address of the operand is contained in a register. I.e. The register acts as a pointer to the memory location. Format: [register] The register must be one of the following: BX SI The operand’s segment number is contained in DS DI BP The operand’s segment number is contained in SS Addressing Modes 4 Ex. suppose that SI contains 0100h, and the word at 0100h contains 1234h. MOV AX, [SI] The CPU: 1. Examines SI and obtains the offset address 100h. 2. Uses the address DS:0100h to obtain the value 1234h. 3. Moves 1234h to AX. MOV AX, SI The CPU will move the value of SI, namely 100h, into AX. Addressing Modes 5 • Ex. Write some code to sum in AX the elements of the 10-element array W defined by W DW 10,20,30,40,50,60,70,80,90,100 MOV AX, 0 LEA SI, W MOV CX, 10 ADDNOS: ADD AX, [SI] ADD SI, 2 LOOP ADDNOS ; AX holds sum ; SI points to array W ; CX has number of elements ; sum = sum + element ; move pointer to the next element ; loop until done Addressing Modes 6 The operand’s offset address is obtained by adding a number called a displacement to the contents of a register. Displacement may be: The offset address of a variable. (ex. A) A constant (positive or negative). (ex. -2) The offset address of a variable + or - a constant. (ex. A + 4) Syntax: [register + displacement] [displacement + register] [register] + displacement displacement + [register] displacement [register] Addressing Modes 7 The register must be one of the following: BX The operand’s segment number is contained in DS SI DI The operand’s segment number is contained in SS BP The addressing mode is called based if BX (base register) or BP (base pointer) is used. The addressing mode is called indexed if SI (source index) or DI (destination index) is used. Addressing Modes 8 Ex. Suppose W is a word array, and BX contains 4 MOV AX, W[BX] The displacement is the offset address of variable W The instruction moves the element at address W + 4 to AX. (this is the third element in the array) The instuction could also have been written in any of these forms: MOV AX, [W+BX] MOV AX, [BX+W] MOV AX, W+[BX] MOV AX, [BX]+W Addressing Modes 9 Ex. suppose SI contains the address of a word array W MOV AX, [SI+2] The displacement is 2. The instruction moves the contents of W + 2 to AX. (this is the second element in the array) The instruction could also have been written in any of these forms: MOV AX, [2+SI] MOV AX, 2+[SI] MOV AX, [SI]+2 MOV AX, 2[SI] Addressing Modes 10 • Ex. Write some code to sum in AX the elements of the 10-element array W defined by W DW 10,20,30,40,50,60,70,80,90,100 XOR XOR MOV ADDNOS: ADD ADD LOOP AX, AX BX, BX CX, 10 ; AX holds sum. Can also use MOV AX,0 ; clear base register ; CX has number of elements AX, W[BX] BX, 2 ADDNOS ; sum = sum + element ; index next element ; loop until done Addressing Modes 11