2. Program to Linking Array Items in Assembly Language using Visual Studio

Chapter 5

Procedures

Assembly Language Programming Exercise

Problem # 2:

Suppose you are given three data items that indicate a starting index in a list, an array of characters, and an array of link index. You are to write a program that traverses the links and locates the characters in their correct sequence. For each character you locate, copy it to a new array. Suppose you used the following sample data, and assumed the arrays use zero-based indexes:
     start = 1
     chars: H    A    A    C    C    E    B    D    D    F    F    G
     links:  0     4     4     5     5     6     2    3     3     7    7     0
Then the values copied (in order) to the output array would be A,B,C,D,E,F,G,H. Declare the character array as type BYTE, and to make the problem more  interesting, declare the links array type DWORD.

Solution:


INCLUDE Irvine32.inc

.data
start DWORD 1
chars BYTE 'H','A','C','E','B','D','F','G'
links DWORD 0,4,5,6,2,3,7,0
linksType BYTE TYPE links
newArray BYTE LENGTHOF chars DUP(?)

.code
main PROC

    MOV ECX, LENGTHOF newArray
    MOV EDI, OFFSET newArray

    L1:
    ;Get char array element accoridng to start
        MOV ESI, OFFSET chars
        ADD ESI, start
        MOV AL, [ESI]
    ;Move element to new array
        MOV [EDI], AL

    ;Move start to element in the links array
        MOV EDX, OFFSET links
        MOV EAX, start
        MUL linksType
        MOV start, EAX
        ADD EDX, start
    ;Replace start      
        MOV EAX, [EDX]
        MOV start, EAX

        INC EDI
    LOOP L1

    call WaitMsg
    exit
main ENDP

END main