Arrays Assembly
Arrays Assembly
Arrays Assembly
OBJECT:
THEORY:
An array is a collection of elements (usually of same type). A single variable contains the array
elements, each with its own index in the array.
String is a special type of array that is specially used for storing a series of characters. The string
constants are defined within single starting and ending quote.
Pre-Lab Task
PROGRAM:
The program defines array and string in the data segment. It then uses these array variables in the
program by using their starting offset address.
Note: Strings in C++ end with ‘\0’ character. Similarly strings in assembly end with a ‘$’ character.
. MODEL SMALL
. STACK 100H
.DATA
NUMBERS DB 0, 2, 4, 6, 8
NUM_LENGTH = $ - OFFSET NUMBERS
UNKNOWN DB 5 DUP (0)
I DB 0
GREETING DB 'ARRAY ELEMENTS ARE', '$'
NEWLINE DB 0AH, 0DH, '$'
. CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MAIN ENDP
END MAIN
LEA or OFFSET keywords are used for arrays because they load the offset address of the first element
of the array. Array elements in the memory are arranged in continuous memory locations. So, if system
gets the address of first element, it can access rest of the elements. The interrupt function number 9 is
used to output string.
. MODEL SMALL
. STACK 100H
.DATA
ARR DB 1,2,3,4
ARR_LEN = $ - OFFSET ARR ; ARR_LEN WILL CONTAIN THE LENGTH
OF THE ARRAY ELEMENTS
. CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AH, 2
MOV BX, 0
NEXT:
MOV DL, ARR[BX]
ADD DL, 48
INT 21H
INC BX
CMP BX, ARR_LEN
JL NEXT
MAIN ENDP
END MAIN
In-Lab Task:
Task1
Define an array of words (with DW) and print its elements in reverse order.
Task 2
Write a program to one array to another.
Task 3
Write a program that finds sum of elements of an array of size three.
Post-Lab Assignment:
REVIEW QUESTIONS
1. Undefined Behavior: The behavior of code that operates on the string may become undefined.
Functions that process strings often rely on null-termination to determine the end of the string.
Without proper termination, these functions may continue processing memory beyond the
intended length of the string.
2. Memory Overrun: Without a proper termination character, code reading or manipulating the
string may continue reading beyond the intended length of the string, leading to memory
overruns and potential crashes.
3. Incorrect Length Calculation: Functions that calculate the length of a string by counting
characters until a null character is encountered may fail to provide the correct length if the string
is not properly terminated.
3. What will be the output of printing string if in the middle of the string comes the character ‘$’?
The printing operation would interpret the '$' character as the end of the string, and the printing
would stop at that point.
4. What will be the effect on output if we print an increment array index further off its original size
(total number of elements)?
1. Memory Overrun:
Accessing elements beyond the bounds of an array can result in reading data from or
writing data to memory locations that do not belong to the array.
This can lead to unpredictable values being printed, or it may even cause a segmentation
fault or memory access violation.
2. Garbage Values:
Accessing memory beyond the end of the array may lead to printing values that were not
part of the original array.
These values are essentially garbage values and may have been used by other parts of the
program or even by other programs.
3. Program Crash:
Accessing invalid memory locations can cause the program to crash.
Modern programming languages and operating systems often have protections against
such behavior, but the behavior is still undefined, and a crash might occur.
5. What is the purpose of using offset or lea for loading an array or string in register? What will
happen if array/string name is directly used without using offset?
Using OFFSET:
It provides the offset (memory address) of a variable, which can be used to access or manipulate
the data at that location.
Using LEA:
The LEA instruction in x86 assembly is versatile and can be used to load the effective address of a
memory operand into a register.
It's commonly used for loading the address of an array or a string into a register.
If the array or string name is used directly without OFFSET or LEA, then it is referring to the
content of the array or string, not its address.
Viva
Performance (15Marks) Total/20
(5 Marks)
Pre-Lab Exercise /3
Performance /4
Results /3
Critical Analysis /2
Post Lab Exercise /3
Comments