2

I have an array of two-byte integers, which I am populating with random 3 digit numbers, using ESI to walk through the array. I am getting an access violation when I attempt to access the 9th element of the array and store it in ESI.

ARRAYSIZE = 200

.data
list        WORD    ARRAYSIZE   DUP(?)

fillArray PROC
    push    OFFSET list
    mov     esi, [esp] ;GET ADDRESS OF FIRST ELEMENT INTO ESI
    mov     ecx, request ;NUMBER OF ELEMENTS TO BE ADDED

ArrFill:
    ;calculate random 3-digit number, store in eax
    dec     ecx
    mov     [esi], eax ;THIS IS THE LINE THAT THROWS THE EXCEPTION
    sub     esi, 2
    cmp     ecx, 0
    jnz     ArrFill

Exception thrown: Access violation writing location 0x00405FFE (The value of ESI when thrown).

When I change the array to four-byte integers, I also get an access violation for trying to access the 5th element of the array at the same address.

hudspero
  • 139
  • 1
  • 1
  • 9

2 Answers2

1
push    OFFSET list
mov     esi, [esp] ;GET ADDRESS OF FIRST ELEMENT INTO ESI

Why not simply assign the offset to ESI with mov esi, OFFSET list

mov     [esi], eax ;THIS IS THE LINE THAT THROWS THE EXCEPTION

Since the array contains words you can only write the contents of AX, not EAX! Use mov [esi], ax

sub     esi, 2

To progress through the array you need to add to the pointer not subtract from it. Use add esi, 2

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
-1

Hudspero

You are getting an access violation because you are trying to write past the array boundary.

Few things:

  1. You don't seem to be initializing ecx. You should initialize this to the size of the array.
  2. As you are cmp ecx,0 you should be decrementing ecx before the comparison
  3. You should move the esiaddress adjustment after you ensure you won't write past the boundary. I would restructure the code to test ecx for zero and jump out if true, otherwise decrement esiand jump back to ArrFill
Frank C.
  • 7,758
  • 4
  • 35
  • 45
  • I included the line ;using ecx as counter specifically to reference the fact that I am using ECX. I did not think it was necessary to include it but I will now. – hudspero Feb 28 '16 at 18:07