You are on page 1of 40

Assembly Level Language Programming

Program Group of instructions


Programming languages :
o
o
o

High level language.


Assembly level language.
Low level language.

Features of High level language Programming :


o
o
o
o

User Friendly.
Easy to debug.
Requires usage of compiler or Interpreter.
Requires move memory space.

Features of Assembly Level Language Programming:


o neither user friendly nor machine friendly but yet good to handle with.
o Requires Assembler.
o Less memory space required when compared with high level language
programs.

Assembler Directives

Assembly Level Language Programming


Features of Low Level Language Programming:
o Machine friendly and machine dependent.
o Lot of human effort required and difficult to debug.
o Lesser Memory space required.

Assembly level language program uses mnemonics, hence easy


to understand.
Assembly level language program explains about internal data
movement.
Assembly level language program use a simple assembler
binary code can be generated.

Assembler Directives

Assembly Level Language Programming


Assembly Level Language Programming Tools:
Editor:
o Program which allows a programmer to create a file containing
Assembly language statements.
o Stores the ASCII codes for letters and numbers in successive RAM
locations.
o The program in Editor is stored into a floppy or hard disk as a file.
o The source file is processed with an assembler by saving the source file
with an extension suitable for the assembler.
Assembler:
o It is program which translates the mnemonics for instructions to
corresponding binary codes.
o It reads the source file and generates two files namely object file and list
file.

Assembler Directives

Assembly Level Language Programming


Object file : (.OBJ)
o Contains the binary code of the instructions and information
about the addresses of the instructions.
o The object file is then loaded into memory and run after
further processing it.

List file : (.LST)


o It contains assembly language program statements, the
binary codes for each instruction and offset for each
instruction.
o It also gives information about any typing or syntax errors
present in the program, i.e., Assembly language grammar
errors.
Assembler Directives

Assembly Level Language Programming


For generating machine codes from source file the assembler
takes two passes:
First Pass:
o Determines the displacement of named data items, the offset of
labels..etc.
o Puts the information into symbol table.

Second Pass:

o Produces the binary code for each instruction and inserts the offsets
etc which are generated during first pass.
o In order to facilitate programmer to test and troubleshoot program, list
file is sent to printer.
o The assembler can give information only about the syntax errors.
o After generating object file and list file an linker or locator is used to
assign the physical starting addresses for the segments.

Assembler Directives

Assembly Level Language Programming


Linker:
o Linker is a program used to join several object files into one large object
file.
o Linker is useful when working with modular program
o While writing program for an bigger application the ALP will be large,
thus it is usually much more efficient to divide the large program into
smaller modules, which can be written, tested and debugged
individually.
o The modules are linked together to form a large, functioning program.
o Generally the object modules for useful programs can be kept in a
library file and linked with other programs as and when needed.
o The linker file and link map file generated by linker contains the binary
code for all the combined modules and the address information about the
linked files respectively.
o The linker assigns only relative addresses of the program which makes
program re-locatable as it can be put any where in memory to run it later.

Assembler Directives

Assembly Level Language Programming


o The linkers associated with TASM or MASM assemblers produce link files
with .EXE extension.
o For running the programs on systems such as SDK-86 board, an locator
which assigns physical address to the segments in the .EXE file is used
before debugging it using the debugger.

Locator :
o Program used to assign the specific addresses for the segments in object
code to be loaded into memory.

Debugger :
o Program which allows programmer to load object code of program into
system memory, execute the program and debug it i.e ., trouble shoot it.

Assembler Directives

Assembly Level Language Programming


Functions of debugger:
o Allows the programmer to look at contents of registers and memory
locations after the program is run.
o Allows to change the contents of registers and memory locations and
return to the program.
o Some debuggers allow programmer to stop execution after each
instruction so that programmer can check or alter memory and register
contents.
o Allows a programmer to set a break point at any point in a program.

Emulator:
o Mixture of hardware and software.
o Usually used to test and debug the hardware and software of an external
system like prototype of microprocessor based instruments.
o Emulator have a multi-wire cable which connects the host system to
prototype system.

Assembler Directives

Start
Define Problem
2

Develop Algorithm
Create Source File
With Editor
Assemble

YES

Assembl
y
Errors

.ASM

.OBJ
.LST

NO

Assembler1Directives

NO

