Author: Bo Persson
Date: 08:04:11 05/21/04
Go up one level in this thread
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 ???
Just a guess: The compiler doesn't understand exactly what each assembly
instruction does, or doesn't know that _a is the same as a.
Your code is:
a = 4;
// some strange assembly (not using a)
print(a)
Even a reasonably stupid compiler realizes that this can be replaced by
print(4)
Bo Persson
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.