Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

INTRODUCTION TO ASSEMBLY LANGUAGE PROGRAMMING

1.0 Outcomes

At the end of this chapter, you should be able to:


1. Describe the basic structure of an assembly language programme
2. List and explain the tools required for assembly language programming
3. Describe the steps in creating assembly language programmes

1.1 The Structure of an Assembly Language Program


An assembly program contains:

1. Statements or instructions that the programmer wants them executed by the


microprocessor. Each statement may have up to 4 fields
2. Directives to the Assembler (translator program) that informs the assembler how to
assemble (translate) the program
3. Remarks that explains the working of the program to other programmers. These remarks
are optional and will be ignored by the assembler.

; Disp.asm
; display a message on the screen
.MODEL SMALL
.STACK 200h
.DATA
Prompt db 13,10,"This is the message $"
.CODE
start:
mov ax, seg prompt ;moves the segment of data into ax
mov ds,ax
mov ah,9
mov dx,offset Prompt
int 21h ;print the message
.exit
End start

Figure 1.1 An Example of an Assembly Language Program


In Figure 1.1, the statements are bold while the directives are in italic and the comments are in

normal text.

1.1.1 Assembly Language Programs Statements


An assembly language statement may have up to 4 fields as shown in example 1.1. The first
field (new:) is the label field. It is used for flow control of the program. A colon (:) must
terminate the label field. This field is not required unless the programmer wants to refer to that
line somewhere in the program.

The next field is the operation code (Add), which must be a valid operation code for the given
microprocessor. The operation codes are given by the microprocessor manufacturer and are
called the instructions set. The operation code tells the microprocessor what action to perform.
This field is required unless that line of the program is a comment line.

The third field is the operand field, (AX,5H). This contains the data on which the operation will
be performed. The type and the number of operands depend on the operation code. An
operation code may require no operand, one, or two operands. If more than one operand is
required they must be separated by commas.

The last field is the remark field (; Add percentage). This may contains anything as it is
intended for the programmer and not the computer. The objectives of the remark field are to
aid the readability of the program; help the programmer understands the program and thus
improve maintainability. The comment field starts with a semicolon (;). This field is not
compulsory but recommended.
Example 1.1
New: Add AX,5H ; Add percentage
Mov dx,ax
Jmp new

Table 1.1 summarises the fields of the four fields of the assembly language instruction

Table 1. 1 The fields of an assembly language statement

Field No Field Name Descriptions


1 Label field • Used as a reference for program execution control
• Must be a single word
• Must be terminated by a colon (: )
• Optional i.e. not compulsory
2 Operation Code • A mnemonic taken from the microprocessor instructions set
• Compulsory except for empty lines
3 Operand • Depends on the operation code
• Not all operation code requires an operand
• Can be a constant or a variable
4 Comment • Must start with a semicolon (;)
• Used to explain the statement line
• Optional

1.1.2 Assembler Directives


Assembler directives are pseudo-operations that control the assembly process but, normally,
they do not generate any instructions in the assembled code. They merely instruct the
assembler on how to process the assembly programs and as such they are specific to each
particular assembler. The assembler directives generally fall into the following categories:
• Assembler-Use-Only Directives
• Data Storage Directives
• Location Control Directives
• Symbol Declaration Directives
• Routine Entry Point Definition Directives
• Repeat Block Directives
• Assembler Option Directive
• Procedure Attribute Directives
• Version Control Directive

The assembler used for all the examples given in this book is Turbo Assembler (TASM
version 5). Some of the directives given in Figure 1.1 are explained in Table 1.2. For a
complete list of all directives consult the TASM user reference.
In describing the syntax of a directive in Table 1.2, the followings are used:

.directive <required parameter>{,optional parameter}

The .directive is the directive name from the list of available directives for TASM. <required

parameter> is a parameter that must be included with the directive. {optional parameter} is a

parameter that can be supplied or omitted. If it is not supplied a default value is used. Note

that the <> and {} should not be included in the directive.

Table 1. 2 Assembly Language Directives

Directive Syntax Description

