Search code examples
assemblyx86

Error in assembly x86: Access violation writing location 0x0081D45C


I want to put the elements from the x[] array >=10 in the vmic[] array, and the others in the vmare[] array. But it throws an error: Access violation writing location 0x0081D45C, when I want to copy the element in one array and I don't know what to do. Thank you in advance!

#include <iostream>

using namespace std;

int main()
{
    //I want to put the elements >=10 in the vmic[] array, and the others int the vmare[] array.
    //until the element in the array is 0
    int x[] = { 1,2,3,4,5,14,22,0 };
    int vmic[10];
    int vmare[10];
    int n = 10;
    int k1 = 0; //vmic
    int k2 = 0; // vmare
    _asm {
        mov eax, 0; //index
        lea ebx, x; 
        lea ecx, vmic; 
        lea edx, vmare; 
        mov esi, n; //val 10
    bucla:
        cmp[ebx + 4 * eax], 0;
        je afara;
        cmp[ebx + 4 * eax], esi;
        jge mai_mare_egal;
        inc k1;
        mov edi, [ebx + 4 * eax]; 
        mov[ecx + 4 * k1], edi;
    mai_mare_egal:
        inc k2;
        mov edi, [ebx + 4 * eax];
        mov[edx + 4 * k2], edi;
    inc eax;
    jmp bucla;
    afara:
    }
    int i;
    for (i = 0; i <= 6; i++)
    {
        printf("% d", x[i]);
    }
    printf("\n");
    for (i = 0; i <= k1; i++)
    {
        printf("% d", vmic[i]);
    }
    printf("\n");
    for (i = 0; i <= k2; i++)
    {
        printf("% d", vmare[i]);
    }
    return 0;
}

the error


Solution

  • I tried debugging this in VS2022, and it gets the line-numbers mixed up sometimes. Better to debug in the Disassembly window ... Also, there was some pretty weird stuff happening in the Disassembly window regarding the on-the-fly pointer arithmetic. For example, this line:

    mov[edx + 4 * k2], edi;
    

    ... became this:

    mov         dword ptr k2[edx*4],edi  
    

    It's been 20+ years since I last wrote assembly, but that looks weird to me. It looks like it's treating k2 as a dword array, and edx as the offset into that array ... which isn't right.

    Anyway, I had a go at fixing it.

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      //I want to put the elements >=10 in the vmic[] array, and the others int the vmare[] array.
      //until the element in the array is 0
      int x[] = { 11,2,3,4,5,14,22,0 };
      int vmic[10];
      int vmare[10];
      int n = 10;
      _asm {
        mov eax, 0; //index
        lea ecx, vmic;
        lea edx, vmare;
        mov esi, n; //val 10
      bucla:
        lea ebx, x;
        add ebx,eax
        cmp dword ptr[ebx], 0;
        je afara;
        cmp [ebx], esi;
        jge mai_mare_egal;
        mov edi, [ebx];
        mov [ecx], edi;
        add ecx,4;
        add eax, 4;
        jmp bucla;
      mai_mare_egal:
        mov edi, [ebx];
        mov [edx], edi;
        add edx,4;
        add eax, 4;
        jmp bucla;
      afara:         
        mov dword ptr [edx], 0
        mov dword ptr [ecx], 0
      }
      int i;
      for (i = 0; x[i]; i++)
        printf("% d", x[i]);
      printf("\n");
      for (i = 0; vmic[i]; i++)
        printf("% d", vmic[i]);
      printf("\n");
      for (i = 0; vmare[i]; i++)
        printf("% d", vmare[i]);
      return 0;
    }