You are on page 1of 4

Birla Institute of Technology & Science, Pilani

First Semester 2010-2011


IS C351: COMPUTER ORGANIZATION AND ARCHITECTURE
Lab #3
Topic: Data Addressing Modes

Introduction:
Addressing modes are an aspect of the instruction set architecture in most CPU designs. The
various addressing modes that are defined in a given instruction set architecture define how
machine language instructions in that architecture identify the operand (or operands) of
each instruction. An addressing mode specifies how to calculate the effective memory
address of an operand by using information held in registers and/or constants contained
within a machine instruction.
Various addressing modes supported by Intel 8086 processor are
1. Register addressing mode
2. Immediate addressing mode
3. Direct addressing mode
4. Register indirect addressing mode
5. Base relative addressing mode
6. Indexed relative addressing mode
7. Base indexed relative addressing mode

Segment:Offset Addressing Scheme:-


All the addressing following this will follow the Segment: offset scheme to calculate the
Effective Address (EA).
For given segment:offset pair, effective address is calculated by
EA = (segment value * 16 ) + Offset value
Segment register is multiplied by 16 (or shifted one hexadecimal byte to the left) (or add an
extra 0 to the end of the hex number) and then the value in an offset register is added to it.
Example : F000 : 0FFD
F0000
+ 0FFD
---------
F0FFD or 9,87,133(decimal)
The segment and offset are grouped in the default way, but the programmer can do
segment overriding to use them as they expect
CS : IP -> Next Instruction
SS : SP ,BP ->Stack Pointer, Base Pointer
DS : SI,DI ->Source Index, Destination Index

Segment Override
A segment override prefix allows any segment register (DS, ES, SS, or CS) to be used as
the segment when evaluating addresses in an instruction. Segment override prefix can only
be used for data access. An override is made by appending the register plus a colon to the
beginning of the memory reference of the instruction as in the following examples:
MOV AX, [ES:60000] ; USE ES AS THE SEGMENT
MOV AX, [CS:BX] ; USE CS AS THE SEGMENT
MOV AX, [DS:BP+DI+6] ; USE SS AS THE SEGMENT

1. Register addressing mode


In this mode the source operand, destination operand or both are to be contained in the
registers.
eg:- MOV BX,CX [BX=0000H CX=0010H ]
MOV CL,DL [CX=1234H DX=3456H ]
Note: The second instruction will change only the lower 8 bits of the register CX.
Use debug to see how it is working!!!

1
2. Immediate addressing mode
In this mode an 8 or 16 bit data can be specified as part of the instruction.
eg:- MOV CL,05H
MOV DX,083AH
Note: Use Debug to verify the above result!!!

3. Direct addressing mode


In this mode the 16 bit effective address (EA) is taken directly from the displacement field
of the instruction. This EA or displacement is the distance of the memory location from the
current value in the data segment (DS) register in which the data are stored. The [DS] is
multiplied by 10H and added to the EA to generate the 20 bit physical address.
Ex:- MOV CX, [1234H]
MOV START, BL
START can be defined as an address by using the assembler DB (Define Byte) or DW
(Define Word) directives. Let START = 0030H and [DS] = 3050H
The physical memory address accessed in first instruction MOV CX, [1234H] is
3050H X 10H + 1234H = 30500H + 1234H = 31734H
Therefore the contents of memory locations 31734H and 31735H are copied into register
CX.
For the second instruction MOV START, BL
3050H X 10H + 0030H = 30530H
Here the contents of register BL are copied into memory location 30530H.

4. Register indirect addressing mode


In this mode the EA is specified in either a pointer register or an index register. The pointer
register can be either base register (BX) or base pointer (BP) and Index register can either
be Source index (SI) or Destination index (DI) register.
eg:- MOV [DI],BX

The instruction moves the 16 bit content of BX into a memory location offset by the value of
EA (EA is specified in DI) from the current contents in DS.
If [DS] = 3040H [DI] = 0030H [BX] = 2346H
The 20 bit physical address will be 3040H X 10H + 0030H = 30430H contents of BX (2346)
is moved into memory locations 30430H & 30431H
Note: Using Debug verify the above result!!!

5. Base Relative Addressing mode


In this mode EA is obtained by adding a displacement (signed 8 bit or unsigned 16 bit)
value to the contents of BX or BP. The segment registers used are DS & SS. When memory
is accessed, the 20 bit physical address is computed from BX and DS. On the other hand,
when the stack is accessed, the 20 bit physical address is computed from BP and SS.
eg:- MOV AL, START [BX] or MOV AL, [BX + START]

