Monday 13 November 2017

Arrays- Assembly language for 8086 processor

Arrays in assembly language 8086 processor

Defining Arrays?

To define an array of 10 elements, each of 1-byte size, one can write
ArrayName db 1,2,3,4,5,6,7,8,9,10; This will reserve 10 bytes in consecutive memory locations. Similarly, to define an array of 10 elements, each of two byte sized, one can write
ArrayName dw 1,2,3,4,5,6,7,8,9,10; Same goes for double type or quad-word type arrays with db replaced with dd or dq, respectively.

How data in arrays is represented in memory?

Consider few examples below of defining arrays; array1 is byte type array whereas array2 is word type array.
array1 db 1,2,3,4,5;
array2 dw 0xa, 0xb, 0xc, 0xd, 0xe;
array1 is shown via highlighted area in the figure attached as contiguous locations in memory. Array2 is stored in memory locations immediately after array1 elements. Note that array2 is word type array, requires two bytes for one elements, and those two bytes are represented in little endian notation as lower byte at lower address in memory. 0xA when written in two bytes is represented as 000A, here, 0A being the lower byte is stored at lower address 010D and 00 being the higher byte is stored at next location address 010E. Next word from array2 is stored at next location in same format.


To define an array of 100 elements ‘’dup’’ operator can be used such as;
Array1 db 100 dup(0); defines an array of 100 elements with zero as initial values.

Accessing data in arrays?

Consider examples below.
i.                 Using array name in instruction refers to the first element of that array interpreting array name as the address of first element.

MOV AL, ARRAY1; THIS WILL LOAD FIRST ELEMENT FROM ARRAY1
MOV BX, ARRAY2; THIS WILL LOAD FIRST ELEMENT FROM ARRAY2
RET
ARRAY1 DB 1,2,3,4,5;
ARRAY2 DW 0XA, 0XB, 0XC, 0XD, 0XE;

           To access next elements within an array, add offsets to array name depending upon array type.

MOV AL, ARRAY1+1; THIS WILL LOAD SECOND ELEMENT FROM ARRAY1
MOV BX, ARRAY2+2; THIS WILL LOAD SECOND ELEMENT FROM ARRAY2
RET
ARRAY1 DB 1,2,3,4,5;
ARRAY2 DW 0XA, 0XB, 0XC, 0XD, 0XE;

Note that to refer to next element within array, you will have to add offset according to array element size. As in above example, to access second element in array1 you add ‘1’ whereas to access second element in array2 you add ‘2’ to primary address.

iii.                 Exercise? Note and observe the value loaded into bx register.

MOV BX, ARRAY2+1; WHAT NUMBER WILL BE LOADED INTO BX FROM ARRAY2?
RET
ARRAY2 DW 0XA, 0XB, 0XC, 0XD, 0XE;

Accessing data within arrays using registers? Register indirect addressing

Example
; Program to add an array and place its sum in variable Result;
LEA BX, ARRAY;
MOV CX, 0
MOV AX, 0
L1:
ADD AX, [BX];
ADD BX, 2;
INC CX;                 ; adds ‘1’ to the destination operand.
CMP CX, 5
JNE L1
MOV RESULT, AX;
.EXIT
ARRAY DW 1,2,3,4,5
RESULT DW 0

Example-
; Program to add an array and place its sum in variable Result; Another approach
MOV SI, 0
MOV AX, 0
LEA BX, ARRAY
L1:
ADD AX, [BX+SI]
ADD SI, 2
CMP SI, 10
JNE L1   ; Jumps to label L1 if value of SI is not equal to 10.
MOV RESULT, AX
.EXIT
ARRAY DW 1,2,3,4,5;
RESULT DW 0
  
To access data from memory using register indirect addressing, intel’s 8086 architecture’s supported registers are bx, si, di, and bp only. Following is an example to access memory locations using si, bx and di registers.  
Program below loads first value from VEC1 and VEC2, adds them and places their result in VEC3 at first index location.

LEA SI, VEC1
LEA BX, VEC2
LEA DI, VEC3

MOV AL, [SI]
ADD AL, [BX]
MOV [DI], AL
RET

VEC1 DB 1, 2, 5, 6
VEC2 DB 3, 5, 6, 1
VEC3 DB ?, ?, ?, ?

Traversing all elements within an array using INC, DEC and JNE instructions.

LEA SI, VEC1
LEA BX, VEC2
LEA DI, VEC3
MOV CX, 4

L1:
MOV AL, [SI]
ADD AL, [BX]
MOV [DI], AL

INC SI;                   ; adds ‘1’ to the destination operand.
INC BX;
INC DI;
DEC CX;                Subtracts ‘1’ from the destination operand.
JNZ L1;                  Repeats executing instructions from label L1 until CX becomes zero.

RET

VEC1 DB 1, 2, 5, 6
VEC2 DB 3, 5, 6, 1
VEC3 DB ?, ?, ?, ?



No comments:

Post a Comment

Effect of ADD and SUB instructions on Flag register-Assembly Language for 8086 processor

Flag Register NOTE: ONE OR MORE FLAGS MAY GET SET OR RESET AFTER AN ARITHMETIC LOGIC INSTRUCTION MOV INSTRUCTION DOESN’T AF...