For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > VisualC's inline assembly failes to acess the array element









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author VisualC's inline assembly failes to acess the array element
bear

2005-06-10, 4:04 am

The code is like this
#include <stdio.h>
int main()
{
int *array;
array=(int *)malloc(100*sizeof(int));
__asm
{
mov array[1 * TYPE int], 0;
}
printf("array[1]=%d",array[1]);
free(array);
return 0;
}
The code is compiled with VC.net 2003 and it cannot gives the right answer
which is array[1]=0. Why?
lallous

2005-06-10, 8:59 am

Hello

Try accessing as:

mov eax, array
mov [eax+1 * type int], 0

> mov array[1 * TYPE int], 0;

The "mov array[1*type int], 0" was generating bad code to access the array.
I don't know the correct syntax in the inline assembler, to cause VC to
generate correct code.

--
Elias


"bear" <bear@discussions.microsoft.com> wrote in message
news:14588647-17E1-40C2-BAB5-CB0D4F2CA902@microsoft.com...
> The code is like this
> #include <stdio.h>
> int main()
> {
> int *array;
> array=(int *)malloc(100*sizeof(int));
> __asm
> {
> mov array[1 * TYPE int], 0;
> }
> printf("array[1]=%d",array[1]);
> free(array);
> return 0;
> }
> The code is compiled with VC.net 2003 and it cannot gives the right answer
> which is array[1]=0. Why?



bear

2005-06-10, 8:59 am

Still not work...
The compiler gives the assembly code, which load [array] to eax. See:
int main()
{
00411A30 push ebp
00411A31 mov ebp,esp
00411A33 sub esp,0CCh
00411A39 push ebx
00411A3A push esi
00411A3B push edi
00411A3C lea edi,[ebp-0CCh]
00411A42 mov ecx,33h
00411A47 mov eax,0CCCCCCCCh
00411A4C rep stos dword ptr [edi]
int *array;
array=(int *)malloc(100*sizeof(int));
00411A4E push 190h
00411A53 call @ILT+500(_malloc) (4111F9h)
00411A58 add esp,4
00411A5B mov dword ptr [array],eax
__asm
{
mov eax, array;
00411A5E mov eax,dword ptr [array]
mov [eax + 1 * TYPE int], 0;
00411A61 mov byte ptr [eax+4],0
}
printf("array[1]=%d",array[1]);
00411A65 mov eax,dword ptr [array]
00411A68 mov ecx,dword ptr [eax+4]
00411A6B push ecx
00411A6C push offset string "array[1]=%d" (4240C8h)
00411A71 call @ILT+1170(_printf) (411497h)
00411A76 add esp,8
free(array);
00411A79 mov eax,dword ptr [array]
00411A7C push eax
00411A7D call @ILT+1215(_free) (4114C4h)
00411A82 add esp,4
return 0;
00411A85 xor eax,eax
}

"lallous" wrote:

> Hello
>
> Try accessing as:
>
> mov eax, array
> mov [eax+1 * type int], 0
>
> The "mov array[1*type int], 0" was generating bad code to access the array.
> I don't know the correct syntax in the inline assembler, to cause VC to
> generate correct code.
>
> --
> Elias
>
>
> "bear" <bear@discussions.microsoft.com> wrote in message
> news:14588647-17E1-40C2-BAB5-CB0D4F2CA902@microsoft.com...
>
>
>

Tom Widmer

2005-06-10, 8:59 am

bear wrote:
> Still not work...
> The compiler gives the assembly code, which load [array] to eax.


This does it:
mov eax, array
mov dword ptr [eax + 1*TYPE int], 0

Tom
Tim Roberts

2005-06-11, 8:58 am

"bear" <bear@discussions.microsoft.com> wrote:
>
>The code is like this
>#include <stdio.h>
>int main()
>{
> int *array;
> array=(int *)malloc(100*sizeof(int));
> __asm
> {
> mov array[1 * TYPE int], 0;
> }
> printf("array[1]=%d",array[1]);
> free(array);
> return 0;
>}
>The code is compiled with VC.net 2003 and it cannot gives the right answer
>which is array[1]=0. Why?


Because "array" is not an array. It is a pointer. The address called
"array" is exactly 4 bytes long, and contains an address. The x86
instruction set does not support double-indirect addressing modes.

Tom's answer was the correct one. You need to fetch the pointer into a
register, and store the value as an offset to that register.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com