Why do i get access violation at mov [edx], al?

1k views Asked by At

I write this code in Visual Studio, but i don't know why it get access violation at mov [edx], al. Can anyone help me to explain it?

int main()
{
    char z[10] = "banana";
    _asm
    {
        lea     ecx, z;
        mov     edx, [ecx];
        inc     edx;
        mov     [ecx], edx;
        dec     edx;
        mov     al, 31;
        mov     [edx], al;
        mov     eax, 31;
        and     eax, 0FFh
    }
}
1

There are 1 answers

1
rkhb On BEST ANSWER

EDX holds the first 4 bytes of z (mov edx, [ecx]) not the address of z. If you "cast" EDX to an address with square brackets you point to anywhere but surely not to correct memory. I can't show a working code because I don't know what you want do.

Looking at your last post I guess you wanted to do something like this:

#include <stdio.h>

__declspec (naked) char khanh_asm (char a, char** b)
{
    _asm
    {
        mov     ecx, [esp+8]
        mov     edx, [ecx]
        inc     edx
        mov     [ecx], edx
        dec     edx
        mov     al, [esp+4]
        mov     [edx], al
        mov     eax, [esp+4]
        and     eax, 0FFh
        retn
    }
}

int main( void )
{
    char b[] = "banana";
    char* P = b;

    printf("%s \n", b);

    khanh_asm ( '1', &P);
    printf("%s \n", b);

    khanh_asm ( '2', &P);
    printf("%s \n", b);

    return 0;
}