In: Computer Science
1. Explain what the loop is doing and what is the final value of AX after this code has been executed?
.data
word1 WORD 100h, 200h, 300h, 400h, 500h
.code
mov ESI, OFFSET [word1+8]
mov ECX, 2
mov EAX, 0
L1:
mov AX, [ESI]
add AX, 20h
sub ESI, 4
LOOP L1
2. Use the XCHG instruction to reorder the array from [134Fh, 2EA6H, 1234h, F0F0h] to [2EA6H, F0F0h, 1234h, 134Fh]: (Direct Addressing)
.data
array1 WORD 134Fh, 2EA6H, 1234h, F0F0h
.code
ANSWER:--
GIVEN THAT:--
1)
mov EDX,OFFSET word1+8
mov ECX,2
mov EAX,0
L1:
mov AX,[EDX]
add AX,20h
sub EDX,4
Loop L1
and
word1 WORD 100h,200h,300h,400h,500h
SO Value of Register AX would be 320h
METHOD--2:--
Step 1: Given program has two sections: data and code.
In data section, an array word1 of WORD type declared with 5 elements.
WORD is of 16-bit size, so each element of the array requires 2-bytes.
Step 2: In the code section,
ESI initialised with address of last element of the array word1. ( each element requires 2-bytes, so word1 is address of first element, word1+8 is address of last element).
ECX, which controls the loop initialised with 2. It means the loop will execute for 2 times.
EAX initialised with zero.
Step 3: Inside loop L1, AX initialised with element at ESI i.e. last element of word1, 500h.
add 20h to AX. AX = 500h + 20h = 520h
subtract 4 from ESI. ESI = ESI - 4 = points to third element of array word1.
goto loop L1 if ECX not zero.
Step 4: loop L1 executes as ECX is one.
mov AX, [ESI] will move 3rd element of array word1, 300h to AX.
add 20h to AX. AX = 300h + 20h = 320h
subtract 4 from ESI. ESI = points to first element of array word1
goto loop L1 if ECX in non-zero.
Step 5: As ECX is zero. Exit from the loop.
Hence the loop is accessing 5th element and adding 20h, updating ESI and then accessing 3rd element, adding 20h to it, updating ESI.
Final content of AX is 320h.