Coal Lab 11
Coal Lab 11
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
mov ah, 2
mov cl, 0
lea si, numbers ; mov the array to one of the indexing register
lbl:
mov dx, [si]
add dl, 48 ; convert the number to its ASCII value
int 21h ; output the number
inc si
inc cl
cmp cl, 5 ; 5 is the length of array
jl lbl
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
Abdullah Javed SP22-BCS-136
In-Lab Task:
Q1
Define an array of words (with dw) and print its elements in reverse order.
Explanation:
This assembly code defines an array of words (1, 2, 3, 4, 5), and then prints the reversed order of these
numbers on the screen using DOS interrupts. It employs a loop to iterate through the array in reverse,
printing each element, and includes subroutines to display individual characters and a newline for better
readability. The program terminates by invoking a DOS interruption for program exit.
Q2
Write a program to copy one array to another.
Abdullah Javed SP22-BCS-136
Explanation:
This assembly program copies elements from the src_array to thusing a loop. After copying, it
displays the copied array elements on the screen. The display logic converts each array element
to its ASCII representation, adds a space for readability, and uses DOS interrupt int 21h with
function ah=2 to print each character. The program then terminates using DOS interrupt int
21h with function ah=4ch. Adjust the array sizes or modify the display logic as needed.
Q3
Write a program that finds sum of elements of an array of size three.
Abdullah Javed SP22-BCS-136
Explanation:
The program calculates the sum of elements in an array of size three and displays the result on the
screen using DOS interrupts. It uses the AX register to accumulate the sum and converts the sum to
ASCII for display. The program then exits using a DOS interrupt
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.
Abdullah Javed SP22-BCS-136
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.
Abdullah Javed SP22-BCS-136
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.
Abdullah Javed SP22-BCS-136
Pre-Lab Exercise /3
Performance /4
Results /3
Critical Analysis /2
Post Lab Exercise /3
Comments