Tuesday, June 5, 2012

Assembly Language Programming


Assembly Language

  •   A low-level language for programming computers.
  •  It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture.
  •   This representation is usually defined by the hardware manufacturer. 
Assembler
  • A utility program that is used to translate assembly language statements into the target computer's machine code.
  • Eg: MASM, TASM etc.
  • With MASM (Microsoft Macro Assembler), you can create x86 assembly language programs that run under MS-DOS, Windows, or Windows NT ; or all three, in some cases.
      

Assembly Language Program Format
  • There are two types of x86 assembly language programs
  •                  exe-format and com-format. 
  • .EXE programs consist of separate code, data and stack segments, whereas .COM programs consist of one segment that contains code, data and the stack.

General Structure of an Exe-Format Program
An exe-format program must contain a code segment and a stack segment. It may contain a data segment or an extra segment.
                                    
<stack_name> SEGMENT STACK
<stack declaration>
<stack_name> ENDS
<data_seg_name> SEGMENT
<data declaration>
<data_seg_name> ENDS
<code_seg_name> SEGMENT
ASSUME SS:stack_name, DS:data_seg_name, CS:code_seg_name
<label>:     ;starting address of the program
                  . . .
<program instructions>
. . .
;terminate the program
MOV AH, 0
INT 21h
<code_seg_name> ENDS
END <label>
 
Example:

                          
SSEG  SEGMENT STACK
            DW 80h dup (0)
SSEG  ENDS
DSEG SEGMENT
                        message DB ‘ICT 3406’, ‘$’
DSEG ENDS
CSEG  SEGMENT
ASSUME SS:SSEG, DS:DSEG, CS:CSEG
BEGIN:    MOV DX, OFFSET message
                  MOV AH, 09H
                              INT 21H
MOV AH, 0
INT 21h
CSEG ENDS
END BEGIN


General Structure of a Com-Format Program
A com-format program contains only the code segment and therefore all segment registers (CS, DS, SS and ES) must be initialize to the address of the code segment using the ASSUME directive.
ASSUME CS:CODE, DS:CODE, SS:CODE, ES:CODE

The first 256-byte (100h) data area of the code segment called the Program Segment Prefix (PSP) is used to store some important information about the program. Therefore the location counter must be set to 100H by the directive:
ORG   100H

This statement must appear at the beginning of every com-format program before the program entry point. It places the first instruction at offset 100h in the code segment.

                    
<code_seg_name> SEGMENT
ASSUME SS: code_seg_name, DS: code_seg_name, CS:code_seg_name
            ORG 100H
<label>:     JMP MAIN
<data declaration>
. . .
MAIN PROC
<program instructions>
. . .
;terminate the program
MOV AH, 0
INT 21h
MAIN ENDP
<code_seg_name> ENDS
END <label>

Example:
                 
CODE SEGMENT
ASSUME SS:CODE, DS:CODE, CS:CODE
            ORG 100H
BEGIN: JMP MAIN
                           message DB ‘ICT 3406’, ‘$’
MAIN PROC
               MOV DX, OFFSET message
               MOV AH, 09H
                           INT 21H
                           MOV AH, 0
                           INT 21h
MAIN ENDP
CODE ENDS
END BEGIN