Load Debugger

Load Program

LINK

.EXE
.MAP

Locate

.BIN

External
System

YES

Load Emulator

Load Program

RUN & TEST


Program

RUN & TEST


Program

Assembler Directives

10

4
NO

NO
Errors

Errors
YES

YES

Use Debugger Tools


To find errors

Use Debugger Tools


To find errors
Stop

Assembler Directives

11

Machine Level Language Programming


Generating the machine codes of program manually and execute it.

Disadvantages of MLP :
o The process is complicated and time consuming.
o The chances of error being committed are more at the machine level( in
hand-coding and entering the program byte-by-byte into the system).
o Debugging a program at the machine-level is more difficult.
o The programs are not understood by every one and the results are not
stored in user friendly form.

Assembler Directives

12

Assembly Level Language Programming


A program called assembler is used to convert the mnemonics of
instructions along with the data into their equivalent object code modules,
which may further be converted in executable code using the linker and
loader programs.
This type of programming is called Assembly level language
programming(ALP).
In ALP, the mnemonics are directly used in the user programs. The
assembler performs the task of coding.

Advantages of ALP over MLP:


o The Programming in assembly language is not so complicated as in
machine language because the function of coding is performed by an
assembler.
o The chances of error being committed are less because the mnemonics
are used instead of numerical opcodes. It is easier to enter an ALP.
o As the mnemonics are purpose suggestive, the debugging is easier.

Assembler Directives

13

Assembly Level Language Programming


o The constants and address locations can be labeled with suggestive labels
hence imparting a more user friendly interface to user.Advanced assemblers
provide facilities like macros, lists, etc making the task of
programming much easier.
o The memory control is in the hands of users as in machine language.
o The results may be stored in a more user-friendly form.
o The flexibility of programming is more in assembly language programming
as compared to machine language programming because of advanced
facilities available with the modern assemblers.

Assembler Directives

14

Assembly Level Language Programming


Assembly language programming (ALP) explains the way the computer
hardware and operating system work together and also about how the
application programs communicate with the operating system.
ALP helps in understanding the computer architecture and operating system.
ALP imposes rules about what is allowed and what not allowed in a program.
ALP makes the programmer understand the operating system, hardware and
data storage which is not possible with high level programming.
Assembly language programming is called low level language programming
because it is close to machine language in structure and function.
Each assembly language instruction corresponds to one machine instructions.

Assembler Directives

15

Assembly Level Language Programming


Mnemonic :
Short alphabetic code that literally assists the memory in remembering
a CPU instruction. It may be an instruction or directive.

Operand :
An instruction may contain zero, one (or) two operands.
An operand may be a register, a memory variable, or an immediate
value (data).
The choice of operand is usually determined by the addressing mode.
The addressing mode tells the assembler, where to find data in each
operand, i.e., whether in a register, or in a memory or as immediate data.

Instruction

Mnemonic + Operand
Assembler Directives

16

Assembly Level Language Programming


Comment :
The beginning of comment is marked by a semi-colon (;) character. All
other characters to the right are ignored by the assembler.
When a particular line is beginned with a semicolon, then whole line is
treated as a comment.

When ALP is being executed using Assembler:


Instruction:
Translated into machine code by the assembler.
Assembler Directives:
For directing the assembler.
For directives no machine code is generated and no memory location is
allocated.

Assembler Directives

17

Assembler Directives And Operators


Assembler Directives:

Gives direction to the assembler.


Necessary for assembler to assemble efficiently and correctly.
Controls the generation of machine code and organizes the program.
Is an indication to the assembler about the assumed contents in the
segment registers under the various circumstances as the assembly
progresses.
Gives the programmer more control over the exact placement of data
and segmentation of program than high level language program.

The hints to be given by programmer are regarding:

The required storage for a particular constant or a variable.


Logical names of segments.
Types of different routines and modules
End of file, etc.

Assembler Directives

18

Assembler Directives And Operators


The hints given to the assembler are:
1) Assembler Directives
2) Operators
Assembler Directives:
The hints in the form of predefined alphabetical strings.
Assembler Directives help the assembler to correctly understand
assembly language programs to prepare the codes.
Thus the directions to the assembler and pseudo operations are known
to be Assembler Directives.
The Assembler directives are not assigned with any memory locations.
The advantages of labels in assembly language programming are:
1) Program becomes more understandable.
2) The tedious byte handling and manipulations are avoided.