START = 04H (8 bit displacement), BX = 2000H and [DS] = 1004H


EA = 1004H X 10H + 2000H + 04H = 12044H

Here the source operand is in Base Relative Addressing mode. EA is obtained by adding the
value of START and [BX]. The 20 bit physical address is produced from DS and EA. The 8 bit
content of the memory location 12044H is moved to AL register.

6. Indexed Relative Addressing or Register Relative Addressing mode


In this mode, the EA is calculated by adding the unsigned 16 bit or signed extended 8 bit
displacement and the contents of SI or DI.
eg:- MOV BH, START[SI]
It moves the contents of the 20 bit address computed from the displacement START, SI and
DS into BH register. For 16 bit displacement, the execution unit adds this to SI to determine

2
EA. On the other hand, for 8 bit displacement the execution unit sign extends it to 16 bits
and then adds to SI for determining EA.

7. Base Relative-Plus-Index addressing mode


In this mode, the EA is computed by adding a base register (BX or BP), an index register (SI
or DI), and a displacement (unsigned 16 bit or sign extended 8 bit)
eg:- MOV CL, LIST [BP+SI]
If [BP] = 0200H, [SI] = 1000H, LIST = 08H and [SS] = 2000H
EA = 2000H X 10H + 0200H + 1000H + 08H = 21208H
Based Indexed addressing mode provides a convenient way for a subroutine to address an
array allocated on a stack.

Way to remember addressing modes


There are a total of 17 different legal combination possible the 8086: DISP, [BX], [BP],
[SI], [DI], DISP[BX], DISP[BP], DISP[SI], DISP[DI], [BX][SI], [BX][DI], [BP][SI], [BP][DI],
DISP[BX][SI], DISP [BX][DI], DISP[BP][SI], and DISP[BP][DI].
Easier way to remember is:

DISP BP or BX SI or DI
We can choose either one column or two columns or three columns to land into the
addressing modes and combination we desire.
Task #1:
Write an ALP for the following expressions:
(i) X = 4*Z-15*Y [Assume Z=9 and Y=7]
(ii) X = (Y/Z)*(W+1) (Try your self!!!)
Sol: (i)
.model small
.data
X dw ? ; allocate 2 byte memory to X
Z db 09
Y db 07
.code
.startup
mov ax,4
imul Z ; Integer(signed) multiplication
mov bx,ax
mov ax,-15
imul Y
add ax,bx
mov X,ax
.exit
end

Note: Signed multiplication (imul) of accumulator by "src" with result placed in the
accumulator. If the source operand is a byte value, it is multiplied by AL and the result is
stored in AX. If the source operand is a word value it is multiplied by AX and the result is
stored in DX:AX.

Task: Identify various Addressing Modes used in above program!!!

Task #2:
The following ALP is adding the elements of two vectors and storing in a third and displays
the result on monitor.
.model small
.data
vec1 db 12,3,15,9 ; initialize vec1
vec2 db 3,5,4,1
3
vec3 db ?,?,?,?
vec4 db 19 dup('$')
.code
.startup
lea si, vec1 ; si contains starting address of vec1
lea bx, vec2
lea di, vec3
mov cx, 4
sum: ;adding the elements of vec1 and vec2
mov al, [si]
add al, [bx]
mov [di], al
inc si
inc bx
inc di
loop sum
mov cx,4
lea di,vec3
mov bx,0

lea si,vec4
result: ;storing the ASCII converted result in vec4
mov al,[bx][di]
mov ah,0
aam ; Converts two digit number in corresponding BCD number
; (e.g. MOV AL, 15 ; AL = 0Fh
AAM ; AH = 01, AL = 05 )

add ax,3030h ; Binary to ASCII conversion


mov [si],ah
inc si
mov [si],al
inc si
inc bx
loop result

mov ah,09h ; Displaying $ terminated ASCII string stored in vec4


mov dx,offset vec4
int 21h
.exit
end

Try These:
1. Identify all kinds of data addressing modes used in the Task #2
2. Rewrite the above program without using lea instruction.
3. Write an ALP to copy the elements of vec1 and vec2 into vec3.
4. Write an ALP that copies four word sized contents of the memory locations
starting from CS:DATA1 into AX, BX, CX and DX respectively.
5. Write an ALP to find whether a number is prime (A prime number is evenly
divisible by only itself and 1) or not. Your program should read number as an
input and display the appropriate message as an output. Further extend the
program such that program should continuously ask input number until user
input is not -1. [Do in the lab duration and show to the instructor!!!]
---------------------------------------------------------------------------------------------------

You might also like