An Introduction to Assembly Language

Jessie White
4 min readJan 31, 2018

--

Just what assembly language is and does, why it’s necessary, and how it differs from machine code or computer hardware is often shadowed completely from the general public, and even the average web developer. Indeed, many graduates of Computer Science think of assembly language only when recounting their most challenging course in undergrad, and by and large it’s considered to be an arduous niche in the ever-changing canon of programming languages. It is, however, invaluable for understanding both how a computer’s central processing unit is programmed and performs specifically and the concerns considered during the implementation of any programming language, and thus, how each should behave when ultimately translated into machine code.

What is assembly language?

Assembly language is, in very simple terms, the last frontier between software and hardware. There are many versions of it, but each has a roughly one-to-one relationship with machine code (whether the machine code be binary or hexadecimal), and it is the language closest to the bare metal that is still human readable. Machine code, in turn, is a set of instructions that tells a computer’s CPU to perform a particular task (add, subtract, jump a line, compare values, etc.), and these instructions are executed directly by a computer’s CPU. As such, assembly language translates directly into machine code without the need for compilation, and allows actual people to write hardware programs. Assembly language programmers must understand the instruction set architecture of a computer, which serves as the interface between software and hardware and varies from machine to machine, in order to specify the necessary instructions to execute any program on a computer. However, these instructions are not necessarily written in assembly language and translated into machine code directly; more commonly, they are written in another programming language and compiled into machine code. In other words, a specific program written in a specific language does not first become assembly language before it becomes machine code and is executed on a specific machine.

Assembly and CPU Registers

A CPU (or processor) register is a location that is quickly accessible to a computer’s CPU, and usually consists of a small amount of fast storage. Registers differ from a computer’s main memory (or primary storage, or RAM), and nearly all computers load data from main memory into registers in order to perform computations or manipulations (such as adding, subtracting, jumping, and comparing) specified by the computer’s instruction set architecture. Properly allocating to and deallocating from a register is crucial to a program’s performance, and, when done manually, is achieved through using assembly’s commands LOAD and STORE, respectively.

JavaScript and Assembly

As a web developer specializing in JavaScript, I think necessarily in terms of JavaScript constructs, but I also often wonder how each relates to and will be ultimately represented in terms of computer instructions. For instance, here is an example of a simple if/else statement in JavaScript and its assembly language equivalent:

In the assembly example, the values op1 and op2 must be moved into the respective ax and bx registers before any calculation can be performed on them. They can then be compared and, if unequal, the program will jump (jne) to line one (L1). This is the equivalent of moving around the if statement and into the else statement in the JavaScript example, and setting x equal to 2. If the values are equal, the program keeps going and finally moves the value 1 into the main memory chunk represented by x. This is the equivalent of moving into the if statement and around the else statement in the JavaScript example and setting x equal to 1. Finally, once the comparison has been completed and x set to the appropriate value, the statement ends at the closing curly brace in JavaScript, with L2 being its assembly equivalent. This snippet is deliberately tiny and fragmented, and I ask the reader here to imagine that these comparison blocks exist within a larger, complete program.

I find this comparison interesting because I think it shows that having a surface level understanding of assembly can be key to making each code block in a higher level language like JavaScript seem much less abstract. While it can be daunting initially to look at two blocks of code written in two very different languages and fully understand how they each achieve the same (or a very similar) result, doing so can be immensely helpful to conceptualizing and breathing life into each program you write in whatever language you choose.

--

--