Assembler Directives

19

Assembler Directives And Operators


When the program is written and assembled using assembler, then the
assembler decides the address of each label and substitutes the values for
each of the constants and variables, the machine code for mnemonics and
data in the assembly language program.
While assembler is assembling a program it can find out only syntax errors
and in order to handle programming errors and logical errors the assembler
needs some special directions or hints from the programmer.
The assembler directives are used to specify start and stop of a program,
attach value to variables, allocate storage locations to input or output data, to
define start and end of Segments/Procedures/Macros.. Etc.
The assembler directives control the generation of machine code and
organization of the programs but no machine codes are generated for
assembler directives.

Assembler Directives

20

Assembler Directives And Operators


Operators:
Operators are the hints which helps the assembler to assign a
particular constant with a label or initialize particular
memory locations or labels with constants is called an
operator.
The operator performs the arithmetic and logical tasks
unlike directives that just direct the assembler to correctly
interpret the program, to code it appropriately.

Assembler Directives

21

Assembler Directives
Data definition and storage allocation:
Statements that reassign data and reserve storage have the form:
Variable Mnemonic Operand,.,Operand ; Comments
(Label)
The variable is optional, but if it is present it is assigned the offset of
the first byte that is reserved by the directive.
Unlike the label field, a variable must be terminated by a blank, not
a colon.
The mnemonic in the statement determines the length of each operand
and is one of the following:
DB (Define Byte) : Each operand datum of the label or variable
occupies one byte location in memory.
DW(Define Word) : Each operand datum of the label or variable
occupies one Word or two memory locations where low-order byte is
stored in first location and the higher-order byte is stored in second
location.

Assembler Directives

22

Assembler Directives
DD (Define Double Word) : Each operand datum is two words long with
low-order word followed by high-order word.
DQ(Define Quad Word) : Each operand data is 4 words long i.e. 8 bytes and
is stored starting from lowest byte to higher bytes.
DT(Define Ten Bytes) : Each operand datum is 10 bytes long and is stored
starting from the lowest byte to higher bytes.
The character string is stored in between single quotes and each characters
ASCII codes are saved(stored) in successive locations(i.e. the first character
goes into the first byte assigned to the variable, then second character of the
string is stored at second byte and so on)
The character string is defined or pre-assigned using byte type.
The character string can be defined using DW and DD also, but they are
rarely used as the bytes are reversed and also string operands in a DW or DD
cannot exceed two characters in length.

Assembler Directives

23

Assembler Directives
When an question mark (?) is used along with mnemonic as second
mnemonic, it does not pre-assign any value but, the appropriate amount of
space is reserved.
DUP operator is used along with data size defining mnemonics to duplicate
multiple locations with the specified value in the braces.
ex: DUP(0) fills 0s in all the variable locations.

Types of numbers used in data statements:


Binary : ex 11100101B
Negative number is represented in 2s complement sign-magnitude form.
Any binary number is represented using a suffix of character B.
Decimal : ex 20/ 20D / -20 / -20D
Any decimal number is represented using a suffix of character D.
Decimal is default data type.

Assembler Directives

24

Assembler Directives
Hexadecimal : ex: 30H
Always any hexadecimal number is represented using a suffix of
character H.
Always a 0(zero) is to be appended in front of a hexadecimal number
that starts with alphabet.
An decimal data type is converted to equivalent hexadecimal number
and then stored in the memory locations.
BCD : ex: 37H
the difference between an hexadecimal number and an BCD number can be
made only from its usage.
ASCII : ex: Array
An ASCII value is always written in single quotes.
An variable of ASCII type will have the ASCII equivalent value of the
operand is stored in the memory.
An Alphanumeric character string given in single quotes as ASCII string
is case sensitive.

Assembler Directives

25

Assembler Directives
Program termination directive: END
END Label
As an END statement is needed to signal the end of a high-level language
program, the end directive indicate the end of a set of assembler language
code.
The label appears only in the termination of the main program in a program
structure,although all separately assembled program modules must conclude
with an END directive.
The label must also appear in the instruction to be executed first, the point at
which the program begins.
Specifically, the address associated with this label is the address that the
loader branches to after the program has been loaded and is ready for
execution.
Thus the END directive is put after the last statement of the program to tell
the assembler that it is the end of the program module/file.
Any statement after an END directive is ignored by the assembler.

