UMBC CMSC 211

UMBC | CSEE


Arrays

Arrays in General

A one-dimensional array is a collection of entries, each of the same length and placed one after the other in computer memory with no gaps. An array of n entries, of s bytes is is n times s bytes of contiguous memory.

| <---- s bytes per entry ----> |
                                  
                                  
                                  
                                  
                                  
                                  
                                   
                                  
                                  

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:

array start address + (1 * sizeOf ( element ) ) = element address An example would be if the array's starting address is 0EF00h and the size of each element is 20 bytes, the 5th element (index of 4) is 4 * 20 = 8010 or 5016. 0EF00h + 50h = 0EF50h To declare the array (uninitialized), we would put into our program:
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];
  
Make sure that you don't try to refer the 4th item as Ary[4] (A legal form for addressing an array) because that would really refer to the 1st element (it is four bytes long!) You would have to refer to Ary[16]. The other suggest that you not use this form for addressing and instead use [ Ary + 16 ] as an aid to help you remember the difference in addressing.

Higher-dimensional arrays can be considered to be arrays of arrays.

Using Arrays

Example

If we have an byte array named counts, and wish to set the 6th element to 27, we would write:
        MOV   	[counts + 5], 27     ; Remember it is zero-based!
  

Another Example

If we have a word array named bigcounts, and wish to set the 6th element to 27, we would write:
        MOV   	[bigcounts + 2 * 5], 27     ; Remember it is zero-based, 
                                            ; and we must get to the correct byte.
  

Variable subscripts

We can use a variable to hold the subscript for us as well. We can use four (one is restricted usage, though) registers for this: BX, DI, SI, and BP.

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!

Indexing Example

If we have an byte array named counts, and wish to set the 6th element to 27, we would write:
        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 
  
Another example of indexing is when you have an array of structures in C. Assume that each row in the array takes 20 bytes as in the following declaration.

    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]
	...
  

Sample of Indexing Using a Byte Array

        MOV     BX, 0
        MOV     CX, 10
more:   MOV     [ counts + BX ], 0
        INC     BX
        LOOP    more
  
This segment will allow me to initialize the ten counts to zero.

Sample of Indexing Using a Word Array (Subscript method)

        MOV     DI, 0
        MOV     CX, 10
more:   MOV     [ counts + DI ], 0
        ADD     DI, 2               ; Remember to byte adjust!
        LOOP    more
  

Sample of Indexing Using a Word Array (Pointer method)

        MOV     DI, OFFSET counts
        MOV     CX, 10
more:   MOV     [ DI ], 0
        ADD     DI, 2                 ; Remember to byte adjust!
        LOOP    more
  

Comparision

What's the difference? MOV [ counts + DI ], 0 instead of MOV [ DI ], 0 Hopefully, you will intuitively see that the time to compute counts + DI adds extra time to the loop. That is why C/C++ use pointers instead of indices.

One more way!

Additionally, we can have: address-expression + { BX | BP } + { SI | DI } ] where the notation "{ ... | ... }" means zero or one of these items. This notation is called based indexing, or double indexing.

In based indexing, there are two important rules:

Array Exercise


CSEE |