Array Processing - Textbook Labs
Array Processing - Textbook Labs
Array Processing
Statement Purpose:
In this lab, student will know about declaration of arrays and their processing element by
element. Student will be introduced various modes to access array elements.
Other objective of this lab is to introduce students with instructions dealing with blocks
of data whether it need to transferred or searched. Strings are also blocks of data as well
as arrays so there would be efficient ways to work upon these data. Specific types of
instructions exist which moves, search and scan blocks of data quickly as well as helping
programmers to make code shorter in size, students will get an exposure to these set of
instructions usually falls under the category of strings instructions.
Activity Outcomes:
This lab teaches you the following topics:
Instructor Note:
As pre-lab activity, read Chapter 4, 9 from the book (Assembly Language for X86
processors, KIP R. IRVINE., 7th Edition (2015), Pearson), and also as given by your theory
instructor.
Introduction
From an assembly language programmer’s perspective, a two-dimensional array is a high-
level abstraction of a one-dimensional array. High-level languages select one of two
1|Page
methods of arranging the rows and columns in memory: row-major order and column-
major order. When row-major order (most common) is used, the first row appears at the
beginning of the memory block. The last element in the first row is followed in memory
by the first element of the second row. When column-major order is used, the elements
in the first column appear at the beginning of the memory block. The last element in the
first column is followed in memory by the first element of the second column.
If you implement a two-dimensional array in assembly language, you can choose either
method. In this lab, we will use row-major order. If you write assembly language
subroutines for a high- level language, you will follow the ordering specified in their
documentation.
The x86 instruction set includes two operand types, base-index and base-index-
displacement, both suited to array applications. We will examine both and show examples
of how they can be used effectively.
String primitive instructions are unusual in that they require no register operands and are
optimized for high-speed memory access. They are
2|Page
Each string primitive instruction has a suffix of B, W, or D when manipulating bytes, words,
and doublewords, respectively.
The repeat prefix REP repeats a string primitive instruction with automatic incrementing
or decrementing of index registers. For example, when REPNE is used with SCASB, it scans
memory bytes until a value in memory pointed to by EDI matches the contents of the AL
register. The Direction flag determines whether the index register is incremented or
decremented during each iteration of a string primitive instruction.
Activities:
Activity 1:
Write an application that does the following: (1) fill a 32-bit array with 50 random
integers; (2) Loop through the array, displaying each value, and count the number
of negative values; (3) After the loop finishes, display the count.
Solution:
INCLUDE Irvine32.inc
.data
.code
main PROC
call Randomize
loop L1
3|Page
; Search for negative values
L2:
L3:
Activity 2:
Implement the following C++ code in assembly language, using the block-
structured.IF and .WHILE directives. Assume that all variables are 32-bit signed
integers:
intarray[] = {10,60,20,33,72,89,45,65,72,18};
int sum = 0;
while( index<ArraySize )
4|Page
{
sum += array[index];
index++;
Solution:
INCLUDE Irvine32.inc
.data
sample DWORD 50
sum DWORD 0
.code
main PROC
5|Page
mov eax,array[esi*4] ; sum += array[index] add sum,
eax
.ENDIF
.ENDW
Activity 3:
Write a program that defines symbolic names for several string literals (characters
between quotes). Use each symbolic name in a variable definition.
Solution:
INCLUDE Irvine32.inc
sym2 TEXTEQU <“Press any key to continue…”> sym3 TEXTEQU <“Insufficient user
training”> sym4 TEXTEQU <“Please re-start the system”>
.data
msg1 BYTE sym1 msg2 BYTE sym2 msg3 BYTE sym3 msg4 BYTE sym4
.code
main PROC
exit
6|Page
main ENDP END main
Activity 4:
Write a program that defines and declare an array of 10 elements and then reverse
the elements.
Solution
.386
.modelflat,stdcall
.data
.code
main PROC
array
L1:
7|Page
mov eax,array[esi] xchg eax,array[edi] mov array[esi],eax
loop L1
call DumpMem
call ExitProcess,0
Activity 5:
Using a loop and indexed addressing, write code that rotates the members of a 32-
bit integer array forward one position. The value at the end of the array must wrap
around to the first position. For example, the array.
[10,20,30,40] would be transformed into [40,10,20,30].
Solution:
ExitProcess proto
.data
8|Page
L1:
9|Page