Assembly Language For Beginners


Assembly Language For Beginners (Tutorial Series: 1)

Hello everybody, today we are going to start a new tutorial series on Assembly Language (Let’s interact directly with hardware now 😉 ). In this beginner series, we will talk about 32-bit assembly on Intel architecture. So, first let’s understand what is assembly language.

What is assembly Language?

Basically, assembly language deals directly with a microprocessor of your system. It’s a low-level programming language for a computer or any other programmable device, in which there is a very strong correspondence between the language and the architecture’s machine code instructions. There are few utilities that convert the assembly language into executable machine code called FASM, NASM, MASM, etc. Assembly language may also be called assembly, assembler, ASM, symbolic machine code or assembly program. To better understand all this, here is a pictorial representation of assembly program and how it gets executed and understood by processors.

Assembly Language For Beginners

This is the simplest representation of the conversion of assembly code into machine code that a system can understand.

If you want to know more about it and how certain high-level programming language ( like C ) is related to assembly language then here is a great illustration of that:

Assembly Language For Beginners

In the above diagram, the C source code(lincoder.c, lincoder.cpp) first passes through preprocessor stages(lincoder.i) and then passes through the C compiler (lincoder.s). Now the C compiler file becomes the input to the assembler and should give an object file (lincoder.o) which then passes through linker and linker will then take object file and any other shared libraries that are required to output an actual executable file (a.out or elf) which then is loaded by loader and made ready for execution.

Enough with the basics, now let’s write a simple assembly program and execute it to get a correct output. In order to execute assembly program you need to have an assembler called NASM or MASM or any other assembler but for our tutorial point of view, we will use NASM assembler.

The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. NASM is considered to be one of the most popular assemblers for Linux.

To install NASM on your Linux system (Ubuntu) type:

Now here is the sample program to print “Welcome To!!”:

Now the save the above program with .asm extension, e.g. lincoder.asm. To execute the above code, type:

Your output should be something like this:

I do know that the above code might be intimidating for beginners, but don’t worry as this is the first article on assembly language. You will start to understand each of the registers mentioned in the above program and what all is going on in the program, in future articles. But let me give you a basic idea about few things from the above code.

If you have read microprocessor in your college then you might have already encountered few of the registers used in the program. But if you didn’t then well let me tell you about few of them.

In the above program, we have used few registers likes edx, ecx, ebx, eax which are basically a 32-bit general purpose registers. Each line of code above has a description of what that specific line is doing, separated with a comment ( ; ). We can see mov (stands for move) command which copies the data assigned to len (stand for length) to the 32-bit register edx and same goes for other values too. To better understand the mov instruction here is simple illustration:

Here AX is the Destination and BX is the Source and data flows from source to the destination.

We can also see that int 0x80 is used to call kernel and global _start must be declared for the linker (ld). You also need to know the hexadecimal value of at least first 15 digits. You can google and learn it. This much of information should be enough for you to have a basic idea about Assembly Language and how to execute assembly program and how the execution flow takes place. So, let’s end this article right here!

Quick Tip:

Assembly Language is really or I must say, is a must for those who are planning to do reverse engineering in near future. Without understanding, assembly it will be really difficult to understand the application source code flow and reverse engineer them. So to become a better reverse engineer, you must learn Assembly Language.

That’s all in this article. Stay tuned for more updates. Have a nice day!