Assembler Directives

26

Assembler Directives
Procedure Definitions directives:
PROC & ENDP
PROC
The PROC directive is used to identify the start of the
procedure.
The PROC directive follows a name given to the procedure.
After the PROC directive the label NEAR or FAR is used to
specify the type of the procedure.
ENDP
The ENDP directive is used along with the name of the
procedure to indicate the end of a procedure to the assembler.

Assembler Directives

27

Assembler Directives
Alignment Directives:
EVEN: This directive forces the address of the next byte to be even.
ORG :
ORG Constant Expression
The ORG directive causes the next byte to be associated with the byte
number expressed by the constant expression
ORG 1000H
X DW 2002H
[1000H] = 2002H
When an program is being assembled by the assembler (data
declarations or instruction statements) an location counter keeps track of
how many bytes it is from the start of a segment at any time.
The usage of EVEN directive tells the assembler to increment the
location counter to the next EVEN address if it is not already at even
address.

Assembler Directives

28

Assembler Directives
When the directive EVEN is used in data segment then the location counter
is simply be incremented to the next even address is necessary.
When the EVEN directive is used in code segment, the location counter will
be incremented to the next even address if necessary. A NOP instruction will
be inserted in the location incremented over.
EVEN Num1 DW 4

Assigning names to expressions:


If an expression appears several times in a program, it is
sometimes more convenient to give it a name and refer to it by
the name, which also permit a short name to replace a lengthy
expression, and also will be meaningful which helps
programmers and others to remember as well as understand
them while reading.
Expression_name EQU Expression
Assembler Directives
29

Assembler Directives
Expression_name: any valid identifier or label
Expression: Have the format of any valid operand or that evaluates to a
constant or any valid mnemonic.
Normally assigning names to expressions is done in the beginning to
the program
EQU (equate) : The directive EQU is used to assign a label with a value or a
symbol.
The use of this directive is just to reduce the recurrence of the
numerical values or constants in a program code.
While assembling, whenever the assembler comes across the label, it
substitutes the numerical value for that label and finds out the equivalent
code.
Using the EQU directive, even an instruction mnemonic can be
assigned with a label which can then be used in program in place of that
mnemonic.

Assembler Directives

30

Assembler Directives
Segment definition directives:
One of the tasks that an assembler should perform is to assign the
offsets of the labels and variables as it translates the instructions into
machine language, the same information has to be passed to the linker such
that various segments and modules are put together to form a program.
In order to explain the exact structure of each segment such that
variable and label offsets are assigned by the assembler
The data,extra data, or stack segment normally has the form:
Segment_name SEGMENT
storage definition directives
allocation and alignment directives
Segment_name ENDS

Assembler Directives

31

Assembler Directives
A code segment normally has the form of
Segment_name SEGMENT
Instructions and
Instruction-related directives
Segment_name ENDS
The directives SEGMENT and ENDS defines the starting and ending
boundaries of a segment and the Segment_name is identifier of the segment.
The definitions,allocations and directives between SEGMENT and ENDS
directives are contained within that particular segment.
In addition to specifying boundaries for the assembler to translate the
instructions, it must know the exact correspondence between the segments
and the segment registers, which allows the assembler to check for certain
types of errors and inconsistencies i.e., whether a variable is defined in the
appropriate data or stack segment.

Assembler Directives

32

Assembler Directives
The assignments of the segments to the segment registers are
made using the directive ASSUME whose format is:
ASSUME Assignment,..,Assignment
Where Assignment will be of the form:
Segment_register_name : Segment_name
It is important to note that the assume directive does not load the
segment addresses into the corresponding segment registers.
The segment addresses can not directly loaded into segment
registers, so general purpose registers are used for this purpose.
When an logical segment is set-up, a name is to be given as a
label for the segment and the label cannot have spaces within it,
but an underscore can be used.
Instruction mnemonic or assembler directive can not be used as
label.
Assembler Directives
33

