Sei sulla pagina 1di 15

CHAPTER 10

Maitraye Das
Lecturer, UIU

One Dimensional Array


A one dimensional array is an ordered list of elements.
Example:
1. A 5 character string named MSG
MSG
DB abcde
2. A word array W of 6 integers initialized to 10, 20,
30, 40, 50, 60
W DW 10, 20, 30, 40, 50, 60

One Dimensional Array


The address of the array variable is called the base address of the array. For the
2nd example, the array is like this:

DUP Operator
repeat_count DUP (value)
Example:
1. An array of 100 words, each initialized to 0
GAMMA
DW 100 DUP (0)
2. An array of 212 uninitialized bytes.
DELTA
DB 212 DUP (?)
3. Nested DUP operator
LINE
DB 5, 4, 3 DUP (2, 3 DUP (0), 1) is equivalent to
LINE
DB 5, 4, 2, 0, 0, 0, 1, 2, 0, 0, 0, 1, 2, 0, 0, 0, 1

Location of Array Elements


A is an array of N elements.
S = the number of bytes in an element of the array, A
S = 1 (for byte array)
= 2 (for word array)
So, the location of the elements of A can be determined as

Exchange Array Elements


Example 10.1 Exchange the 10th and 25th elements in a word array W.
Solution: W[10] is located at address W + 9 x 2 = W + 18 and W[25] is
at W + 24 x 2 =W + 48; so we can do the exchange as follows:
MOV AX, W + 18
XCHG.W + 48, AX
MOV W+18, AX

; AX has W[10]
; AX has W[25]
;complete exchange

Addressing Modes
So far we have used
1. Register mode
2. Immediate mode
3. Direct mode
Example:
4. MOV AX, 0
5. ADD X, AX

operand is a register
operand is constant
operand is a variable

; Destination AX is register mode, source 0 is immediate mode


; Destination X is direct mode, source AX is register mode

There are four additional addressing modes


6. Register indirect mode
7. Based mode
8. Indexed mode
9. Based indexed mode

Register Indirect Mode


The offset address of the operand is contained in a register.
Operand format
[register]
register can be BX, SI, DI, or BP.
If BX, SI, or DI contains the offset of the operand,
DS contains the segment number
If BP contains the offset of the operand,
SS contains the segment number

Register Indirect Mode


Example 10.2 Suppose that
BX contains 1000h
Offset 100h contains 1BACh
SI contains 2000hOffset 2000h contains 20FEh
DI contains 3000h
Offset 3000h contains 031Dh
where the above offsets are in the data segment addressed by DS.
Tell which of the following instructions are legal. If legal, give the source offset address
and the result or number moved.
Instruction
Legal
1. MOV BX, [BX]
2. MOV CX, [SI]
Y
3. MOV BX, [AX]
4. ADD [SI], [DI]
5. INC [DI]
Y

Source offset result/number moved


Y
1000h
1BACh
2000h
20FEh
N, illegal source register
N, illegal memory-memory addition
3000h
031Eh

Based Mode
Operand format
[register + displacement]
[displacement + register]
[register] + displacement
displacement + [register]
displacement [register]
register can be BX (base register), or BP (base pointer)
displacement can be
the offset address or a variable (e.g., A)
a constant (positive or negative) (e.g., -2)
the offset address or a variable plus or minus a constant (A + 2)
If BX is used as register, DS contains the segment number
If BP is used as register, SS contains the segment number

Based Mode
Example: Suppose W is a word array and BX contains 4. displacement is the
offset address of variable W. So, to move the 3rd element of the array to AX, we
can write
MOV AX, [BX + W]
MOV AX, [W + BX]
MOV AX, [BX] + W
MOV AX, W + [BX]
MOV AX, W [BX]

;[register + displacement]
;[displacement + register]
;[register] + displacement
;displacement + [register]
;displacement [register]

Indexed Mode
Operand format
[register + displacement]
[displacement + register]
[register] + displacement
displacement + [register]
displacement [register]
register can be SI (source index), or DI (destination index)
displacement can be
the offset address or a variable (e.g., A)
a constant (positive or negative) (e.g., -2)
the offset address or a variable plus or minus a constant (A + 2)
If SI, or DI is used as register, DS contains the segment number

Indexed Mode
Example: Suppose SI contains the address of a word array W. displacement is
the constant 2. So, to move the 2nd element of the array to AX, we can write
MOV AX, [SI + 2]
MOV AX, [2 + SI]
MOV AX, [SI] + 2
MOV AX, 2 + [SI]
MOV AX, 2 [SI]

;[register + displacement]
;[displacement + register]
;[register] + displacement
;displacement + [register]
;displacement [register]

Based Mode and Indexed Mode


Example 10.6 Suppose that ALPHA is declared as
ALPHA
DW 0123h,0456h,0789h,0ABCDh
in the segment addressed by DS. Also, suppose that
BX contains 2
Offset 0002 contains 1084h
SI contains 4 Offset 0004 contains 2BACh
DI contains 1
Tell which of the following instructions are legal. If legal, give the source offset address
and the result or number moved.
Instruction
Legal
Source offset result/number moved
1. MOV AX, [ALPHA + BX] Y ALPHA + 2
0456h
2. MOV BX, [BX+2]
Y 2+2
2BACh
3. MOV CX, ALPHA [SI]
Y ALPHA+ 4
0789h
4. MOV AX, -2 [SI]
Y -2+4 = 2
1084h
5. MOV BX, [ALPHA+3+DI] Y ALPHA+3+1 = ALPHA+4 0789h
6. MOV AX, [BX] 2
N, illegal form of source operand
7. ADD BX,[ALPHA+AX]
N, illegal source register

Based Mode and Indexed Mode