Arrays Assembly

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

LAB 09: IMPLEMENTATION OF ARRAYS AND STRINGS IN ASSEMBLY.

OBJECT:

Arrays and Strings in Assembly.

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, 9 ; TO OUTPUT STRING


; MOV DX, OFFSET GREETING
LEA DX, GREETING
INT 21H
; PRINT NEW LINE STRING, AH ALREADY
CONTAINS STRING OUTPUT FUNCTION 9
MOV DX, OFFSET NEWLINE
INT 21H
MOV AH, 2
MOV CL, 0
LEA SI, NUMBERS ; MOV THE ARRAY TO ONE OF THE INDEXING REGISTERS
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.

There is another more intuitive way of accessing the array elements.


The program below shows an example.

. 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. How are arrays and strings arranged in the memory?

• Arrays and strings are both arranged in contiguous memory locations.


• Arrays are collections of elements of the same data type, stored sequentially.
• Strings are sequences of characters often represented as character arrays.
• Strings are typically null-terminated, and the null character marks the end of the string.

2. What will happen if a string is not terminated with a ‘$’ character?

If a string is not properly null-terminated, it can lead to several issues:

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:

 The OFFSET keyword is often used in MASM (Microsoft Assembler) syntax.

 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.

Critical Analysis /Conclusion

Viva
Performance (15Marks) Total/20
(5 Marks)
Pre-Lab Exercise /3
Performance /4
Results /3
Critical Analysis /2
Post Lab Exercise /3
Comments

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy