Unix Programming - Structure offsets in GCC inline assembly for the i386

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > Structure offsets in GCC inline assembly for the i386





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 Structure offsets in GCC inline assembly for the i386
KJK::Hyperion

2004-01-23, 5:14 pm

Not strictly an UNIX question, but: how do I specify a structure member
offset in inline assembly? let's say I have a CONTEXT * loaded in EAX,
and I need to move its Eip field into ECX. So far I've come up with:

__asm__("mov %0(%%eax), %%ecx" "\n" : : "o"(offsetof(CONTEXT, Eip)) );

but this creates a variable with a value of offsetof(CONTEXT, Eip) and
tries to read into ECX from an offset of EAX from it (???). Using the
"i" constraint I get a syntax error, because instead of an offset I get
an immediate. How should I do?

While we're on topic, how do I jump to a C label or load its address? I
keep getting variables containing the offsets instead of true offsets

Thanks in advance
Valentin Nechayev

2004-01-23, 5:15 pm

>>> KJK::Hyperion wrote:

KH> Not strictly an UNIX question, but: how do I specify a structure member
KH> offset in inline assembly? let's say I have a CONTEXT * loaded in EAX,
KH> and I need to move its Eip field into ECX. So far I've come up with:
KH> __asm__("mov %0(%%eax), %%ecx" "\n" : : "o"(offsetof(CONTEXT, Eip)) );
KH> but this creates a variable with a value of offsetof(CONTEXT, Eip) and
KH> tries to read into ECX from an offset of EAX from it (???).

You can load offset to yet another register and sum base pointer and
offset. This isn't optimized, but is working.
But, all examples I've seen keeps offset values in assembler code as constant
calculated manually or using separate preprocessing phase.

KH> While we're on topic, how do I jump to a C label or load its address? I
KH> keep getting variables containing the offsets instead of true offsets

It's better to think you can't.
But if using only i386 + gcc + GNU binutils, the following will work:
asm("...; jc j2");
....
asm("j2:");


-netch-
KJK::Hyperion

2004-01-23, 5:18 pm

> You can load offset to yet another register and sum base pointer and
quote:

> offset. This isn't optimized, but is working.
> But, all examples I've seen keeps offset values in assembler code as
> constant calculated manually or using separate preprocessing phase.



blech. Anyway, the problem is that the code I'm porting uses offsets to
access local variables (I doubt we can use C variables, but I can ask),
so I really can neither hardcode offsets nor use an offset register,
because most offsets aren't offsetof(), but sums of several constants
quote:

> It's better to think you can't.



it's that I can't use scope-sensitive labels (__label__) in ASM, and
I'd like to. If it isn't possible, we'll just give up nesting, anyway
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com