Professional Documents
Culture Documents
Computer Organization
2D array of strings
2D arrays of numbers
Objectives:
In this lab we will practice base index addressing mode to handle 2D array of strings.
Tools Used:
Visual Studio
2D Array
A two-dimensional array is a table with R rows and C columns, written R x C. An element of the array is
specified by [r,c], where r is the row and c is the column. The figure below illustrates a 3 x 4 array with
the elements labelled by row and column.
Ordering of Rows and Columns High Level Language, two dimensional arrays are arranged either
row major order or column major order.
.data
byte "assembly",0ah,0dh
byte "language",0ah,0dh
byte "notebook",0ah,0dh
Rows =4
Columns = 8
.code
mov ch , Rows
L2:
mov cl , Columns
mov esi , 0
L1:
mov al , [ebx+esi]
call writechar
inc esi
dec cl
Jnz L1
dec ch
Jnz L2
XOR instruction:
XOR clears the output on same inputs and set the output on different inputs.
0 0 0
0 1 1
1 0 1
1 1 0
Example
KEY = 239
Mov esi , 0
Inc esi
Lab Tasks:
1. Suppose you have an array of 10 strings of equal size. Print each string on separate line.
CODE:
include irvine32.inc
.data
strr byte "cad", 0ah ,0dh
row = ($ - strr)
col = 10
.code
main proc
mov ebx , offset strr
mov ch , row
L2:
mov cl , col
mov esi , 0
L1:
mov al , [ebx+esi]
call writechar
inc esi
dec cl
Jnz L1
add ebx, col
dec ch
Jnz L2
exit
main endp
end main
OUTPUT:
2. Get 10 names from the user and store in an array of strings. Then ask the user to enter the
number and print the name stored at that number.
CODE:
include irvine32.inc
.data
array2d byte 10(5) dup(?)
msg byte " ",0
msg1 byte "Enter the index to print the name: ",0
rowsize byte 5
.code
main proc
mov ebx, offset array2d
mov ecx,10
mov esi,0
l1:
push ecx
mov ecx,5
l2:
call readchar
mov [ebx+esi],al
inc esi
loop l2
call crlf
pop ecx
loop l1
mov ebx,offset array2d
mov ecx,10
mov esi ,0
l3:
push ecx
mov ecx,5
l4:
mov al, [ebx+esi]
call writechar
inc esi
loop l4
pop ecx
call crlf
loop l3
call readint
mov ecx,5
mov esi,0
mul rowsize
add ebx,eax
l5:
mov al, [ebx+esi]
inc esi
call writechar
loop l5
exit
main endp
end main
OUTPUT:
3. Write a procedure that is passed offset of an array of 10 strings of characters and prints all the
strings.
CODE:
include irvine32.inc
.data
strr byte "cad", 0ah ,0dh
row = ($ - strr)
col = 10
.code
main proc
call sample
exit
main endp
sample proc
mov ebx , offset strr
mov ch , row
L2:
mov cl , col
mov esi , 0
L1:
mov al , [ebx+esi]
call writechar
inc esi
dec cl
Jnz L1
add ebx, col
dec ch
Jnz L2
ret
sample endp
end main
OUTPUT:
4. Modify the above code to make it a procedure which is passed offset of a 10 x 10 array integers
of DWORDS, and it Prints the 2D array and Sum of Each Row in a line with space between them.
CODE:
include irvine32.inc
.data
array2D DWORD 1,2,3,4,5,6,7,8,9,10
rowsize=($-array2D)/TYPE array2D
DWORD 81,82,83,84,85,86,87,88,89,90
DWORD 11,12,13,14,15,16,17,18,19,20
DWORD 21,22,23,24,25,26,27,28,29,30
DWORD 41,42,43,44,45,46,47,48,49,50
DWORD 51,52,53,54,55,56,57,58,59,60
DWORD 91,92,93,94,95,96,97,98,99,100
DWORD 61,62,63,64,65,66,67,68,69,70
DWORD 31,32,33,34,35,36,37,38,39,40
DWORD 71,72,73,74,75,76,77,78,79,80
colsize=($-array2D)/TYPE array2D/rowsize
msg BYTE " ",0
msg1 BYTE "Sum of row = ",0
.code
printarray proc
mov ecx,colsize
mov esi,0
L1:
push ecx
mov ecx,rowsize
L2:
mov eax,[ebx+esi*TYPE array2D]
call writeint
mov edx,offset msg
call writestring
inc esi
Loop L2
pop ecx
call crlf
Loop L1
ret
printarray endp
sumrows proc
mov ecx,colsize
mov esi,0
L1:
push ecx
mov ecx,rowsize
mov eax,0
L2:
add eax,[ebx+esi*TYPE array2D]
inc esi
Loop L2
mov edx,offset msg1
call writestring
call writeint
pop ecx
call crlf
Loop L1
ret
sumrows endp
main proc
mov ebx,offset array2D
call printarray
call sumrows
exit
main endp
end main
OUTPUT: