Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that we execute function hw2 with the following inputs: arr1 = {20, 43, 4

ID: 3856691 • Letter: A

Question

Assume that we execute function hw2 with the following inputs: arr1 = {20, 43, 45, 5, 7, 76, 234, 56, 6, 8};

len = 10;

Answer the following questions:

(i) Show the content of the registers (ESI, EDI, ECX) right after the instruction in line 5 is executed.

(ii) Show the content of the registers (EAX, EBX, EDX) right after the 5th execution of the instruction in line 15.

(iii) Show the content of the registers (EAX, EBX, EDX) right after the 8th execution of the instruction in line 15.

(iv) Show the content of the registers (EAX, EBX, ECX, EDX) in line 17.

(v) Briefly describe what this function computes.

Notes:

You may represent values in decimal or hexadecimal.

Denote the base address of array by &arr1.

int hw2(int *arr1, int len)

{

     int ret_val;

__asm{

1         mov esi, arr1

2         mov edi, 0

3         mov ecx, len

4         mov edx, 0

5         mov ebx, 0

6    WHILE:

7         cmp ebx, len

8         jge WHILE_END

9         mov eax, dword ptr[esi+4*ebx]

10        and eax, 1

11        cmp eax, 1

12        jne INCREMENT_INDEX

13        inc edx

14 INCREMENT_INDEX:

15        inc ebx

16        jmp WHILE

17 WHILE_END:

18        mov eax, edx

19        mov ret_val, eax

}

return ret_val;

   }

Explanation / Answer

i.

ESI = &arr1 //moving the array base address

EDI = 0   

ECX = 10

ii.

EAX = 1

EBX = 5

EDX = 5

iii.

EAX = 0

EBX = 8

EDX = 8

iv.

EAX = 0

EBX = 10

ECX = 10

EDX = 10

v.

The program simply gives the number of elements whose 0th bit is set.