Chap 10
Chap 10
Chap 10
1
Outline
• One-Dimensional Arrays
• Addressing Modes
• Two-Dimensional Arrays
• Based Indexed Addressing Mode
2
One-Dimensional Array A
• One-Dimensional Array is an
ordered list of elements, all of
same type
• DB & DW pseudo-ops to
declare byte and word sized
arrays
3
Arrays
W DW 1000,40,29887,329
Address of the array variable is called base address of array
Offset Address Symbolic Address Contents
0300h W 1000d
0302h W +2 40d
0304h W +4 29887d
0306h W +6 329d
4
The DUP Operator
• The DUP (duplicate) is used to define arrays
whose elements share a common initial value.
• repeat_count DUP (value)
• GAMMA DW 100 DUP (0) DUP may be
nested
• DELTA DB 212 DUP (?)
• LINE DB 5, 4, 3 DUP (2, 3 DUP (0), 1)
• LINE DB 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1
5
One-Dimensional Array A
W DW 10, 20, 30, 40, 50, 60
MOV AX, W + 18
XCHG W+48, AX
MOV W+18, AX
Addressing Modes
• The way an operand is specified
• register mode: an operand is a register.
• immediate mode: an operand is a constant.
• direct mode: an operand is a variable.
– INC AX
– MOV AX, 0
– ADD AX, ALPHA
– ADD AX, [1234H]
9
ADDITIONAL ADDRESSING MODES
Four additional addressing modes to address
memory operands indirectly
1. Register Indirect Mode
2. Based
3. Indexed
4. Based Indexed
Register Indirect Mode
Offset address of the operand is contained in a
register. Register acts like a pointer to the
memory location
• [register]
• The register is BX, SI, DI, or BP.
12
Suppose that
BX contains 1000h Offset 1000h contains 1BACh
SI contains 2000h Offset 2000h contains 20FEh
DI contains 3000h Offset 3000h contains 031Dh
where the above offsets are in the data segment addressed by DS.
16
Based and Indexed Addressing Mode
• [register + displacement]
• [displacement + register]
• [register] + displacement
• displacement + [register]
• displacement[register]
• The register is SI, DI, BX, or BP.
indexed based
17
Based and Indexed Addressing Mode
18
Rework the last example by using based
mode.
XOR AX, AX ; AX holds sum
XOR BX, BX ; clear base register
MOV CX, 10 ; CX has number of
elements
ADDNOS:
ADD AX, W[BX] ; sum = sum + element
ADD BX, 2 ; index next element
LOOP ADDNOS ; loop until done
19
Suppose that ALPHA is declared as
26
How Two-Dimensional Array are stored
28
Column-Major Order
B DW 10, 50, 90
DW 20, 60, 100
DW 30, 70, 110
DW 40, 80, 120
29
Locating an element in an array
• Consider M xN array stored in row major order
• Size of element is S
• To find location of A[i,j]
• Find where the row i begins
• Location of jth element in that row
– Row 1 begins at A
– Row 2 begins at A + N x S
– Row 3 begins at A + 2 x N x S
– Row i begins at A + (i-1) x N x S
33
Based Indexed Addressing Mode
• W is a word variable
• BX contains 2
• Si contains 4
MOV AX, W[BX][SI]
Will move contents of W + 6 to AX
MOV AX, [W + BX + SI]
MOV AX, W[BX + SI]
34
• M is a 5x7 word array stored in row major
order, write some code to;
– Clear row 3
– Clear column 4
1. Clear row 3
For an MxN array; Row i begins at A + (I-1) x N x S
Thus in a 5x7 array, row 3 begins at; A + (3-1) x7x2 = A + 28;
MOV BX, 28
XOR SI, SI
MOV CX, 7
CLEAR:
MOV A[BX][SI], 0
ADD SI,2
LOOP CLEAR
• M is a 5x7 word array stored in row major
order, write some code to;
– Clear row 3
– Clear column 4
2. Clear column 4
For an MxN array; column j begins at A + (j-1) S
Column 4 begins at A + (4-1) x 2 = A + 6;
Since A is a 7 column array stored in row major order, to get to the next element in
column 4 we need to add 7x 2 = 14 ;
MOV SI, 6
XOR BX, BX
MOV CX, 5
CLEAR:
MOV A[BX][SI], 0
ADD BX,14
LOOP CLEAR
An application: Average Test Scores
sum[j] = 0
i=1
FOR 5 times DO
sum[j] = sum[j] + score[i, j]
i=i+1
END_FOR
37