Structure of an Assembly Language Program 

Program Structure:

A program has always the following general structure:

Structure of an Assembly Language Program
	.model small		;Select a memory model
	.stack stack_size	;Define the stack size
				; Variable and array declarations
				; Declare variables at this level
	main proc
 				; Write the program main code at this level
	main endp
	;Other Procedures
				; Always organize your program 
				; into procedures
	end main		; To mark the end of the source file

Title directive:

The title directive is optional and specifies the title of the program. Like a comment, it has no effect on the program. It is just used to make the program easier to understant.

The Model directive:

The model directive specifies the total amount of memory the program would take. In other words, it gives information on how much memory the assembler would allocate for the program. This depends on the size of the data and the size of the program or code.

Segment directives:

Segments are declared using directives. The following directives are used to specify the following segments:
  • stack
  • data
  • code
  • Stack Segment:

  • Used to set aside storage for the stack
  • Stack addresses are computed as offsets into this segment
  • Use: .stack followed by a value that inicates the size of the stack
  • Data Segments:

  • Used to set aside storage for variables.
  • Constants are defined within this segment in the program source.
  • Variable addresses are computed as offsets from the start of this segment
  • Use: .data followed by declarations of variables or defintions of constants.
  • Code Segment:

  • The code segment contains executable instructions macros and calls to procedures.
  • Use: .code followed by a sequence of program statements.

  • Example:

    Here is how the "hello world" program would look like in assembly:

    The hello world program in Assembly
    .model small
    .stack 200
            greeting db 'hello world !',13,10,'$'
    mov ax,@data
    mov ds,ax
    mov ah,9
    mov dx,offset greeting
    int 21h
    mov ah,4ch
    int 21h