Re: How function works internally



Op 28 Sep 2006 19:01:22 GMT schreef Chris Torek:

[The context, which the poster below snipped, was questions about
the internal mechanisms of C runtime systems, with particular
respect to function calls.]

In article <1159436717.246591.312020@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
<raxitsheth2000@xxxxxxxxxxx> wrote:
learn(and understand) any x86 based assembly lang.

While there is nothing *wrong* with learning any particular assembly
language, it is a huge mistake to learn *only* x86 assembly and
assume that this is the way "everything" works. In particular,
the x86 is largely based on 1970s-era architectures (specifically
the Intel 4004, 8008, and 8080).
I beg to differ, x86 carries a lot of 8080, less of 8008 any most nothing
of the 4040. You could then say _every_ CPU carries a lot of the 4040, be
it 6502, 6800, 68K, because they are IC's. I don't buy that.
x86 implementations carry a huge
burden of "backwards compatibility" with a system optimized for
the conditions that held at the time. It is true that, through
great cleverness, modern CPUs that implement the IA32 architecture
squeeze tremendous performance from this klunky instruction set;
but the instruction set itself remains klunky nonetheless.

Compare, for instance, the call sequence:

/* compute three parameters, then: */
push parm3 /* write 3rd parameter to RAM, adjust stack pointer */
push parm2 /* write 2nd parameter to RAM, adjust stack pointer */
push parm1 /* write 1st parameter to RAM, adjust stack pointer */
call func
addl $12,%esp /* remove 12 bytes of parameter from stack */

with:

/* compute three parameters directly into arg registers */
call func
/* no stack adjustment required */

This has nothing to do with processors, the first is (perhaps was) the C
way, because this language has variadic functions, Pascal (the second type)
does not, here the called function always knows the number of parameters
and can discard them. I don't know how a printf is implemented at the
moment, perhaps that is changed. This same schema could easily done on a
68K.
<skipped more ranting>
Leave the CPU-bashing to their own news groups. It's very OT here.
--
Coos
.