Thread: C and Assembly

  1. #1
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43

    Question C and Assembly

    Not sure whether this is the correct forum to post this query

    I am having a doubt regarding the presence of assembly code inside the linux kernel source code which mostly comprises of C code. Is there any specific reason which makes the assembly code special. With todays advanced compilers like GCC we can get a much optimized binary when the transfermation occurs with
    C -> Assembly(Machine/Object code) -> Binary
    So, why do we still have assembly code in place? Can anyone point me to any relavent links to understand this?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    There are some things you simply cannot do in C - for example on the x86 architecture, there are assembler opcodes to load/store the global descriptor table and page tables which simply have no representation in C because they're too hardware-specific. Another example would be handling interrupts - registers must be pushed onto/popped off from the stack in a specific way, and a different instruction is used to return from interrupt handlers ("iret" instead of the normal "ret"). A search for assembler/interrupts/etc. on OSDev might also bring up some other examples.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from the fact that assembler allows exploitation of hardware-specific instructions, and C doesn't, well written assembler can be very highly tuned for performance, and is under explicit control of the programmer - assuming the programmer has a strong understanding of the underlying hardware platform and its instruction set. A good quality compiler can optimise C code, but usually makes some different technical trade-offs than a programmer might choose. Compiler options aren't necessarily enough to overcome that. When the difference in performance - by whatever measure chosen - matters, as it can in core parts of an operating system or in other performance-critical code, then assembler can be the better choice. So it gets used.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Thanks JohnGraham and grumpy

    I agree on the point
    well written assembler can be very highly tuned for performance, and is under explicit control of the programmer - assuming the programmer has a strong understanding of the underlying hardware platform and its instruction set

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Write a bootloader in pure C, then you'll see the problem.

    Almost all machines need a different initialisation to get to the point where you can use C's flat memory model, etc., and it's not something that can be done by C alone. Some hardware would need, say, a particular assembly instruction executed while the processor is still in 8086 mode in order to enable memory above 640Kb, for instance. And you can't do that from pure C. And you can't run more pure C code without having done that first.

    Almost all the assembly in the Linux kernel is boot code, hardware-specific code (e.g. APIC, interrupts, etc.) or assembly-optimised versions of code (e.g. encryption algorithms) that are proven to be faster than equivalent C where that particular set of instructions are supported (and so you end up with, say, an ARM-optimised version, an MMX-optimised version, an SSE-optimised version, etc. that just replicates some basic C code, all of which are better on the machines that have those instruction sets than the best compiler can achieve from the equivalent C code).

    Tell me how you enable access to the entire 8Gb of RAM in a 64-bit x86 machine from pure C code, or how you enable protected mode (x86 processors start up in "real mode", which isn't sufficient to do ANYTHING in nowadays except bootstrap some assembly to get into protected mode - hell, the standard C library probably wouldn't fit into the memory accessible from real mode any more). You can't so such things without bootstrapping and specific assembly aimed at individual computer architectures. And that's where most of the assembly in the Linux kernel lies. That, and hand-optimised routines.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert assembly>machine code, machine code>assembly
    By wenxinleong in forum C Programming
    Replies: 12
    Last Post: 06-23-2011, 10:42 PM
  2. assembly help
    By Brw_Abhi in forum C Programming
    Replies: 19
    Last Post: 08-23-2007, 04:34 AM
  3. Assembly....
    By Victor in forum Linux Programming
    Replies: 8
    Last Post: 12-28-2004, 01:46 AM
  4. x86 assembly
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-18-2003, 02:29 AM
  5. Assembly?????
    By kas2002 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-07-2003, 02:31 PM

Tags for this Thread