| UMBC CMSC 211 |
|   | ||||||
The starting point is given a label as the name of the array. Like in C, indexing into the array is 0-origin indexing, which means the first element is the 0th element. (Actually, C does it the way assembly language does it, because because so much of C is directly related to the assembly language implementation of C's features.) To locate a specific element in the array, we take the size of the element and multiple it by its index number. The second element (with the subscript of 1) is 1 times the sizeOf( element ) plus the starting address or:
N EQU 50 ; array holds fifty items S EQU 4 ; each item is four bytes long Ary DB N DUP ( S DUP (?) ) ; in C, this is int Ary[50];
Higher-dimensional arrays can be considered to be arrays of arrays.
MOV [counts + 5], 27 ; Remember it is zero-based!
MOV [bigcounts + 2 * 5], 27 ; Remember it is zero-based,
; and we must get to the correct byte.
BX and BP are the base registers. BP is called the base pointer and is used only in stack operations. Use BP in the stack only, NEVER for general-purpose indexing! We will study BP in Chapter 13.
DI is used to hold the destination index, SI holds the source index and BX is used as a general purpose index register. SI and DI are used in string instructions as source and destination, but other instructions can use them as general indexing registers. Additionally, the can be used as general registers, but only as 16-bit registers!
MOV BX, 5 ; the index
MOV [counts + BX], 27 ; This will allow us to use loops
; and simply update the contents of
; BX each time
struct myStruct
{
short IDnum; /* This will be a 16-bit value */
short renumb;
short count;
char Dealer[14];
} A;
Also assume that there is an EQU for COUNTOFFSET that shows how far into the row count is located. A[5].count could result in the instruction:
.data
;; Only am showing one row here!
IDnum DW ?
renumb DW ?
count DW ?
Dealer DB 14 DUP (?)
...
IDNUMOFFSET EQU 0
RENUMBOFFSET EQU 2
COUNTOFFSET EQU 4
DEALEROFFSET EQU 6
...
.code
...
MOV AL, 5
MOV CL, 20
MUL CL
MOV BX, AX
MOV AX, [A + COUNTOFFSET + BX]
...
MOV BX, 0
MOV CX, 10
more: MOV [ counts + BX ], 0
INC BX
LOOP more
MOV DI, 0
MOV CX, 10
more: MOV [ counts + DI ], 0
ADD DI, 2 ; Remember to byte adjust!
LOOP more
MOV DI, OFFSET counts
MOV CX, 10
more: MOV [ DI ], 0
ADD DI, 2 ; Remember to byte adjust!
LOOP more
In based indexing, there are two important rules: