Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Inline assembly and gcc

Author: Robert Hyatt

Date: 08:07:04 05/21/04

Go up one level in this thread


On May 21, 2004 at 09:23:01, Anthony Cozzie wrote:

>On May 21, 2004 at 09:15:36, Christophe Drieu wrote:
>
>>Hi, i'm learning Inline assembly with gcc, but have some trouble with global C
>>variables:
>>
>>int a=4, b=0;
>>
>>int main()
>>{
>>	asm("incl _a");		// a++
>>	asm("movl _a,%eax");	// eax=a
>>	asm("movl %eax,_b");	// b=eax
>>
>>	printf("a=%d b=%d\n", a, b); // print a=5 b=5
>>
>>	a=4;
>>
>>	asm("incl _a");		// a++
>>	asm("movl _a,%eax");	// eax=a
>>	asm("movl %eax,_b");	// b=eax
>>
>>	printf("a=%d b=%d\n", a, b); // print a=4 b=5
>>}
>>
>>My question is, why a==4 in second case and not 5 ? Thank you.
>
>
>Dunno if this is the issue, but registers are not guaranteed to be preserved
>between asm calls.
>
>gcc -S will show you the actual assembly that is generated - I'd take a look at
>that.
>
>This is my personal inline assembly reference:
>
>http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html
>
>anthony


He also needs to study the gcc "clobber list".  His asm changes memory.  The
compiler might not realize that and use a previous value of _a that was kept in
a register across the asm.  He also needs to indicate that he is clobbering the
registers he modifies so that the compiler will know they are changed after the
asm...



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.