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







0 comments:

Post a Comment