Assembler Directives
A logical segment is not usually given a physical starting address when it is
declared. After the program is assembled and linked with other assembled
program modules, it is then assigned the physical address where it will be
loaded in the memory to run.
Additional terms/indicators like PUBLIC,WORD.etc are added along
with the segment directive statement to indicate some special way in which
we want the assembler to treat the segment.
Public: To indicate that this can be used with other segments with same
name from other assembly modules, when the modules are linked
together.
Word: Contents of this segment located on the next available word(even)
address when absolute addresses given to it, else it will be loaded on the
next available paragraph(16-bytes) address.
When the assembler starts reading a segment by default the location
counter is automatically set to 0000H, the ORG directive allows
programmer to set the location counter to a desired value at any point in
the program.

Assembler Directives

34

Assembler Directives
LABEL :
Used to give a name to the current value in the location counter.
INCLUDE : (Include Source Code From File)
Used to tell the assembler to insert a block of source code from the
named file into current source module.
GROUP: (Group Related Segments)
The group directive is used to tell the assembler to group the logical
segments named after the directive into one logical group segment.
This directive allows the contents of all the segments to be accessed
from the same group segment base.
The assembler sends a message to the linker telling it to link the
segments so that the segments are physically in the same 64K-Byte segment.

Assembler Directives

35

Assembler Directives
PUBLIC:
Any label/variable name referred to in other modules must be declared
public in the module in which it is defined.
The public directive is used to tell the assembler that a specified name
or label will be accessed from other modules.
GLOBAL : (Declare symbols as Public/Extern)
The GLOBAL directive is used in the place of a PUBLIC / EXTERN
directive.
The GLOBAL directive is used to make the symbol/variable available
to other modules.
EXTERN :
Used to tell the assembler that the names or labels following the
directive are in some other assembly module.
LOCAL :
The labels, variables, constants or procedures declared LOCAL in a
module are to be used only by that particular module.

Assembler Directives

36

Assembler Directives
NAME : (Logical Name Of A Module)
The NAME directive is used to assign a name to an assembly language
program module. The module,may now be referred to by its declared name.
The names, if selected to be suggestive, may point out the functions of
the different modules and hence may help in documentation.
FAR PTR :
This directive indicates to the assembler that the label following FAR
PTR is not available within the same segment and the address of the label is
of 32-bits i.e., 2-bytes offset followed by 2-bytes segment address.
NEAR PTR :
This directive indicates that the label following NEAR PTR is in the
same segment and needs only 16-bits i.e., 2 bytes offset to address it.

Assembler Directives

37

Operators

SHORT :
Used to tell the assembler that only a 1-byte displacement is needed to
code a jump instruction.
Using the SHORT operator saves 1-byte of memory by telling the
assembler that it needs to reserve only 1-byte for this particular jump.
The destination must be in the range of 128 bytes to +127 bytes from
the address of the instruction after the jump.
LENGTH :
Returns the number of units assigned to a variable (not available in
MASM)
SIZE :
Same as the LENGTH operator,except that it returns the number of
bytes instead of the number of units.

Assembler Directives

38

Operators
+ and - operators:
These operators represent arithmetic addition and subtraction respectively
and are typically used to add or subtract displacements( 8-bit or 16-bit ) to
base or index registers and stack or base pointer registers.
SEG :
Causes the segment address of the variable or label to be inserted as an
immediate data or operand (although the actual insertion is made by the
linker)
If the data_seg is assigned to the block of memory beginning at 05000H and
opr1 is in data_seg, then the instruction
MOV BX, SEG opr1 would put 0500 in BX register.
TYPE :
Tells the assembler to determine the data type of a specified variable/label
and replaces the TYPE label by the decided data type.
The assembler determines the number of bytes in the type of the variable,ex:
for the word type variable the data type is 2.
ex: MOV AX , TYPE STR will load 1 into AX if STR is a byte array and 2 is
STR is a word array

Assembler Directives

39

Operators
OFFSET : Offset of the label
Tells the assembler to determine the offset or displacement of a named
data item (variable) or procedure from the start of the segment which
contains it.
This operator is usually used to load the offset of a variable into a
register so that the variable can be accessed with one of the indexed
addressing modes.
PTR : ( Pointer)
Used to assign a specific type to a variable or a label .
It is necessary to do so in any instruction where the type of the operand
is not clear.
The PTR operator can be used to override the declared type of variable
and also to clarify our intentions when we use indirect jump instructions.

Assembler Directives

40

You might also like