Yesterday afternoon, I took the plunge into i386 type assembly programming. Although this is my first day getting my hands dirty, I was amazed by the elegance of POSIX compliant systems (like Linux) and how involved with the processor and the machine you have to be to do assembly coding. As a computer engineer, I like to bridge the world of software and hardware as much as I can, and consequently find assembly language fascinating.
For those of you who don’t know what assembly is, its pretty much the lowest level programming that can be done effectively and with good results. Although it is a way to program the computer, it is a vastly different way of thinking than is used in high level languages like C, or Java. The idea of learning it spooks many programmers, especially high level programmers due to the tedious, precise instructions that are needed, and the general trickiness of getting a program to run correctly. It also leaves pretty much no abstractions when it comes to interation with the kernel, so good operating system concepts are needed to a much greater extent than it takes to program in C.
Without getting too much into microprocessor architecture concepts, a microprocessor has a bunch of registers, or readily accessible and very small memory locations, and an “instruction set”, or hardware defined operations that the processor can carry out. These operations are very simple, like “look at what is in this memory location” or “add these two numbers together”. For instance, in a 4 bit microprocessor, the “0101” instructions may mean “add the two numbers in register A and register B together, and put the result in register A”. The simple interaction between these readily accessible memory locations and the instruction set is what allows for the incredible power of the modern microprocessor. The processor starts reading a sequence of instructions stored in memory and generates the corresponding output you’re familiar with.
The first programmers manually typed in each instruction into a file and fed that into the processor. These guys needed two keys on their keyboard, a “1” and a “0”. 😀 This of course, proved to be difficult to do. Specifying memory locations for where the instruction and data was located proved especially tedious, as each instruction must be carefully placed. A simple typo can make your program totally ineffective. As instruction sets grew larger with more functions, it was also hard to remember what bit sequence meant what, and furthermore, it was almost impossible to tell where instructions started and data ended. Looking back on code you wrote was very difficult. From these needs, the assembler was born. Writing in assembly is one small step removed from wrtiting in one’s and zero’s. The instruction set is aliased to human readable commands. For instance, if the bit instruction to move data is 10101110, the assembler lets you type in “mov” and translates that to bits for you. Furthermore, the assembler allocates memory methodically for the program you wrote, eliminating the tedium of figuring out what memory to use, and eliminates any chance of incorrectly specifying memory. These two allowed for programs that were fast to write, and far less error prone.
Assembly programming is still very tedious and very specific compared to high level programming, and there is only a small subset of people who can do it. It is very useful though in generating small programs and functions that high level compilers add unnecessary complexity to. For instance, processor intensive video encoders use assembly to increase the speed at which they operate.
Learning assembly is not like a Java coder learning C++, it requires a different way of thinking, and deep knowledge of operating systems and computing concepts to do anything effective with it. So, like all other things worth doing, its not a cakewalk. If I haven’t scared you off yet, I’ll point you in the right direction to start learning this powerful programming technique. The best tutorials I found for UNIX assembly programming were in the links contained in the page here. Also, I am planning on creating a website in the near future aimed at low level hardware development for open source coders and for hardware engineers, so check back here for the link to that site when I have written my own tutorials for i386 assembly! I personally can’t wait to learn more about assembly, and hope this article got you interested in it too. Good luck in this critical step to attaining computer enlightenment!