How to Create and Iterate Through an Array in x86 Assembly Language

x86 Assembly programming language


In this article, we show how to create and iterate through an array in x86 assembly language.

An array is a data element in assembly language that can hold several elements of the same data type.

This data type may be a byte or a word in the 8086 processor being that it is a 16-bit processor. If it were a 32-bit, it could hold a double word. If it were a 64-bit processor, it could hold a quad word.

But when referring to the 8086 processor, it can either be a byte or a word.

Below we show how to create an array of bytes and iterate through the array.

We will then show how to do the same with an array of words.

This is shown in the code below.



The first thing we do is create our array, which we call array1.

Any data element is always declared within the data segment (.data).

This array holds 3 elements of type db, which stands for a data byte.

A data byte is a single byte, which is composed of 8 bits.

The 3 elements are 0xAB, 0xCD, and 0xEF.

We then have our code segment (.code).

The first line we have is, mov si, OFFSET array1

This line moves array1 into the si register, which is the source register, which is the register used in the 8086 processor to process strings and arrays.

The si register inputs in the array and then we can process each element one at a time by incrementing the si register to point to the next element once we are done with the preceding one.

The keyword OFFSET allows the data segment of our code to point to the beginning of the array, array1. The data segment may not begin where array1 begins. Therefore, we use OFFSET, so that there is offset from the start of the data segment to the point of where the address of array1 starts in the data segment.

What we're really doing then is moving the addresses of the array1 element into the si register.

We then move the data of the si register into the al register.

Remember that we created an array of bytes. So we're moving in bytes of data.

The al register holds one byte of data, so this is an equal swap.

We move the first byte of the array1 element into the al register. After running the operation, the al register now contains the value, AB.

We now increment the si register, so that it points to the next element of the array.

An important thing to note is that when passing in addresses into the si register, each increment in the si register is an increment in the address of 1 byte.

Right now, we are dealing with array elements that are 1 byte; therefore, incrementing the si register by 1 allows us to get to the next array element. However, if the array elements were words (or 2 bytes), we would have to increment the si register by 2. However, we increment by 1, as the array elements are 1 byte elements.

We then move the next array element into the al register. The al register now contains the value, CD

We increment the si register again to point to the next array element.

We move the next array element into the al register. The al register now contains the value, EF



Now let's go into an array composed of words (or 2 bytes).

This will be a little different, because we cannot increment the si register by 1, as explained previously.

Remember an increment of 1 is an increment of 1 byte.

Now that we are dealing with words, we must increment the si register by 2 in order to get to the next array element, since element is 2 bytes of data.

Also, being that words are 2 bytes of data, we must work with the ax register, and not the ah or al registers, which only hold 1 byte of data.

This is shown in the code below.



So we move the array1 element into the si register.

We then move the data in the si register into the ax register.

The ax register holds 2 bytes of data.

We move the first array element into the ax register. The ax register now holds the value, 1234.

We then increment the si register by 2.

We then move the second array element into the ax register. The ax register now holds the value, 2567.

We then increment the si register by 2.

We then move the third array eement into the ax register. The ax register now holds the value, 389A.

So this is how to create and iterate through an array in x86 assembly language.

Related Resources



HTML Comment Box is loading comments...