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:





0 comments:

Post a Comment