Assembly Arrays
Assembly Arrays
http://www.tutorialspoint.com/assembly_programming/assembly_arrays.htm
Copyright tutorialspoint.com
We have already discussed that the data definition directives to the assembler are used for
allocating storage for variables. The variable could also be initialized with some specific value. The
initialized value could be specified in hexadecimal, decimal or binary form.
For example, we can define a word variable 'months' in either of the following way
MONTHS DW 12
MONTHS DW 0CH
MONTHS DW 0110B
The data definition directives can also be used for defining a one-dimensional array. Let us define
a one-dimensional array of numbers.
NUMBERS DW
34,
45,
56,
67,
75, 89
The above definition declares an array of six words each initialized with the numbers 34, 45, 56,
67, 75, 89. This allocates 2x6 = 12 bytes of consecutive memory space. The symbolic address of
the first number will be NUMBERS and that of the second number will be NUMBERS + 2 and so on.
Let us take up another example. You can define an array named inventory of size 8, and initialize
all the values with zero, as
INVENTORY
DW
DW
DW
DW
DW
DW
DW
DW
0
0
0
0
0
0
0
0
DW
0, 0 , 0 , 0 , 0 , 0 , 0 , 0
The TIMES directive can also be used for multiple initializations to the same value. Using TIMES,
the INVENTORY array can be defined as:
INVENTORY TIMES 8 DW 0
Example
The following example demonstrates the above concepts by defining a 3-element array x, which
stores three values: 2, 3 and 4. It adds the values in the array and displays the sum 9
section .text
global _start
_start:
mov
mov
mov
top:
eax,3
ebx,0
ecx, x
add
add
dec
jnz
ebx, [ecx]
ecx,1
eax
top
done:
add
mov
ebx, '0'
[sum], ebx ;done, store result in "sum"
display:
mov
mov
mov
mov
int
edx,1
ecx, sum
ebx, 1
eax, 4
0x80
;message length
;message to write
;file descriptor (stdout)
;system call number (sys_write)
;call kernel
mov
int
eax, 1
0x80
section .data
global x
x:
db 2
db 4
db 3
sum:
db
When the above code is compiled and executed, it produces the following result
9