Assembly Arrays - Definition Syntax Usage and Examples

Prahlad Godara ------ From DOOSEEP


Arrays are used in assembly language to store multiple data of the same type at once. That is, you can consider it as a table or list that stores data of the same size and type. Like a variable has the address of a data. Similarly array contains address of multiple data which you can get back through index.

Assembly Arrays - Syntax

As the initial value in the variable is specified in hexadecimal, decimal or binary form.

For example, we can define a word variable 'month' in any of the following ways −
 MONTHS DW 12
      MONTHS DW 0CH
      MONTHS DW 0110B 

Similarly data definition instructions can also be used to define one-dimensional arrays.

Arrays define in Assembly language

An assembly language instruction is needed to define the size of each element in the array. First of all the identifier is the variable, then there is the data type and then there is the value list.

 identifier: datatype item 1 , item 2 , item 3 , item 4 , 

We further make this one dimensional word array. Each element of which is 2 bytes long and 'AbList' is assigned as its identifier. The data type for the DW instruction word is:

Define a one dimensional array of Numbers.

 AbList :  DW  84,  35,  57,  27,  15, 29 

The above definition declares an array of six words each starting with the numbers 84, 35, 57, 27, 15, 29. It allocates 2x6 = 12 bytes of contiguous memory space. The coded address of the first number will be AbList and the coded address of the second number will be AbList + 2 and so on.

You can do it like this also. and can initialize all values with zero, like -

  INVENTORY   DW  0
    DW  0
    DW  0
    DW  0
    DW  0
    DW  0
    DW  0
    DW  0 

And in this way too. Using TIMES, the Inventory array can be defined as:

  INVENTORY TIMES 8 DW 0 

The TIMES instruction can also be used for multiple initializations for the same value.

Getting data value from array in assembly

To access array elements, we obtain a pointer to the element we want to access. Then you can get the data back through the index.

The following snippet of code shows how to do this.

  mov ecx, [esi];pointer to element at the 0th index stored in the register ecx
  mov ecx, [esi+1];pointer to element at the 3rd index stored in the register ecx
  mov ecx, [esi+2];pointer to element at the 2nd index stored in the register ecx

Assembly Arrays - Example

The following example demonstrates the above concepts by defining a 3-element array x that stores three values: 1, 4, and 3. This adds the values to the array and displays the sum 8


  section	.text
   global _start   ;must be declared for linker (ld)
	
_start:	
 		
   mov  eax,3      ;number bytes to be summed 
   mov  ebx,0      ;EBX will store the sum
   mov  ecx, a     ;ECX will point to the current element to be summed

top:  add  ebx, [ecx]

   add  ecx,1      ;move pointer to next element
   dec  eax        ;decrement counter
   jnz  top        ;if counter not 0, then loop again

done: 

   add   ebx, '0'
   mov  [sum], ebx ;done, store result in "sum"

display:

   mov  edx,1      ;message length
   mov  ecx, sum   ;message to write
   mov  ebx, 1     ;file descriptor (stdout)
   mov  eax, 4     ;system call number (sys_write)
   int  0x80       ;call kernel
	
   mov  eax, 1     ;system call number (sys_exit)
   int  0x80       ;call kernel

section	.data
global x
a:    
   db  1
   db  4
   db  3

sum: 
   db  0

When the above code is compiled and executed, it produces the following result −

 8

The 2-dimensional array is also similar, but instead of the data address, it has the address of another 1-dimensional array. A 2 dimensional array is a set of 1 dimensional array list list. In which there is array inside array and then there is data.



Tags- Nasm Assembly language Arrays. This blogcreates content similar to stackoverflow geeks for geeks tutorialspoint w3schools and dooseep, Arrays in nasm