.MODEL .MODEL <memory model> This directive defines the attributes that affect
SMALL the entire module such as: memory model,
{,options}
default calling and naming conventions,
operating system, stack type etc. Thus, it must
appear in the program before any other
simplified segment directive. The memory
models available are: tiny, small, compact,
medium, large, huge, or flat. The memory
model selected determines size of code and data
pointers
.STACK .STACK {stack size} This directive creates a stack segment. If the
200h stack size is not supplied, the assembler
allocates 1K of memory for the stack.
.DATA .DATA This directive creates a near data segment that
contains the frequently used data for your
program. The data segment can occupy:
• up to 64K in MS-DOS
• or up to 512 megabytes under flat model
.CODE .CODE This directive creates a near code segment that
contains the code for your program
.exit .exit This directive is available only in 16-bit
addressing modes. It is used to terminate
program execution and return control to DOS

1.2 Assembly Language Programming Tools


To be able to effectively develop assembly programs, the following tools are needed:

1.2.1 Text Editor


A text editor is a program that allows you to create a file containing the assembly programs
(source code). Some of the operations that you can perform with a text editor are: -
1. Type the assembly program
2. Correct any typing error in the program
3. Insert new statements or delete unwanted ones
4. Save the program on magnetic medium for later uses
5. Rearrange the order of the statements by moving them from one place in the program to
another
6. Copy part of the program and insert it in another part of the program or in another program
7. Some text editors may have more advanced facilities like find and replace, etc.
Figure 1.2 shows the process of creating source code ( assembly language programs) using a

text editor such as Notepad or WordPad which comes free with Windows operating systems.

Hand written Text Source


Manual
Editor Code
Assembly Input
Program

Source
Code

Figure 1.2 Creating a Source Code with an Editor


1.2.2 Assembler
The assembler is a program that translates assembly language statements into machine code

instructions that can be executed by the microprocessor. As can be seen from Figure 1.3, the

assembler takes the source code as input and will produces an object file containing the

machine code instructions if the assembler did not detect any syntax error during the assembly

process. Other optional files may be generated depending on the assembler in use. If the

assembler detects any error during the assembly process only a list file containing the assembly

instructions, the error code, and an error message for each error encountered will be produced.

Yes Source code


Source Assembler Syntax Listing + error
Code Errors? messages

No
Source code
Listing Object
Code

Figure 1.3. The Process of Assembling (Translating) an Assembly Code into Machine Code

1.2.3 Linker
A linker is a program that links several object files into an executable file, a program that can be

executed by the operating system. The output of the linker is an executable file if no error

encountered during the linking process. If any error encountered the linking process is halted

and an error message describing the error is displayed as shown in Figure 1.4.
Object Linker Executable
Code Code

Library
Files

Figure 1.4

1.3 Creating Assembly Programs


To create an assembly program, follow the steps below:

1. Design your program using any of the design tools mentioned earlier

2. Use a text editor or a word processor that can produce text file to type the assembly

language programs

3. Save the program (as a DOS text file) on a disk

4. Assemble the program to check for syntax errors

5. If the program assembles properly, Link the program to produce an executable file

6. Run the executable file

1.4 Review Questions


1) An assembly program may contain
a) Assembly language instructions
b) Assembler directives
c) Comments
d) All the above
2) An assembly language statement may have up to ______ fields
3) Assembler directives can be used to:
a) Declare variables
b) Define segments
c) Define procedures
d) All the above
4) The input file to an assembler is called
a) Source code
b) Object code
c) Executable code
5) Describe the structure of an assembly language program
6) Describe the structure of an assembly language statement
7) Identify each field of the following assembly language statement:
New: Mov Dx, ax ; Copy the content of ax into dx
8) Describe the .code directive
9) Describe the .data directive
10) Describe the .stack directive
11) Describe the .model directive
12) Describe the Label field in the assembly language statement
13) Describe the operation code field in the assembly language statement
14) Describe the comment field in the assembly language statement
15) With the aid of a diagram, describe the assembly process
16) With the aid of a diagram, describe the linking process
17) With the aid of a diagram, describe process of creating source codes

You might also like