Module 2
Module 2
ASSEMBLER
• An assembler is a program used to convert an
assembly language program into equivalent
machine code modules, which may further be
converted into executable codes.
• It decides the address of each label and
substitutes the values for each of the constants
and variables.
• It then forms the machine code for mnemonics
and data in the ALP.
• It may find syntax errors while doing this, but, not
the logical errors.
• To complete these tasks, assembler needs some
hints from the programmer, such as, required
storage for a particular constant or a variable,
logical names of the segments, types of the
different routines and modules, end of file, etc.
• These types of hints are given to the assembler
using some predefined alphabetical strings called
assembler directives, which helps the assembler
to correctly understand the ALP to prepare codes.
ASSEMBLER DIRECTIVES
ASSUME
• Used to tell the assembler the names of the logical segments to be
assumed for different segments used in the program.
• In the ALP, each segment is given a name.
• You must tell the assembler that what to assume for any segment
you use in the program.
• Example, ASSUME CS: CODE tells the assembler that the
instructions for the program are in segment named CODE and
hence the CS register to be loaded with the address allotted by the
OS for the label CODE, while loading.
• The ASSUME statement is a must at the starting of each ALP,
without which a message ‘CODE/DATA EMITTED WITHOUT
SEGMENT’ may be issued.
DB – Define Byte
• Used to declare a byte type variable or to set
aside one or more locations of type byte in
memory.
• Example, PRICES DB 49H, 98H, 29H: Declare
array of 3 bytes named PRICES and initialize 3
bytes as shown.
DD – Define Double Word
• Used to declare a variable of type double word or to reserve a
memory location which can be accessed as double word.
DQ – Define Quadword
• Used to tell the assembler to declare the variable as 4 words of
storage in memory.
DT – Define Ten bytes
• Used to tell the assembler to declare the variable which is 10 bytes
in length or reserve 10 bytes of storage in memory.
DW – Define Word
• Used to tell the assembler to define a variable type as word or
reserve word in memory.
END – End the program
• To tell the assembler to stop fetching the instruction and
end the program execution.
• When the assembler comes across this END directive, it
ignores the source lines available later on.
• So, it should be ensured that the END statement should be
the last statement in the file and should not appear in
between.
ENDP – it is used to end the procedure.
ENDS – used to end the segment.
EQU – Equate
• Used to assign a label with a value or symbol.
• Reduces the recurrence of numerical value or
constants in a program.
• The recurring value is assigned with a label,
and that label is used in place of that
numerical value.
EXTRN (External and PUBLIC)
• Used to tell the assembler that the names,
procedures and labels declared after this
directive have already been defined in some
other assembly language modules.
• While in other module, where the names,
procedures and labels actually appear, they
must be declared public, using PUBLIC
directive.
EXTRN (External and PUBLIC)
INCLUDE – include source code from file
• Used to tell the assembler to insert a block of source code
from the named file into the current source module. This
shortens the source code.
LABEL
• Used to give the name to the current value in the location
counter.
• The LABEL directive must be followed by a term which
specifies the type you want associated with that name.
LENGTH
• Used to determine the number of items in some data such
as string or array. (Not available in MASM)
NAME
• Used to give a specific name to a module when the
programs consisting of several modules.
OFFSET
• It is an operator which tells the assembler to determine the
offset or displacement of named data item or procedure
from the start of the segment which contains it.
ORG – Originate
• Tells the assembler to set the location counter value.
• Example, ORG 7000H sets the location counter value to
point to 7000H location in memory.
SEGMENT
• Used to indicate that the start of a logical segment.
• Preceding the segment directive is the name you want
to give to the segment.
SHORT
• Used to tell the assembler that only a 1-byte
displacement is needed to code a jump instruction.
• If the jump destination is after the jump instruction in
the program, the assembler will automatically reserve
2 bytes for the displacement.
Assembly language development tools
To develop assembly language program, need program
development tools
such as,
EDITOR
ASSEMBLER
LINKER
LOCATOR
DEBUGGER
SIMULATOR
EMULATOR
EDITOR
• It is a program that allows the user to enter, modify, and
store a group of instructions or text under a file name.
• As you type in your program, the editor stores the ASCII
codes for the letters and numbers in successive RAM
locations
ENTERING A PROGRAM - EDITOR
To start Editor, type edit after C and enter the directory.
C > edit filename.asm
After pressing the enter key, Editor’s opening page will be
displayed
After that, enter text to edit the assembly-language program.
A sample program ABC.ASM is edited to subtract two numbers
as shown in Fig.
ASSEMBLER
An assembly program is used to transfer assembly language
mnemonics to the binary code for each instruction with the help
of an editor, it is then assembled with the help of an assembler.
An assembler works in 2 phases,
- it reads your source code two times.
- On first pass through source program: collects all the symbols ,
offsets of labels and put the information in symbol table.
- On second pass: produces binary code for each instruction of the
program, and insert the offsets that it calculated during first pass
ASSEMBLING A PROGRAM – MASM
ASSEMBLER
The Microsoft Assembler MASM is a most popular assembler
and it is very easy to use.
Main task of any Assembler is to accept the text assembly
language program file as an input and prepare an object file
MASM accepts the file names only with extension.ASM
Example, to assemble the program, enter a following command
C > MASM ABC or C > MASM ABC.ASM
Assembler generate three files- The object file, the list file and
cross reference file.
OBJECT FILE(.OBJ)
The object file contains the binary code for each instruction in the
program.
It is created only when your program has been successfully
assembled with no errors.
The errors that are detected by the assembler are called the
symbol errors.
For example, MOVE AX, 25H ;
it reads the word MOVE, match with the mnemonic sets, as there is
no mnemonic with this spelling and it assumes it to be an identifier.
looks for its-entry in the symbol table. It does not even find it there
therefore gives an error as a undeclared identifier
LISTING FILE (.LST)
It is optional
Contains source code, binary equivalent of each instructions
and
Offsets of symbols
this file used for documentation purposes
Available assembler: MASM, TURBO etc
CROSS REFERENCE FILE(.CRF)
contains information such as size of file in bytes, number of
labels, list of labels, and routines of the source program.
After entering the cross reference file name, the assembly-
language process starts.
LINKER
A linker is a program used to join several
object files into one large object file.
It is better to break your source program
into several subroutines.
These files are assembled separately.
After each file has been successfully
assembled, they can be linked together
to form a large file which constitutes your
complete program
produces a link file, which contains the
binary code for all compound modules.
It also produces link maps, which contains
the address information about the linked
files.
LINKING A PROGRAM – LINK
LINK.EXE file link the different object modules of source
program and function library routines to generate executable
code of source program.
Input to linker is .OBJ file and it generate .EXE file
The linker program is executed by command
C> LINK or C> LINK ABC.OBJ
LOCATOR
A locator program is one that assigns specific address of
where the segments of the object code are to be located into
memory.
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV CH,04H
DOWN: INC SI
DEC CL
JNZ UP1
DEC CH
JNZ UP2
HLT
CODE ENDS
END START
Descending order
DATA SEGMENT
NUMBERS DB 09H,05H,08H,04H,0FH
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV CH,04H
MOV AL,[SI]
MOV BL,[SI+1]
CMP AL,BL
JNB DOWN
MOV DL,[SI+1]
XCHG [SI],DL
MOV [SI+1],DL
DOWN: INC SI
DEC CL
JNZ INNER
DEC CH
JNZ OUTER
INT 3
CODE ENDS
END START
Largest number
data segment
STRING1 DB 08h,14h,05h,0Fh,09h
res db ?
data ends
code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, 04h
mov bl, 00h
LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jl nxt
mov bl, al
nxt:
inc si
dec cx
jnz up
mov res,bl
int 3
code ends
end start
Smallest Number
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
ARRAY DW 00A1H,00A3H,0008H,0007H,0002H
COUNT EQU 05H
DATA ENDS
CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV CX,COUNT-1
MOV SI,OFFSET ARRAY
XOR AX,AX // mov ax,0
MOV AL,[SI]
LOOP1: ADD SI,02H
CMP AL,[SI]
JC LOOP2
MOV AL,[SI]
LOOP2: LOOP LOOP1
HLT
CODE ENDS
END START
CODE CONVERSION PROGRAMS
• BCD to ASCII Conversion
• ASCII to BCD Conversion
• BCD to Hexadecimal Conversion
• Hexadecimal to Decimal Conversion
ASCII to BCD Conversion
BCD to Hexadecimal Conversion
Hexadecimal to Decimal Conversion
Assembly Language programming
• Loops
• If then else
• For loop
• While loop
For Loop
Syntax:
Translation
sum = 0; mov eax, 0 ; eax is sum
for (i = 0; i <= 10; i++) mov ebx, 0 ; ebx is i
sum += i loop_start:
cmp ebx, 10 ; compare i and 10
jg loop_end ; if (i>10) go loop_end
add eax, ebx ; sum += i
inc ebx ; i++
jmp loop_start ; goto loop_start
loop_end:
CMP Instruction
INC EDX
CMP EDX, 10 ; Compares whether the counter has
reached 10
JLE LP1 ; If it is less than or equal to 10, then jump to LP1
Loop Structure