Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Inline assembly and gcc

Author: Robert Hyatt

Date: 08:08:58 05/21/04

Go up one level in this thread


On May 21, 2004 at 10:42:12, Anthony Cozzie wrote:

>On May 21, 2004 at 10:29:06, Christophe Drieu wrote:
>
>>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
>>Hi, here is what i get with -S:
>>
>>/APP
>>	incl _a
>>	movl _a,%eax
>>	movl %eax,_b
>>/NO_APP
>>	movl	$LC0, (%esp)
>>	movl	_b, %eax
>>	movl	%eax, 8(%esp)
>>	movl	_a, %eax
>>	movl	%eax, 4(%esp)
>>	call	_printf
>>	movl	$4, %edx
>>	movl	%edx, _a
>>/APP
>>	incl _a
>>	movl _a,%eax
>>	movl %eax,_b
>>/NO_APP
>>	movl	$LC0, (%esp)
>>	movl	_b, %eax
>>	movl	%eax, 8(%esp)
>>	movl	$4, %eax        Why load 4 in eax and not _a ???

Probably the compiler thinks _a is not changed.  It is faster to load the
immediate value 4 than to go back to memory to get the value, when the compiler
doesn't realize _a has been modified.  Check out "clobber list" in the inline
instruction pages...


>>	movl	%eax, 4(%esp)
>>	call	_printf
>
>I've never anyone trying to use a variable with _ before.  Usually you pass in
>the variables explicitly (see link).
>
>anthony



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.