Prev: IDE
Next: New NBASM IDE
From: jacob navia on
Seebs a �crit :
> On 2010-03-13, Nathan <nathancbaker(a)gmail.com> wrote:
>> So we know that "return N;" is guaranteed to set the contents of EAX.
>
> No, we don't.
>
> We know that it did in a particular function compiled with a particular
> compiler with particular settings for a particular target. We don't
> know that it would always do the same even for this function -- it might
> compile differently if it were compiled along with particular other
> functions, or with different compiler flags, or...
>
> In short, no, it is not "guaranteed".
>
> -s

I have never seen any compiler unde the Intel architecture that
doesn't place the return value (when there is one of course, and
when it fits 32 bits) in the EAX register.

Can you name a compiler or a combination of compiler+flags
that would place the return value in another register?

From: jacob navia on
Nathan a �crit :
>
> Before we declare this "successfully replace" status, I think we
> should put it to a test.
>
> int main(void)
> {
> unsigned ebx;

Here you declare a local variable called "ebx"

> unsigned ecx;
>

Another one

> ebx = 5;

You assign 5 to the local variable ebx

> for (ecx = 0; ecx < 5; ecx++)
> {
> ebx++;
> }
>
> return 42;
> }
>
> $ gcc -c -o asm.o asm.c
> $ objdump -d asm.o
>
> asm.o: file format elf32-i386
>
>
> Disassembly of section .text:
>
> 00000000 <main>:
> 0: 55 push %ebp
> 1: 89 e5 mov %esp,%ebp
> 3: 83 ec 10 sub $0x10,%esp

Make space for 2 local variables, "ecx" and "ebx". Since the
compiler wants to keep the stack aligned, it subtracts 16
instead of just 8. ebx will be at -4 from the start of the
frame, and "ecx" will be at offset -8

> 6: c7 45 fc 05 00 00 00 movl $0x5,-0x4(%ebp)
Assign 5 to "ebx"
> d: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%ebp)
Assign zero to "ecx"
> 14: eb 08 jmp 1e <main+0x1e>
goto StartOfLoop
Increment counters:
> 16: 83 45 fc 01 addl $0x1,-0x4(%ebp)
> 1a: 83 45 f8 01 addl $0x1,-0x8(%ebp)
StartOfLoop:
> 1e: 83 7d f8 04 cmpl $0x4,-0x8(%ebp)
if "ecx" is smaller or equal to 4 goto IncrementCounters
> 22: 76 f2 jbe 16 <main+0x16>
return 42
> 24: b8 2a 00 00 00 mov $0x2a,%eax
Now we destroy the frame
> 29: c9 leave
return
> 2a: c3 ret
>
> Dang! I'd say this here new-fangled "C" assembler doesn't know its
> ASM from a hole in the ground. :)
>
> Nathan.
> [ a.l.a added ]

You do not know what you are talking about obviously.
You think that "magically" a variable called
"ecx" will be assigned to register "ecx"?
From: Ike Naar on
In article <12a5f182-3b60-4a34-968e-e82078961c5c(a)g28g2000yqh.googlegroups.com>,
Nathan <nathancbaker(a)gmail.com> wrote:
>On Mar 13, 12:06�am, "H. Peter Anvin" <h...(a)zytor.com> wrote:
>> On 03/12/2010 08:05 PM, Nathan wrote:
>>
>> > � � return 42;
>> > }
>>
>> 00000000 <main>:
>> � �0: � b8 2a 00 00 00 � � � � �mov � �$0x2a,%eax
>> � �5: � c3 � � � � � � � � � � �ret
>
>So we know that "return N;" is guaranteed to set the contents of EAX.
>Are there any guaranteed methods of setting EBX?

No; the EAX register might have gone out to lunch; in that case,
a replacement register is used:

return 42;
}
main:
/* 000000 2 */ retl ! Result = %o0
/* 0x0004 */ or %g0,42,%o0

Here, %o0 is used to hold the return value.
From: Ike Naar on
In article <hnfs0b$2a2$2(a)speranza.aioe.org>,
jacob navia <jn(a)nospam.org> wrote:
>Seebs a �crit :
>> On 2010-03-13, Nathan <nathancbaker(a)gmail.com> wrote:
>>> So we know that "return N;" is guaranteed to set the contents of EAX.
>>
>> No, we don't.
>>
>> We know that it did in a particular function compiled with a particular
>> compiler with particular settings for a particular target. We don't
>> know that it would always do the same even for this function -- it might
>> compile differently if it were compiled along with particular other
>> functions, or with different compiler flags, or...
>>
>> In short, no, it is not "guaranteed".
>
>I have never seen any compiler unde the Intel architecture that
>doesn't place the return value (when there is one of course, and
>when it fits 32 bits) in the EAX register.
>
>Can you name a compiler or a combination of compiler+flags
>that would place the return value in another register?

Here's an example of a 32 bit value that is not returned in EAX:

return 42;
}

00000004 <_main>:
4: 83 ec 0c sub $0xc,%esp
7: e8 f4 ff ff ff call 0 <___gnu_compiled_c>
8: DISP32 ___main
c: d9 05 00 00 00 00 flds 0x0
e: 32 .text
12: 83 c4 0c add $0xc,%esp
15: c3 ret
From: Branimir Maksimovic on
On Sat, 13 Mar 2010 11:43:34 +0000 (UTC)
ike(a)localhost.claranet.nl (Ike Naar) wrote:

> In article <hnfs0b$2a2$2(a)speranza.aioe.org>,
> jacob navia <jn(a)nospam.org> wrote:
> >Seebs a écrit :
> >> On 2010-03-13, Nathan <nathancbaker(a)gmail.com> wrote:
> >>> So we know that "return N;" is guaranteed to set the contents of
> >>> EAX.
> >>
> >> No, we don't.
> >>
> >> We know that it did in a particular function compiled with a
> >> particular compiler with particular settings for a particular
> >> target. We don't know that it would always do the same even for
> >> this function -- it might compile differently if it were compiled
> >> along with particular other functions, or with different compiler
> >> flags, or...
> >>
> >> In short, no, it is not "guaranteed".
> >
> >I have never seen any compiler unde the Intel architecture that
> >doesn't place the return value (when there is one of course, and
> >when it fits 32 bits) in the EAX register.
> >
> >Can you name a compiler or a combination of compiler+flags
> >that would place the return value in another register?
>
> Here's an example of a 32 bit value that is not returned in EAX:
>
> return 42;
> }
>
> 00000004 <_main>:
> 4: 83 ec 0c sub $0xc,%esp
> 7: e8 f4 ff ff ff call 0 <___gnu_compiled_c>
> 8: DISP32 ___main
> c: d9 05 00 00 00 00 flds 0x0
> e: 32 .text
> 12: 83 c4 0c add $0xc,%esp
> 15: c3 ret

This is not linked I guess. Since it calls 0 and loads in fpu stack
from 0?

Greets


--
http://maxa.homedns.org/

Sometimes online sometimes not


First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: IDE
Next: New NBASM IDE