Tuesday, 21 July 2015

The concept of Arrays in Assembly Language !

An array allows us to declare a collections of variables that referenced by a common name.
Suppose you have to write a program which stores marks of 50 students.
In normal way, you declare  50 variables  which store the value of 50 student's marks. So instead of creating individually variables, this can be done by using Arrays. Instead of declaring individual variables, such as marks0, marks1, ..., and marks49, you declare one array variable such as "marks" and use marks[0], marks[1], and ..., marks[49] to represent individual variables.

Arrays are of two types:
One-dimensional arrays
Multidimensional arrays(Two or more)

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows:
data_type arrayName [Size];
For example:
int marks[49]; // this is declaration of one-dimensional array.
Initialization of one-dimensional array:
int marks[5]={67,89,55,76,67};
pictorial representation of the array we discussed above:
                                                         

                            
                                                     

Arrays in Assembly language: We have already discussed that the data definition directives to the assembler are used for allocating storage for variables. Assembly does not care what your variable is. It only cares how many bytes it needs.

1-Dimensional Arrays:There are two ways to define an array in assembly language. One way to declare an array:-
Initialized Lists :An initialized array is defined in the same way as a scalar variable, but with multiple initial values:
scores: .word   100, 78, 63, 88, 52, 91, 75
       
MAL also provides a mechanism for defining a large array with all elements initialized to the same value:
scores: .word   0:100
       
The another way to declare an array:-
Uninitialized Memory Blocks :Uninitialized arrays are defined using the .space directive:
scores: .space 100
The .space directive allocates the specified number of bytes. Specifying the desired number of array elements is a common mistake.

What is the difference between these two declarations? Answer: The first way initializes this declared memory space before the program begins execution. The second way (with the .space directive) does not initialize memory. It allocates the 100 bytes, but does not change their contents before the program begins its execution.

And if we wanted to declare the array of 13 characters, but initialize each character to the value 'A'?:
my_As:    .byte   'A':13

Alternatively:
my_As:    .byte   65:13

This second way works fine, but may be less clear to a programmer looking at the code. This second way initializes each byte of the array to contain the 8-bit, two's complement representation for the decimal value 65. Since that is the ASCII character encoding for 'A', it works equally well.

To declare an array of integer-sized elements, on the MIPS architecture, each integer requires 4 bytes (or 32 bits). Also, each word on the MIPS architecture is 4 bytes. Therefore, we may use the .word directive to declare an array of integers:
int_array:  .word   0:36

This declaration allocates 36 words (integer-sized memory chunks), which are all (nicely) located at word-aligned addresses. The initial value of each array element is 0.

If, instead, we wanted an array of 36 integers, where each element is initialized to the value 2, we may use:
all_twos:   .word   2:36



If you like this post or have any question, please feel free to comment!

No comments:

Post a Comment

Blogger Widget