Lecture 9 Arrays
Lecture 9 Arrays
Lecture 9 Arrays
The following directives indicate the integer’s size and value range:
Example
Data1 DB 87H
Data2 DW 88F8H
(Only the directives, which are underlined, are included in your course)
Directive Description of Initializers
BYTE, DB (byte) Allocates unsigned numbers from 0 to
255.
The amount, which these data types take, are given in the table below.
9.1.1 Constants:
In an assembly language program, constants are defined through the use of the EQU
directive.
These are the main points in using constant definitions:
The EQU directive is used to assign a name to a constant.
Use of constant names makes an assembly language easier to understand.
No memory is allocated for a constant.
Reference is made to a constant through immediate addressing mode.
Example:
Constant Declarations
...
.data
LF EQU 0AH
PROMPT EQU First Message
MSG DB Ali Al-johani
.code
...
MOV DL,LF ; LF means 0AH
MOV DL, LF
...
Arrays are probably the most commonly used data type. Its a data type whose
members (elements) are all the same type. It consists of list of elements all of same
data type. Different elements of array can be accessed through the addresses in the
memory. For example, If i holds the starting address of first element of array then
A[i] chooses the first element from array A. A[I+1] refers to the next element
respectively.
The base address of an array is the address of the first element on the array and
always appears in the lowest memory location. The second array element directly
follows the first in memory; the third element follows the second, etc.
The following example shows how to declare an array of bytes (b_array) and an
array of words (w_array):
Array declaration
b_array BYTE 1, 2, 3, 4
w_array WORD FFFFh, 789Ah, BCDEh
An array may span more than one logical line, such as the array var1 in the example
below, although a separate type declaration is needed in each logical line:
The DUP operator is very often used in the declaration of arrays. This operator
works with any of the data allocation directives.
In the syntax:
The count value sets the number of times to repeat all values within parentheses.
The initial value can be an integer, character constant, or another DUP operator, and
must always appear within parentheses.
For example, the statement allocates the integer value 1 five times for a total of 5
bytes.:
barray BYTE 1, 1, 1, 1, 1
The following examples show various ways for allocating data elements with the
DUP operator:
Another example
This array has 1024 elements, not 256. The n dup (xxxx) operand tells MASM to
duplicate xxxx n times, not create an array with n elements. If xxxx consists of a
single item, then the dup operator will create an n element array. Since there are
four items in the parentheses above, the dup operator creates 256*4 or 1024 items
in the array. The values in the array will initially be 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 ...
.EXIT
END
Example Program 2
.TITLE A program that finds the highest number in a list of numbers and stores in DL
register
.MODEL SMALL
.STACK 200
.DATA
DATA1 DB 2, 6, 1, 3, 5
.CODE
.STARTUP
MOV CX, 5
MOV BX, OFFSET DATA1
SUB AL, AL
AGAIN: CMP AL, [BX]
JA NEXT
MOV AL, [BX]
NEXT: INC AX
LOOP AGAIN
MOV DL, AL
.EXIT
END
.TITLE A program that add elements of two arrays and store it into a new array
.MODEL SMALL
.STACK 200
.DATA
NUM1 DB 2, 6, 1, 3, 5
NUM2 DB 3, 4, 6, 2, 4
SUM DB 5 DUP(0)
.CODE
.STARTUP
MOV BX, OFFSET NUM1
MOV SI, OFFSET NUM2
MOV DI, OFFSET SUM
MOV CX, 5
NEXT: MOV AX, [BX]
ADD AX, [SI]
MOV [DI], AX
INC SI
INC BX
INC DI
LOOP NEXT
.EXIT
END
Write a program which counts the number of zeros in a byte stored in AL register.
Exercise Program 2
Write a program, which searches for a value 3 in a list of given five numbers, suppose, 4, 3,
1, 9, 6
If the program can find the number present in the list then it subtracts 2 from it and saves in
BL register, otherwise it adds 3 in it and stores in BL register.
Pseudocode