Basic Assembly Language Program Explained

Programming in Assembly Language is quite simple than programming in Machine Language. To understand the assembly language programming, we should be aware of its keywords and structure. Every Programming language has its own structure or way of writing the program. For example
C Program:
void man()
{
      int a;                 //first number
      int b;                 //second number
      b=a+b;
      return 0

}

Above example is structure of C program where every C program should start with main() function which starts with curly brackets and ends with same brackets again. Same logic can be written in assembly language programming. But the look and structure of assembly will be different than that of C programming.
Assembly Program:
.386
.stack 100h
.data
a dword ?                  ; first number
b dword ?                  ; second number
.code
main proc
mov eax, a
add b,eax
ret 
main endp
end

In above program, some of the statements are directives and some of them are instructions. Instructions tell the CPU what to do and Directives tell the Assembler what to do. This is the main difference between directive and instructions. Whatever starts with dot in assembly language is a directive.
.386 in above program tell the assembler that program will run on 386 or newer microprocessors.
.stack indicates that the size of stack is 100 hexadecimal bytes large, that is 256 bytes
.data directive tells assembler that what kind of data the program is going to use. It contains all the data defined by programmer including constants, arrays, string data and integer data.
.code directive tells that actual code of program is starting for the next line to this directive.
proc directive stands for procedure and indicates the main procedure. We can give any name to the procedure. We are not forced to give name as main as in C language.
ret instruction is similar to that of return instruction in C language.
endp indicates end of procedure and end indicates end of program.

Previous
Next Post »