ravelinx ravelinx - 17 days ago 6
C Question

Change pointer to an array that is contained in a struct using inline assembly ia32

So I have this code in C, basically is a struct that contains a pointer to an array and a function that changes the value of the array in the index 0

typedef struct archivo
{
unsigned char *info;
} FILE;

void funct(FILE *arch) {
arch->info[0] = 'b';
}


So I have to translate the funct function to inline assembly. I tried doing the following

void funct(FILE *arch) {
_asm {
mov eax, 0
mov ebx, arch.info[eax]
mov ecx, [ebx]
mov ecx, 'b'
}
}


But when I print the output, the info continues to have the same value.
So I try the output with the following code

void funct(ARCHIVO *arch)
{
printf("\n--%c--\n", arch->info[0]);

_asm {
mov eax, 0
mov ebx, arch.info[eax]
mov ecx, [ebx]
mov ecx, 'b'
}

printf("\n--%c--\n", arch->info[0]);
}


and I get as output

--P--

--P--


How can I manage to change the C array value in position 0?

Answer

you can use

void funct(FILE *arch) {
    _asm{
        mov eax, arch
        mov eax,[eax]archivo.info
        mov byte ptr [eax],'b'
    }
}

or better

__declspec(naked) void __fastcall funct2(FILE * /*arch*/) {
    _asm{
        mov eax,[ecx]archivo.info
        mov byte ptr [eax],'b'
        ret
    }
}