Assembly Language for beginners
There are a number of reasons why it is worthwhile to study Assembly language:
- It clarifies the execution of instructions.
- It shows how data are represented in memory.
- It shows how a program interacts with the operating system, processor, and the I/O system.
- It clarifies how a program accesses external devices.
A processor can only understand and execute machine instructions. These instructions are simply binary numbers stored in the computer. Typically, each assembly language instruction is translated into one machine instruction by an assembler.
Assembly language is hardware dependent, with a different assembly language for each type of processor. In particular, assembly language instructions can make reference to specific registers in the processor,
include all of the opcodes of the processor, and reflect the bit length of the various registers of the processor and operands of the machine language.
You can read this article if you don’t know what these terms mean✨
An assembly language programmer must therefore understand the computer’s architecture.
Disadvantages
Programmers rarely use assembly language, compared to High-level Languages (HLLs) for a number of different reasons:
- It is easy to make errors in assembly code. The assembler is not checking if the calling conventions and register save conventions are obeyed. Nobody is checking if the number of PUSH and POP instructions is the same in all possible branches and paths. There are so many possibilities for hidden errors in assembly code that it affects the reliability and security of the project unless you have a very systematic approach to testing and verifying.
- Assembly code is platform-specific. Porting to a different platform
is difficult. - The best modern C++ compilers already have functions for accessing system control registers and other system instructions. Assembly code is no longer needed for device drivers.
Common uses
There are still some advantages to the occasional use of assembly language:
- Understanding assembly coding techniques is necessary for making compilers, debuggers, and other development tools.
- Small embedded systems have fewer resources than PCs and mainframes. Assembly programming can be necessary for optimizing
code for speed or size in small embedded systems. - Accessing hardware, system control registers, and instructions may sometimes be difficult or impossible with high level code.
Consider the simple statement in the C programming language:
n = i + j + k;
Suppose that we want to program this statement in machine language and
the contents of i, j, and k have been initialized to 2, 3, and 4, respectively.
Assume a simple machine with a 16-bit word length consisting of an 8-bit opcode and an 8-bit address, and the only available register is an accumulator (AC).
The binary program for this is shown below:
Memory is reserved for the four variables starting at location 201. The program consists of four instructions:
1. Load the contents of location 201 into the AC.
2. Add the contents of location 202 to the AC.
3. Add the contents of location 203 to the AC.
4. Store the contents of the AC in location 204.
This is clearly a tedious and very error-prone process
In particular, we have to give an absolute address for each word. This means that the program and data can be loaded into only one place in memory, and we must know that place ahead of time. Worse, suppose that we want to change the program some day by adding or deleting a line. This will change the addresses of all subsequent words.
A much better system, is to use symbolic addresses:
Each line consists of three fields. The first field is for the address, but a symbol is used instead of an absolute numerical. Some lines have no address, implying that the address of that line is one more than the address of the previous line.
Get Chamuditha Kekulawala’s stories in your inbox
Join Medium for free to get updates from this writer.
For an instruction, the second field contains the three-letter symbol for
the opcode. If it is a memory-referencing instruction, then a third field contains a symbolic address.
This is called Assembly Language
Programs written in assembly language (assembly programs) are translated into machine language by an Assembler. This program must not only do the symbolic translation, but also assign some form of memory addresses to the symbolic addresses.
Key terms in Assembly
Assembler
A program that translates assembly language into machine code. It takes the basic commands and operations from assembly code and converts
them into binary code that can be recognized by a specific type of processor.
Assembly code consists of lots of ASCII characters; this would be stored in a file called source code. This then forms the input to a assembler which translates the source code into machine code or object code.
Popular Assemblers:
● MASM (Microsoft Macro Assembler)
● NASM (Netwide Assembler)
● TASM (Turbo Assembler)
● GNU Assembler
Assembly Language
A symbolic representation of the machine language of a specific processor, augmented by additional types of statements that facilitate program writing and that provide instructions to the assembler.
Compiler
A program that converts another program from some source language (or programming language) to machine language (object code). Some compilers output assembly language which is then converted to machine language by a separate assembler.
A compiler is different from an assembler by the fact that each input statement does not correspond to a single machine instruction or fixed sequence of instructions.
A compiler may support such features as automatic allocation of variables, arbitrary arithmetic expressions, control structures such as FOR and WHILE loops, variable scope, input/output operations, higher-order functions and portability of source code.
Executable Code
This is the machine code generated by a source code language processor such as an assembler or compiler. This is software in a form that can be run in the computer.
Instruction Set
The collection of all possible instructions for a particular computer; that is, the collection of machine language instructions that a particular processor understands.
Linker
A utility program that combines one or more files containing object code from separately compiled
program modules into a single file containing loadable or executable code.
Loader
A program routine that copies an executable program into memory for execution
Object Code
The machine language representation of programming source code. Object code is created by a compiler or assembler and is then turned into executable code by the linker.
That’s it for this article! In part 2 we’ll dive into more depth in the Assembly Language 🎉








