Calm Down: It’s Only Assembly Language

Based on [Ben Jojo’s] title — x86 Assembly Doesn’t have to be Scary — we assume that normal programmers fear assembly. Most hackers don’t mind it, but we also don’t often have an excuse to program assembly for desktop computers.

In fact, the post is really well suited for the typical hacker because it focuses the on real mode of an x86 processor after it boots. What makes this tutorial a little more interesting than the usual lecture is that it has interactive areas, where a VM runs your code in the browser after assembling with NASM.

We really like that format of reading a bit and then playing with some code right in the browser. There is something surreal about watching a virtual PC booting up inside your browser. Yeah, we’ve seen it before, but it still makes our eyebrows shoot up a little.

We hope he’ll continue this as a series, because right now it stops after talking about a few BIOS functions. We’d love to see more about instructions, indexing, string prefixes, and even moving to code that would run under Linux or Windows. It would be nice, too, if there was some information about setting up a local environment. Now if you want to make a serious investment and you use Linux, this book is a lot to chew on but will answer your questions.

Of course, there are many tutorials, but this is a fun if brief introduction. If you want to know more about assembly outside the browser, we covered that. If you really want to write a real bootloader, there’s help for that, too.

43 thoughts on “Calm Down: It’s Only Assembly Language

  1. x86 assembly — just say no! What a wretched disaster, but with a C compiler, why bother.
    ARM assembly on the other hand is relatively civilized.

    But I am never content to use a processor that I don’t have a familiarity with at the assembly language level.
    It is all about the hacker mindset of “wanting to know what is inside the box”.

    1. Agreed, assembly is not scary, but X86 with all it’s modes, “extensions” and “features” still scare the hell out of me (even though I’ve worked with it for 15+ years).

        1. You’re misunderstanding what he says. He doesn’t say he has trouble with it, he says that it scares him. The latter is often proof of knowing exactly what you’re dealing with an not liking it one bit. Just like chemists are appropriately scared of certain substances, while novices are blissfully unaware of what they’re toying with.

    2. I suppose you will be leading the charge to replace x86 with Arm as the default desktop and server processors that everyone uses? I don’t know enough to care which one ‘wins’ but I wish one would. I miss the days when all my computers could run the same code.

      1. Yep, that would suit me fine — but I won’t be leading the charge. If it runs fast, I don’t care what is inside, and that goes for x86 at this point in time for the desktop. x86 is dead in embedded, so no worries there.

    3. >>ARM assembly on the other hand is relatively civilized.

      “Relatively” is a … um … relative term. Granted that the ARM instruction set is nicely orthogonal etc., and if you stick to the basics, it’s easy enough. But the ARM’s clock-saving features, like branching both before and after almost any instruction, make it horribly complex.

      Jack Ganssle is, without a doubt, one of the most experienced ARM experts on the planet, but he told me that he_NEVER_ tries to use ARM assembly language.

      As for which processor has the best assembly language, my vote is the 8080. When Federico Faggin introduced PUSH, POP, CALL, and RET, he changed assembly language for all time.
      Intel’s mistake — and it was a _HUGE_ one — was trying to make the x86 behave like an 8080.

    1. I’d have to say that AVR (i.e. Arduino) assembly is pretty nice. ARM assembly is OK too, particularly for the simpler cores (e.g. M0). x86 assembly seems a bit more complex to me, whenever I’ve looked at it.
      Besides, microcontrollers are where assembly is most useful.

      1. I have to agree! Assembly language has always been my first choice for the full Atmel AVR range. Their STK-500 development board was my entry into the world of microcontrollers and I remember it fondly. I chose it over Arduino for reasons I can’t clearly recall. I think it was to reduce the component count in my projects, or easily change crystal oscillators or something. Perhaps it was just on promotion.

        They had a really nice free IDE too (AVR Studio as I recall) with excellent simulation features and few bugs. At some point I felt it got a bit bloated but I kept an old version archived.

        I’ve since moved on to other platforms, but still return to it whenever I need precise timing and 20Mhz is fast enough.

  2. x86… who thought segmented memory registers was a good idea… *shudder*

    if you really want to learn assembly, go bare metal on a really simple ยต-controller, heck today most of them are more like SoC’s than just CPU’s….

    1. Like many things on the x86, these are all about backwards compatibility to ancient days. Remember that the x86 started with 8086 and 8088, with limited address space and the segment registers were a benefit, albeit nasty. Once we had something like the 386 with protected mode, they became a ball and chain. So the x86 is encumbered with all these historical artifacts. This is why the ARM is so much better for the first time assembly language learner.

      A great way to learn any assembly language is to reverse engineer disassembled code, but maybe not for the beginner. But fine for the true hacker.

    2. 8-bit or even 16-bit PIC are great platforms for learning Assembly. I started with an 8-bit PIC16 in high school, then 68000 in college, and then MSP-430 in grad school.

  3. I saw this site linked to via the
    Somebody beat me to the HackAday tips line, probably because I fell asleep (I’m lacking sleep lately) whilst having a read and a play around on the site with the built-in VM.

  4. Assembly is easy, complex data structures in assembly are hard. Most people learn x86 assembly in a couple days by reverse engineering binaries.

    Reversing compression and encryption algorithms in x86 is about as tough as it gets, and then there is things like making ROP exploits around modern GCC flags, ASLR, and NX.

  5. Learning assembly is useful for understanding how things are working at a low level. However modern compilers do a really good job and in most cases unless you are really skilled at assembly, modern compilers produce faster , more efficient optimized code than you can do by hand.

    1. Assembly vs higher-level languages is really just a question of scope: it’s easier to create complex code in higher-level languages. If your problem is essentially complex, then those are the right tools to use.

      I’m still surprised by how low the bar is to be “reasonably skilled in assembly” enough to beat a compiler. I’m no guru, but I’ve never failed to beat AVR GCC and straight C. Of course, there are tons of tricks you can take to convince the compiler to do with the C code what I would have done in assembly. But for the small, tight-loop stuff that I end up coding in assembly, its more pain to convince GCC to do the right thing than it is to just bash it out in assembly.

    1. No. 6502 assembly is not the best. Both architecturally, 8 bit index registers are a pain and made worse in that they only operate on 256 byte pages. So if you try to cross a page boundary with the index registers, it wraps around. And then there are the bugs in the instruction set…

      In at the the early documentation, how an instruction is documented as working and how it actually works can be two different things. Not greatly different, but can be fun to figure out. To add insult to injury, some of those were :”fixed” in later versions of the processor. So your code might not be portable.

      Nope, if you want a better architecture, go with the AVR microcontrollers as others have noted. More registers, mostly orthogonal instructions and less bugs.

      1. I’m not going to argue whether the 6502 is great or not, it has if flaws and benefits just like any other processor. But a fact is that it is a very well known/widely used processor. The fact that we are still talking about it more then 40 years after it’s first production run is in many ways very interesting.

        The 8bit indexed registers aren’t a pain at all. Instead, they can be very practical for small tables.
        The wrapping around you mention ONLY OCCURS when you are working in zero-page memory. When you work in the area above 0x0100 the wrapping does not occur. But the instruction does take an extra cycle to complete when a page-boundry is crossed, a small penalty that can be avoided.

    2. Yes, wrote a 29 byte program to read a keyboard (88 keys), play the notes polyphonically, while recording them in mrmory and playing other notes previously stored in memory. All on a 6502 development board that had a hexidecimal keypad to enter address/instrution. In the ’70’s. All real time with a 1 MHz colck and 1 k memory. But assembly is not what happens inside the processor – microcide translates the assembly instructions into the actual electronics of what happens. Unfortunately, microcode is usually hardwired so not available.

  6. Learning assembly language is a bit like learning to drive by building the car first.
    The best part about learning assembly language is when you realise how grateful you are for compilers when you want to throw a piece of code together.

  7. I remember when I wrote a graphical boot manager for PC (called GAG), and I had to do it in assembler. Assembler itself wasn’t complex because I already worked with the Z80, but working with the VGA and the calls to the BIOS to read after the 8GB limit in hard disks… that was crazy :)

  8. Thinking about this some, my advice is simple. If you have never learned assembly language, DO NOT start by learning on the x86. Your mind will be warped and possibly ruined forever. The x86 is an ugly hack. Learn on some civilized processor, i.e. virtually anything other than the x86. I am fond of the ARM at this point in time. I used to be a big champ of m68k, and have enjoyed xtensa, sparc, and a multitude of 8 bit processors. 6502 is nice and clean, but learning an 8 bit processor at this time is kind of pointless unless you are into retro stuff.

    1. > “but learning an 8 bit processor at this time is kind of pointless unless you are into retro stuff.”

      I have wondered about that. Some toys I have considered making, only need 8 bits, and if wanting to learn, a trike might come before a bike. I would have thought that learning a 16 bit first would add to the complexity of learning, See Spot run. Like tubes and nixies and even clocks, lots of us have missed certain things and like retro.

  9. Why did you deprecate this so quickly, HaD, when you keep real yawners up for days at a time? I have often thought I detect here a bit of the”…I don’t know anything about this subject; get rid of it before I get embarrassed…” syndrome.
    This one HAD some real potential. Not any more.
    You need to get feedback on these matters from people other than yourselves…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.