UMBC CMSC 211

CSEE | 211 | Current | lectures | news | help


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. To locate a specific element in the array, we take the size of the element and multiple it by its index number. The first element 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.

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 wemust get to the correct byte.
  

Variable subscripts

We can use a variable to hold the subscript for us as well. We can use three registers for this: BX, DI, SI.

DI is used to hold the destination index, SI holds the source index and BX is used as a general purpose index register.

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 
  

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 | 211 | Current | lectures | news | help