Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with the movl instruction in x86 assembly

Status
Not open for further replies.

BoB78

Technical User
Jun 8, 2005
4
US
I am trying to run a simple program in x86 linux assembly, but it keeps mysteriously segfaulting, and I can't figure out why. The line of code that is causing the problem is:

movl %esi, 0x8(%esi)

In fact I ran a separate program consisting of just this one line of code (embedded in c) and it segfaults. I replaced %esi with $0x0 and it segfaults again. If I replace the entire expression with something like "movl %esi %eax" then it doesn't segfault. So clearly the problem must be with the 0x8(%esi) part. Does this mean I cannot write to the memory location 0x8($esi)?? Any suggestions on how to get around this problem?
Bob
 
1. What does ESI point to ?
Is it a valid pointer in ur program's address space.

2. While using inline assembly with GCC, u need to be
careful about few things. For exp, the COMPILER never
knows anything about the registers u r destroying.
If u dont inform GCC that u r destroying a value in
a register, GCC would not relinquish register-memory
mappings while generating code. For exp, if the compiler
had generated code such that "EBX" contains the value
of a memory location prior and after ur code, then ur
assembly instruction would b a pain in the ass. He would
spoil the entire broth
Similarly, if u update a memory location in ur
inline assembly , u need to explicitly tell GCC that
it has to purge "all" register--memory caches prior
to this assembly. An ASM implementation of
"memcpy" would need this..

So, for exp : consider this :
__asm__ ("movl %eax, %ebx");
If u insert this intrn amidst ur "C" code, it might
b catastrphic. Coz, prior and after this instrn,
GCC would have generated Code that might have a
dependency that EBX would contain some value.. Since
u r destroying it now.. u need to tell GCC explicity that
this ASM stmt would destroy EBX register.
like this: __asm__ ("movl %eax, %ebx":::"bx");
The "bx" is called a "clobber". This means that
GCC doesn't peep into ur ASM stmt and see what all
it destroys. U need to tell him explicitly. Thats
reasonable too.

IIIrly when u write into a memory location in ur
inline assembly, just tell GCC to relinquish all
register-memory mappings during Code Generation.

Thus ur new code would look like:
__asm__ ("movl %esi, 0x8(%esi)":::"memory");

Hope it helped

For more info on GCC inline =>



Sarnath
Do not rejoice that ur code works.
it might be a special case of an error :-(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top