Newbie inline assembler assignment questions
Author Message
Newbie inline assembler assignment questions

I am using(trying) to use inline assembly with
microsoft vc

Say I have two structures

struct_a{
short a;

Quote:
}

struct_b{
int i;
struct_a *ptrA;

Quote:
}

And  i pass a pointer to struct_b to a C function with
inline assembly

func1(struct_b* struct)
{
inline stuff.....

Quote:
}

How can I access the elements of this array, as
c pointer notaion doesnt work eg

mov  cx,  struct->i
mov  esi, struct->ptrA

fails.

How do I access struct->ptrA->a ?

Further if struct->ptrA points to the first item in a
contiguous array of struct_a structures (allocated
via malloc) can I use pointer arithmetic so step
through them (as in C) or must you calculate the
structure size through sizeof() and 'manually' move
the pointer.

Sorry if these are basic questions

TIA

Tue, 29 May 2001 03:00:00 GMT
Newbie inline assembler assignment questions
I guess you're using 32 bit code?  ok, i'll just assume 32 bit win95,
where int's and pointers are 32 bit.

Quote:
>struct_a{
>     short a;
>}
>struct_b{
>     int i;
>     struct_a *ptrA;
>}

>How do I access struct->ptrA->a ?

mov     eax,[struc_b_ptr+4] ; eax now = struc_b->ptrA
mov     ax,[eax]        ; ax = struc_b->ptrA->a

Quote:
>Further if struct->ptrA points to the first item in a
>contiguous array of struct_a structures (allocated
>via malloc) can I use pointer arithmetic so step
>through them (as in C) or must you calculate the
>structure size through sizeof() and 'manually' move
>the pointer.

if there was an array of struct_a's, here's how you would
access the 5th one:

mov     eax,[struc_b_ptr+4] ; eax now = struc_b->ptrA
mov     ebx,5 ; let's access the 5th one
mov     ax,[eax+ebx*2] ; each struc_a is two bytes
; ax = the entry in the array(5)

This wasn't tested, but it looks like it would work.  What-
ever you put in EBX in this last example would than access the corris-
ponding entry in the array.

Dave

Tue, 29 May 2001 03:00:00 GMT
Newbie inline assembler assignment questions

Quote:

>I am using(trying) to use inline assembly with
>microsoft vc

First the operator "->" is only allowed  in C expressions,
the corresponding thing is written a different way in Asm.

Second--at least in Borland--if you define something as a fieldname
then you access it with a dot in C or Asm, but the assembler *WILL*
check that you are doing this to an object with a field of that name.

typedef   struct s1   { int bag;  int scrag;  char name[8]}  TBill;
TBill henry;
TBill *ptr;                    // .field can only be applied to
// an object whose type has such a field,
void myfunc(TBill *q){         // or to indirect-of a pointer:
asm   mov esi,q;            // esi =   pointer
asm   mov eax,[esi].bag;    // eax = (*pointer).bag
asm   mov ebx,henry.scrag;  // ebx =   henry.scrag
//etc

Quote:
};

--

Tue, 29 May 2001 03:00:00 GMT
Newbie inline assembler assignment questions

Quote:
>I guess you're using 32 bit code?  ok, i'll just assume 32 bit win95,
>where int's and pointers are 32 bit.

yup.
Thanks for you reply - you are explicitly moving the data around
in your examples where I was hoping to use the field names of the
structure items. I hoped this might be quicker - no moving to esi -
and easier. But I got some problems - see the other thread.

Anyway I sort of :) got your method working. In my actual structure
the pointer is offset 15 bytes not 4, and when I query this by

Quote:
> mov eax,[struc_b_ptr+4] ; eax now = struc_b->ptrA

eax is filled with the ptr memory but padded with two zero bytes.
If i fill the structure to make the offset 16 it works OK.
I thought the compiler was clever enough to handle boundary
crossing ? (if that is what the problem is).

I havent had a chance to look at the array processing bit - but youll be the
first to know if I have any problems :)

Quote:
> mov ax,[eax] ; ax = struc_b->ptrA->a

> if there was an array of struct_a's, here's how you would
>access the 5th one:

Wed, 30 May 2001 03:00:00 GMT
Newbie inline assembler assignment questions
I am using MSVC on win32.

