Re: A89: Re: "Transfer" of values between C and ASM


[Prev][Next][Index][Thread]

Re: A89: Re: "Transfer" of values between C and ASM




>You are both correct and incorrect.  C can be just as fast as ASM provided
>1) the compiler is efficient
>2) the programmer _knows_ the compiler and all it's faults
>
>A real programmer understands where the compiler will make slow or
inefficient
>code, and shore it up with ASM as necessary - or modify the compiler.
>
>Please note that GCC is a very good compiler.  It has been modified many
many
>times in order to make it very efficient.  The nice thing is the source
code is
>open so if you see where it generates bloated code, you can adjust it to
make
>tighter code.
>
>C programs, properly coded (without any asm) can and do run as fast as
programs
>made completely in asm.

No, C can _appear_ as fast as ASM (to the point where the user will only
notice a minimal difference), but if the ASM code is written well enough it
will always run faster.  Think of it this way: worst case scenario, the ASM
code and the compiled C are exactly the same.  I'll guarantee you that the C
portion of the C program (and not inline ASM) can be optomized even
slightly.  For instance, GCC would output the following code:

    jsr userlib::idle_loop    ;Just picking a library/ROM call
    rts

when anyone writing in assembly can simply use:

    jmp userlib::idle_loop

Also, the more inline ASM the program contains, the less optomized the C
portion will be, as the C compiler is unable to realize the results output
in the registers - even if they're always the same - and will instead
re-load values into each register it uses and not exploit the output.  And
there are always some other little tricks -- I remember seeing a z80 C
compiler which, when the output was disassembled, showed:

    load a,0

when, by using:

    xor a

it could save a byte and several clock cycles.  While these minor changes
may not be enough to make a C program appear slower, it is.  The advantage
of C is that it makes a program more easily "portable" between two different
platforms, instead of requiring a total rewrite, and that it's just "easier"
to program than ASM and leads to shorter program development times.

FYI, try out the "Hello World" examples output by GCC and pure ASM - the C
output is _NOT_ faster OR smaller.  Are you saying that by replacing some of
the lines with inline ASM, the C program will be equivalent to the ASM one?
Which lines should we replace?  It seems that the more lines we replace with
inline ASM, the closer the programs are - try it yourself.

If you still think that a C program can be faster, here's a challenge: write
a C program - no more than half inline ASM - and not too simple nor too
complex (in terms of time it would take to write it in ASM) and someone on
this list can - will - create a smaller or faster version in ASM.  I'm not
even specifying the type of program - it would be a lot easier for me if I
told you to write a program that allows a ball to bounce around the screen
in full 360-degree movement and allow the user to turn the ball left and
right in under 846 bytes, my most recent optomized accomplishment.

    -Scott
     (I missed this list =)



Follow-Ups: