A Bit about Assembly Language

Seattle Web Design
computer programming languages
7 min readOct 14, 2020

--

Assembly was one of the first computer languages to be created. Before this language, the only way to program a computer before assembly was with binary.

Thus, an operator would need to know how to get the desired result with no more than 1’s and 0’s. Humans needed to read and write long, complicated strings of numbers to perform actions. Understandably, this was not something easy to read or write programs in.

This snippet of code loads the number 01100001 (decimal 97) into the AL register of a x86 processor. The binary instruction is 10110, with the 3 digit identifier 000 following telling it to use the AL register. As mentioned before, this is difficult to read and understand.

Assembly was the first language to use mnemonic instructions, which are English word abbreviations. This shortened the amount of work and made it easier to read, paving the way for today’s higher-level languages.

The above code is the same as the binary snippet — it loads decimal 97 into the AL register. However, it has become much more readable using the English word abbreviations to do the same task. Comments have also been added to help remember what is happening.

History

The first assembly language is attributed to Kathleen Booth in 1947, for the ARC2 at the University of London. This implementation however more closely resembles math to run a machine rather than the modern mnemonic form.

The first mnemonic assembly language format was developed by Maurice Wilkes and David Wheeler for the Electronic Delay Storage Computer. The ESDC had an assembler with one letter mnemonics, which were stored in the first 31 locations of the machine. These locations were referred to as ‘initial orders’.

The modern implementation of Assembly was developed in the ’60s for the IBM 7090. Several people worked on the project, called the Generalized Assembly System. These people included Douglas McIllroy and George Mealy.

Platforms

Assembly can run on anything, as it is a foundational machine language. Lines of code translate 1:1 into machine code — as in, every line is one processor instruction. Each line of code is not interpreted or compiled. Despite the complexity and difficulty in using assembly, it is still widely used today in computing.

You can find it running on many kinds of devices with many kinds of applications. Here are a few examples:

· Writing code for processors that have limited high-level language options, or require legacy code to be maintained.

· Assembly is perfect for interacting directly with the hardware. Device drivers are an excellent application of the language.

· It is used in situations where you require absolute control over the machine. You might see this in simulations, sensitive computations, flight sensors, and medical equipment.

· Assembly may be used frequently in matters of cryptography. This is particularly true in matters where it must take the same amount of time to execute.

· Malware may be written in assembly much to the displeasure of anyone on the receiving end.

· Boot sequences use assembly to start a computer up.

· Reverse engineering software. Sometimes compiled software is disassembled into assembly to understand how it works. This is how hackers figure out how to beat the security measures of a program.

Benefits and Drawbacks

Like any language, Assembly has pros and cons.

Assembly is a very old, low-level language. It doesn’t require the machine to perform a lot of interpretation to run a program. As a result, programs written in assembly run very quickly. Assembly Language also offers a level of control over a device that higher-level languages simply cannot compete with.

The cost of this, however, is that it is a programming language that is exceptionally difficult. This unfortunately also makes programs written in assembly error-prone, and many avoid Assembly in favor of higher-level languages.

Examples of Code

The traditional first program a programmer creates is called “Hello World!”. This is a program which only prints those two words to a screen. Here is this program written in Assembly:

In contrast, the same simple “Hello World!” program in Python is a single line:

Code Comparison with Python

Assembly code follows a very specific syntax. The label, operands, and comments are optional, while the mnemonic is required per line:

A label is a special way of representing a place in memory. It’s a good idea to label important parts of your program, such as the START, a LOOP, or similar functions. The assembler will replace these with the required values. The mnemonic is the specific direction you will be issuing to the program on the line. The operands are the specifics of how to operate the instruction.

Some instructions will not require an operand, while others may require as many as three operands to function.

An Assembly code segment starts with these section headers. Section.data and section.bss are optional, while section.text is mandatory and contains the programming instructions for the computer. Assembly language can vary slightly processor to processor so this may have a different requirement for a different specific processor.

Comments are supported to provide additional notes to the reader, much like in Python:

To write code in assembly involves interacting with bits in memory using the mnemonics. Each instruction could be stored in memory, but the time to store and fetch it would take time. The solution is to instead use a processor’s internal memory, which is called a register. A limited number of registers are built into a processor chip and grouped into categories.

· General, which are further divided into more categories:

◦ Data: Arithmetic and logical operations.

◦ Pointer: Stores offset values or references needed.

◦ Index: Used as a source or destination for strings.

· Control: Comparisons for mathematical, logic, or conditionals, or any operation that changes flags on an operation to change how it works.

· Segment: Stores the starting spots for code, data or stack parts of a program.

As this is a fairly complicated topic outside the scope of this paper to explain, register uses have been generalized above. Each register has a specific ID assigned to it, and has a kind of task it is used to perform. Some registers are specifically used for one kind of task exclusively.

If statement

Conditions are much less straight forward than in Python. In Python, you use ‘if’ statements to for conditional checks:

Assembly uses the compare and jump statements to execute conditionals, the mnemonics being CMP and JMP respectively. CMP checks if a condition is met, and JMP jumps to a specified code block. CMP serves as the ‘if this’ statement, JMP is used to execute code based on results of CMP.

JMP also can be set to have conditions fulfilled to jump to the code block it targets.

Loop

The JMP statement above can be set up to act as a while loop. Additionally, Assembly has a LOOP mnemonic statement that will continually loop code a set number of times. The EDX register counts the number of loops.

Trends

Assembly is still utilized today for its speed and is unlikely to ever depart the computing world. As of 2020, it is still a language with some demand. A job writing an assembly program can net you a well deserved 120k per year.

While it is under Python in popularity, Assembly is still in demand and loved by some programmers for its strengths. It was also high on the list of the world’s most dreaded languages according to the 2020 stack exchange survey. It’s a love-hate relationship with this language.

Tiobe has noted that Assembly seems to have risen in usage in 2020 to the 14th most used language. This is up from 2019, where it was the 15th most used language in computing.

Should I learn Assembly?

The answer is “No”. The only two reasons to ever learn assembly are:

You are writing a piece of software that needs to be written in Assembly. Assembly offers an unparalleled level of control and speed in software that high-level languages struggle with in comparison.

You want to study how computers work on a more fundamental level. This may assist you with your higher-level programming and is often why computer science majors study assembly language.

If your a high level coder, even a web developer, you will never have the need to understand hardware and assembly programming!

--

--