Hmm, the trouble I have here is that i f I have another structure
with 'bag' defined (which is not unreasonable) then

Quote:
>   asm   mov eax,[esi].bag;    // eax = (*pointer).bag

fails at compile time with

error C2410: 'bag' : ambiguous member name in 'second operand'

Also why does henry.scrag work but more generally q.scrag compile
OK but give garbage ?
( It would be nice to try and use the pointer in that
way you dont need to know the specific instance of the variable -( Im
assuming
moving the pointer to esi is slower than just using the . operator))

Quote:
>typedef   struct s1   { int bag;  int scrag;  char name[8]}  TBill;
>TBill henry;
>TBill *ptr;                    // .field can only be applied to
>                               // an object whose type has such a field,
>void myfunc(TBill *q){         // or to indirect-of a pointer:
>   asm   mov esi,q;            // esi =   pointer
>   asm   mov eax,[esi].bag;    // eax = (*pointer).bag
>   asm   mov ebx,henry.scrag;  // ebx =   henry.scrag
>   //etc
>};

Wed, 30 May 2001 03:00:00 GMT
Newbie inline assembler assignment questions

writes

Quote:
>I am using MSVC on win32.

>Hmm, the trouble I have here is that i f I have another structure
>with 'bag' defined (which is not unreasonable) then

>>   asm   mov eax,[esi].bag;    // eax = (*pointer).bag

>fails at compile time with

>    error C2410: 'bag' : ambiguous member name in 'second operand'

>Also

you want is    mov eax,[(TBill *)esi].bag   i.e. a typecast
but I'm not sure if/how this can be done.

Quote:
>why does henry.scrag work but more generally q.scrag compile
>OK but give garbage ?

Why is it garbage?  because q does not have a field called scrag,
only (*q) has a field called called scrag.  Why does it compile OK?
it shouldn't; I don't think it does in Borland

Quote:
>( It would be nice to try and use the pointer in that
>way you dont need to know the specific instance of the variable -( Im
>assuming
>moving the pointer to esi is slower than just using the . operator))

The problem you have is that assembler statements specify single
machine instructions.  You might want to do    MOV ESI,[[MyPtr]+disp1]
but sadly you can't:  you have to break it down into
MOV ESI,[MyPtr];   MOV EAX,[ESI+disp1].

It isn't slower....it's the only way to do it.

--

Wed, 30 May 2001 03:00:00 GMT
Newbie inline assembler assignment questions

Quote:
>I am using MSVC on win32.

>Hmm, the trouble I have here is that i f I have another structure
>with 'bag' defined (which is not unreasonable) then

>>   asm   mov eax,[esi].bag;    // eax = (*pointer).bag

>fails at compile time with

>    error C2410: 'bag' : ambiguous member name in 'second operand'

That's because VC doesn't know which structure type is being accessed by
[esi].  So it can't choose the correct offset for bag.

Quote:
>Also why does henry.scrag work but more generally q.scrag compile
>OK but give garbage ?

henry.scrag works correctly because henry is static (with extern scope)
and is a struct (it is *not* a pointer).

Quote:
>( It would be nice to try and use the pointer in that
>way you dont need to know the specific instance of the variable -( Im
>assuming
>moving the pointer to esi is slower than just using the . operator))

The problem is if you want to use a pointer, you need indirection.  And
the nonhacking indirection method on the x86 architecture requires using a
register.  Limited indirection from memory for JMPs and CALLs is allowed--
but for general data fetches, you need to put the pointer in a register.
That's the way it is on an x86.
If there is only one structure, you can make the structure static and

Quote:
>>typedef   struct s1   { int bag;  int scrag;  char name[8]}  TBill;
>>TBill henry;
>>TBill *ptr;                    // .field can only be applied to
>>                               // an object whose type has such a field,
>>void myfunc(TBill *q){         // or to indirect-of a pointer:
>>   asm   mov esi,q;            // esi =   pointer
>>   asm   mov eax,[esi].bag;    // eax = (*pointer).bag
>>   asm   mov ebx,henry.scrag;  // ebx =   henry.scrag
>>   //etc
>>};

Henry S. Takeuchi

Seattle, Washington (USA)

Thu, 31 May 2001 03:00:00 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages