Skip to content

Assembly Language: An Introduction

Previously we took a sneak peak on the Intel X86 processors, their register configurations, interrupts etc. Now lets start with our introduction to ASM language. For starters, every CPU understands only machine language which is very very hard to program and hardly obvious.

For example:
add EAX and EBX registers the machine code will be

Fortunately, to do this kind of stuff there is assembly language that required a special program called assembler that does this kind of tedious work for us. In assembly language (ASM) above instruction can be given as:

This ADD is called mnemonic for addition instruction, an ASM program basically stores as a text file which can be read by assembler and converts them to appropriate machine code. Each mnemonic directly represent single machine code. But because each CPU has its own architecture thus understands its own machine language only and hence have its own assembly language, so porting assembly code between different CPU is much more difficult then in HLL(high level language) like C/C++ or Java.

Before going futher first lets talk about tool required, we need an assembler to work with so I’m using NASM (Netwide Assembler) as my assembler program. it can be found at: http://www.nasm.us/pub/nasm/releasebuilds/?C=M;O=D

There are other popular assemblers like MASM (Microsoft Assembler) and TASM (Turbo Assembler from Borland). You can use these as well but for all of my code I’ll only be testing on NASM so it will be better if you have NASM. Now if you are going to use NASM than just go to above link, download it and install it.

After Installation to test if NASM is successfully installed, open command prompt (cmd) and go to the installation folder and enter “nasm -h” without quotes it should show help from NASM showing its working just fine.

Basic Instructions

Now lets talk about the basic instruction in assembly language. The most basic instruction is the MOV instruction. It moves data or value of one register or memory location to another. e.g.

generalized => MOV destination, source

Before going further one more thing i want to tell is operands there are only 4 type of them :

  1. Register : operands that directly refer to CPU registers.
  2. Memory : operands that are memory locations/addresses thus refers to data in memory.
  3. Immediate : these are the data that are in code it self not stored in data segment.
  4. Implied : these are the data that are not explicitly shown rather are used directly like add 1A.

Now back to the topic of basic instructions :)

MOV dest, source will copy data from source to destination but have only two restriction that is firstly both source and destination cannot be memory address i.e. MOV mnemonic is specifically to process with registers there must be at least one register and second one is both source and destination should be of same data size or size

After MOV the next instructions are ADD and SUB.

ADD instruction is used to add integers only. e.g.

ADD requires data to be in registers so that CPU can access them and put result in one of them, same goes for SUB which subtracts one integer from other. e.g.

There are two more instruction which should be placed in basic category are INC(Increments) and DEC(decrements). INC adds 1 to the value and DEC subtracts 1 from the value.e.g.

Before ending this session should add one more thing, which is how to comment in the code so we can tell what the hell is actually happening in our program. You may be wondering what these lines are after the mnemonics. These are comments. Comments start with semicolon “;“. e.g

Comments are used as follows

Comments are very-very important in any ASM program. As ASM program are very hard to understand and make sense out of them. So comments helps tremendously. It is advised that each mnemonic line should be followed by a comment stating what exactly this statement is doing. And also there should be block of comments to define what exactly the preceding code trying to do.

This is all for this time will add next one most likely today or tomorrow..

cheers!

Published inAssembly Language

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *