Friday, September 7, 2012

First Program in Assembly Language


Now in this post I’m goanna teach you that how to write you first complete assembly program. As in other languages you must have to use an appropriate editor in order to write draft a program easily. In assembly language there are many editors in which you may write a program but here in this tutorial because I’m using assembly 8086 so I’m going to suggest you “Emulator 8086” . You can download it from http://8086-microprocessor-emulator.soft32.com/ and this is the latest version of emulator. So I’m supposing that you have downloaded it for the next part of this tutorial.
Before writing a full program lets starts with a template of the assembly program just like in C language headers files must always present in every C language. So the below template will always be the same in every assembly 8086 language code.

Template:
1 ;<Program name>.asm:
2 ;Author:
3 ;Date:
4 .model small
5 .stack 100h
6 .code
7  start:
8     ;  < Code will go here >
9  mov ax, 4c00h ; return to ms-dos
10 int 21h
11 end start
Now let’s suppose you are going to write a program which will display a single character on screen you have will use the line#1 in the above template as “single_character_display.asm” use semicolon before writing the Program name so that it could not be the part of code. Semicolon represents start of the program. Line# 2 represent the author name the person xyz is write a code , so put xyz after the semicolon to make sure it will not execute. After then write a current date to keep a record.

The first two directives, .model and .stack are concerned with how your program will be stored in memory and how large a stack it requires. The third directive, .code, indicates where the program instructions (i.e. the program code) begin.
After the start label you write your own code. Remember in assembly 8086 labels are terminated by colons.  And label can be assigned any name e.g. in above you can write your own name instead of writing start.
This same label is used in a last line with the keyword end. When the program written by you is finished at line#8 controls must be transferred to MSDOS. This is also accomplished by using the int instruction. This time MS-DOS subprogram number 4c00h is used. It is the subprogram to terminate a program and return to MS-DOS. Hence, the instructions:


mov     ax, 4c00h         ; Code for return to MS-DOSint       
int 21H            ; Terminates program
Terminate a program and return you to MS-DOS.

Now we know that what the template of assembly 8086 language is so we are in the condition to write our very first program which is to display a single character on the output screen.

Example 1: A complete program to display the letter ‘a‘ on the screen:

org 100h
; Example 1.asm: displays the character 'a' on the screen
; Author: salaser babu
; Date: 7th sep 2012
.model small
.stack 100h
.code
start:
mov dl, 'a'     ; Store ascii code of 'a' in dl
mov ah, 2h      ; 2h is the output subprogram num
int 21h         ; Interrupt displays character in dl register
mov ax, 4c00h   ; Return to ms-dos
int 21h
end start

 Below is the screen shot:





Saturday, September 1, 2012

I/O Instructions in 8086 Assembly Language


In order to start with a assembly language you have to have basic concepts regarding C language and it's subprogram.
The 8086 provides the instructiions in for input and out for output. These instructions are quite complicated to use, so we usually use the operating system to do I/O for us instead.
To perform I/O operations in C language we must call subprograms like printf(), scanf(), putchar(), getchar() and many more...
I'm assuming that you are familiar with the word "Parameter". So in order to properly call a subprogram in C language one must know the name of subprogram and its parameter as well.
for example if you want to display as single character "a" on a screen you will do it in the below mention way.
putchar(a);
In assembly language we must have a mechanism to call the operating system to carry out I/O.
So to do all these things in assembly language we must do below mention 3 steps.

  • Mechanism to call the operating system to carry out I/O.
  • What kind of I/O you wish to perform either it is read or write?
  • Passing parameters to subprograms. 

Calling Subprograms Using Interrupts.

Unlike C language we can't just simple call subprograms by their names and pass parameters to it. Now you will surely wants to know what is this interrupt?
Wikipedia defines it as : In computing, an interrupt is an asynchronous signal indicating the need for attention or a synchronous event in software indicating the need for a change in execution.
Now there are two types of Interrupts in computer science.
Hardware Interrupts: A hardware interrupt causes the processor to save its state of execution and begin execution of an interrupt handler.
Software Interrupts: Software interrupts are usually implemented as instructions in the instruction set, which cause a context switch to an interrupt handler similar to a hardware interrupt.
In 8086 Assembly Language interrupts are called by the "int" instruction. int instruction generates a software interrupt. You can find whole 8086 instruction set here http://en.wikipedia.org/wiki/X86_instruction_listings.
Int instruction uses a single operand which is a number indicating that which MS-DOS subprogram is to be invoked. In-order to invoke a I/O operation you have to use a number 21h. Thus, the instruction int 21h transfers control to the operating system, to a subprogram that handles I/O operations. This subprogram handles a variety of I/O operations by calling appropriate subprograms. This means that you must also specify which I/O operation (e.g. read a character, display a character) you wish to carry out. This is done by placing a specific number in a register. The ah register is used to pass this information. For example, the subprogram to display a character is subprogram number 2h. This number must be stored in the ah register.

Display a Single Character on Screen.

So here i'm going to tell you how to write a single character on a screen. To do this there are 3 steps which you need to follow carefully. 
  • Specify a character which you want to display i.e. "a". Make sure the character must resides in a register here in my case I will use dl register.
  • Specify the subprogram number. To display a character on screen I will use 2h.
  • Request MS-DOS to complete the operation using INT instruction.
Below is the snippet of 8086 code which implements the above theoretical steps.
mov dl, 'a'
mov ah,2h
int 21h

You can clearly see that if you want to implement this in C language you just have to do putchar ('a');


Read a Character From Keyboard.

Now again you have to follow three steps with a little bit change.
  • Specify the subprogram number. To read a character from keyboard I will use 1h.
  • Request MS-DOS to complete the operation using INT instruction. 
  • Here in this case the value you entered in keyboard  will be stored in al register.
Below is the snippet of 8086 code which implements the above theoretical steps.
mov ah,1h
int 21h
mov c,al