C == Assembly

March 4, 2007

Last month John Gruber asked the question “Is C the new assembly?”

It’s a tough question – what, exactly, does ‘the new assembly’ mean? The first things that pop into my head when I think of assembly language are these:

  1. Fast (or believed to be so)
  2. Ugly and difficult
  3. Nonportable
  4. Foundation for other languages

C has all of these attributes. (And more!) But does it have these attributes in such a way that it can be called ‘the new assembly’? I think so.
Fast
Ever since the development of higher-level-langauges, programmers have tended to view assembly language as a Tool of Last Resort for when you need something to run blindingly fast. Though assembly language retains its place in the toolboxes of many programmers, it’s hardly the first thing which John Q. Coder will reach for to solve a problem. But thanks to the years of tweaks and optimizations made by truly intelligent people to gcc, C (under gcc) is the fastest programming language. Though several languages go faster in the right situations, “C” is nonetheless the byword for “fast” among high-level languages, like it or not.

Ugly and unpleasant

You can argue about why C is ugly, difficult, and unsafe all you like, but the fact remains – as jwz says, “C has all the expressive power of two dixie cups and a string.” When compared to the D language (which is as fast, if not faster), it’s hideous. There are no auto-expanding array types (even FØRTRAN had auto-expanding lists!) which makes it extremely difficult to create proper data structures without the use of pointers.

C’s string support is laughable – how in blazes are we supposed to use a language that doesn’t automatically understand that strings a) are more than just an array of one-byte characters and b) have an end? A well-crafted preprocessor statement stuck in the middle of some otherwise innocuous code can bring an entire codebase to its knees.

You can quantify a language’s simplicity of syntax by looking at the recommended coding standards. Python’s can be boiled down to “use clear_variable_names instead of bd_1s, don’t scre w a ro u n d with whites pace, and keep the documentation up to date.” C’s, on the other hand, are labyrinthine tomes full of references to implicit ints and nested includes.

Nonportable

The old axiom is true – C combines all the elegance and power of assembly language with the portability of assembly language. Here’s a fun idea for a drinking game:

  1. Take a look through any reasonably complicated cross-platform and/or cross-architecture C library.
  2. Take a drink every time you see one of these phrases:
    • “hack to make this work on $PLATFORM/$PROCESSOR”
    • “$PLATFORM doesn’t understand $NOUN, so we have to do this”
    • #if defined ($PLATFORMNAME) // $PROFANITY
  3. Repeat steps 1 and 2 until insulin shock sets in.

And don’t forget the lovely fact that arguments to a function are evaluated in an order that depends on a) the compiler b) the compiler version and c) the processor architecture. One would think that the order of evaluation of the arguments to a function would be a critical part of a specification. One would be unpleasantly surprised to discover that this is not the case.

Foundation

The majority of high-level languages realize that using the wheel which C has “perfected” (I use the term in its loosest possible sense) is an acceptable compromise – after all, why not shift the responsibility for ensuring cross-platform compatiblity to the interpreter/compiler, not the programmer? The four programming languages with which I am most familiar – Ruby, Python, Objective-C and Java – are either based on or a superset of C.

Conclusion

Now, in the days of AJAX, Vista, OS X and Beryl/Compiz, assembly language is nearly forgotten. (I’ll leave discussions as to whether or not that is a good thing to people who know what they’re talking about.) Perhaps in the future C will be similarly forgotten; after all, Boo is based on Python.

Won’t it be strange if in twenty years we’re arguing over whether Python is the new C?

Strange, yes; unpleasant, no.

Entry Filed under: assembly, c, code, rant, unfounded-conjecture. .

13 Comments

  • 1. Dark Templar  |  March 4, 2007 at 10:02 pm

    Hiya Patrick!
    It’s Jack here. Nice blog you’ve got.
    Havn’t read much but have you ever used Runtime Revolution or the Torque Game Builder?I’ve just started dabling in the TGB but i used runrev for a while til i be came more practical to use that god-forsaken, decrepit hunk o’ junk known as Visual Basic
    Also, a personal favour… can u make me an anagram too?

    Thats all. Tell the family we all love them also tell ur sis our b-days coming up!
    thanks

    Jack

  • 2. sdpurtill  |  March 5, 2007 at 6:46 am

    Hey Patrick,

    I’m lovin your blog, you really inspire me to go deeper into actual programming. I’m a HS Senior and I learned Flash/ActionScript starting 5 years ago. Looking back, it was a terrible decision, but I will always love Flash. Only recently have I jumped ship and dived into Python and Ruby; I am going to learn Lisp here soon too. The beauty of programming languages is they are all relatively the same once you have the mindset of a programmer. Anyways, keep it up. I subscribe to your feed and enjoy knowing that there are other high school geeks alive :D … Do you know what college you are going to?

  • 3. Alex  |  March 5, 2007 at 10:04 am

    “And the majority of C libraries are divided into various forks so that they’ll compile under various different compilers, since years of tweaking and fiddling with the internals of compilers has rendered them completely incompatible”

    Please cite your sources. This does not reflect my experience.

    Anyway, we have C++ these days which is infinitely superior to C for most tasks.

  • 4. Aaron Griffin  |  March 6, 2007 at 4:03 am

    If you think C is non-portable, you’re not talking ISO standards. You’re talking about some platform cruft you probably wrote and couldn’t get to compile somewhere (“oh noes OpenProcess doesn’t work on linux! stupid C!”). I know for a fact that and C99 program will compile on hundreds of architectures.

    And regarding you “hack for $PLATFORM” and you D fanboy-ism… it appears you forgot to notice the names of these packages:
    http://sourceforge.net/project/showfiles.php?group_id=154306&package_id=171372&release_id=484228
    Yeah, that’s D. Way to talk out your ass.

  • 5. Patrick Thomson  |  March 6, 2007 at 4:32 am

    Alex:
    Touché. When I wrote that statement I mainly had in mind the Boost C++ libraries – which, incidentally, are such a bear to compile that they’ve spawned an entire other project, boost-jam, to assure “easy” compilation. I have replaced the offending text.

  • 6. Patrick Thomson  |  March 6, 2007 at 4:52 am

    Aaron:

    Are you actually making the statement that C99 is write-once, run-everywhere? Go run a Google Code search for ‘C99 platform workaround’. I’ll wait.

    Back? Good. Those nine thousand results point to the fact that for anything more complicated then Hello World C becomes a platform-dependent language. If you want to write anything as useful or as complicated as ImageMagick, zlib or libpng, ISO (or ANSI – yay for two different standards!) C is not going to cut the metaphorical mustard.

    And some cruft that *I* wrote? If you must know, I’m porting a C library called AtomicParsley so that it plays nicely with Objective-C inside CenterStage. Much to my dismay, GOTO is alive and well inside the AtomicParsley sources.

    And about my alleged “D fanboy-ism” (which is ironic, considering the fact that I’ve only dabbled in D and am a lukewarm advocate for it at best), apparently you missed my paragraph entitled “Foundation”. Go up and read it again – it’s the one in which I say that COMPILERS, not CODERS should bear the responsibility of making code cross-platform.

    Before flaming me accusing me of talking out of my ass – on a blog which you don’t, after all, have to read – please give my articles more than a cursory glance.

  • 7. Alex  |  March 6, 2007 at 11:53 am

    Boost is not a multitude of forks. And boost.build merely solves the issue of actually invoking the compiler and linker tools relevant to your platform. Boost is dark magic though, just as the implementation of CPython is for most people. The platform specific hacks involved in it are not the problem of the client code that depends on it.

    C99 is as capable of being write once run anywhere as any other language that makes claims to being cross platform.

    All this talk of cross platform compatibility is conflating the issues.

    When you write code in Python you are essentially just linking together many external libraries. In C/C++ there are many platform specific libraries that people often make the mistake of using and then expecting the result to be platform agnostic. If I build a Python extension for PPC and then expect it to work under Intel is this Pythons fault, C’s fault, or my own?

    Certainly at some point all languages that run under multiple platforms *require* platform specific hacks. Any ANSI standard C/C++ compiler will compile ANSI standard C/C++. This is the whole point of it being a standard. Unfortunately there are vast swathes of the language that live in a realm known as “undefined behaviour.” Alas far too many developers do not build their code with sufficient error checking enabled in their compiler, and do not fix all compile errors, and generally rely on platform specific functionality.

    This though is not a language failure, it is due to compiler authors for not being strict enough and the developers themselves for not knowing enough about their language of choice.

  • 8. Patrick Thomson  |  March 6, 2007 at 12:12 pm

    Alex:
    I agree with your statements.

    For whatever reason – coders, compilers, or the language – C programs have to resort to platform/compiler-specific hacks. That, I believe, is a sign that C programs are difficult to port.

    But thanks for reading my blog!

  • 9. Alex  |  March 6, 2007 at 1:31 pm

    What I was trying to state was that *all* programs have to resort to platform specific hacks, who has to write them depends on your library choices. It isn’t a language issue at all, it’s a code reuse one.

    Java, Python, Ruby et al all reuse C libraries which are no doubt crammed full of platform specific code branches. There is nothing preventing a similiar level of abstraction being achieved in C++ for example. And this is in fact exactly the point of libraries such as Boost and GUI toolkits like Qt.

    Assembly is inherently not cross platform. You are just writing raw code for a specific architecure when you write in assmebler.

    It’s not about how difficult to port it is. (Certainly C and C++ *can* be difficult to port, and to work aroudn various compiler bugs however. You still don’t need to do this yourself very often though.) If you are porting your code it isn’t cross platform. The bit you port is platform specific bit, perhaps the GUI code to provide a more OS Xy experience for example. This is usually because there are functional differences on how the program is interacted with by the users of those systems, on those systems. If your code is ANSI compliant, does not rely on libraries that do not work on any of your target systems or any undefined behaviour introduced through bad coding and compiler defaults, which part of the system is platform specific?

    I really don’t understand how C or C++ can not be thought of as cross-platform and I actually think that comparing C to assembly on these points is dangerously misleading as it casts into the same category of dark magic as assmebly is often attributed. Then again, perhaps as you are a C programmer you like the mystical air that might lend you? ;)

  • 10. noz3001  |  March 7, 2007 at 12:07 am

    Assemble is only better than C/C++ when it comes to speed and file size. Otherwise, C is much easier and much more useful.

  • 11. Mexxrp  |  April 10, 2007 at 10:21 pm

    test.com

  • 12. Idetrorce  |  December 15, 2007 at 11:32 pm

    very interesting, but I don’t agree with you
    Idetrorce

  • 13. CodeAssembly  |  August 22, 2008 at 11:37 am

    C++ is crossplatform, but you can’t create windows, buttons etc in the same way for linux, windows or mac, they have different api’s.
    For these problems you can use something like http://www.wxwidgets.org wich wraps all these different api’s into one.
    And C can even by compiled for PIC microcontrollers, isn’t that portable ?.
    All you need is a compiler for that platform, the diferences lies in libraries which, like in wxwidgets case can be wrapped into a single api.


About Me



I'm Patrick Thomson. I'm a sophomore at George Washington University, passionate about technology, Apple, and programming in Cocoa, Python, Ruby, and Nu.

Why "important shock"? It's an anagram of my name.

Meta

Blogroll

a

Top Posts

Blog Stats