You are on page 1of 81

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.

IBM PC Disk Operating System (DOS) has a locator on called


EXE2BIN. It converts ‘.exe’ file to ‘.bin’ file which has the
physical addresses.
DEBUG
A debugger is a program which allows you to
load your object code program into system
memory, execute the program and troubleshoot
or debug it.
 allowsyou to look at the contents of registers
and memory locations

The command to start debug is C> DEBUG


The debug command is executed and a hypen(-
) which is known as debug prompt will be
displayed
DEBUG COMMANDS
COMMAND FUNCTIONS
S
-R Display all registers and flags
-D Display contents of specified memory location
-A Assemble from the current CS:IP
-G Go command execute any program
-T Trace command used to run a program in
single step mode
-S Search a byte or string of bytes
-Q Quit the debug and return to DOS
-M Move the block of data from one memory
block to another
-U Unassemble from the Current CS:IP
ALP PROGRAMS
Addition
Subtraction
Multiplication
Division
To write an assembly language program to
perform addition of two 16-bit signed and
unsigned numbers.
ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
OPR1 = 4269H
MOV AX, OPR1
OPR2 = 1000H
ADD AX, OPR2
Output:
MOV RES, AX
HLT
RES = 5269H
CODE ENDS
DATA SEGMENT
OPR1 DW 4269H
OPR2 DW 1000H
RES DW ?
DATA ENDS
END
Subtraction of two 16-bit numbers
ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MOV BX, OPR2 OPR1 = 4269H
SUB AX, BX OPR2 = 1000H
MOV RES, AX Output:
HLT
CODE ENDS RES = 3269H
DATA SEGMENT
OPR1 DW 4269H
OPR2 DW 1000H
RES DW ?
DATA ENDS
END
Multiplication of two 16-bit unsigned
numbers
ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MUL OPR2
MOV RESLW, AX
MOV RESHW, DX Input:
HLT
CODE ENDS
DATA SEGMENT OPR1 = 2000H
OPR1 DW 2000H OPR2 = 4000H
OPR2 DW 4000H Output:
RESLW DW ?
RESHW DW ?
DATA ENDS
RESLW = 0000H (AX)
END RESHW = 0800H (DX)
Division of unsigned numbers

ASSUME CS : CODE, DS : DATA


CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
DIV OPR2
MOV RESQ, AL
MOV RESR, AH
HLT
CODE ENDS Input:
DATA SEGMENT
OPR1 DW 2C58H OPR1 = 2C58H (DIVIDEND)
OPR2 DB 56H OPR2 = 56H (DIVISOR)
RESQ DB ?
Output:
RESR DB ?
DATA ENDS
END RESQ = 84H (AL)
RESR = 00H (AH)
ASCENDING ORDER
DATA SEGMENT
STRING1 DB 99H,12H,56H,45H,36H
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX

MOV CH,04H

UP2: MOV CL,04H


LEA SI,STRING1
UP1: MOV AL,[SI]
MOV BL,[SI+1]
CMP AL,BL
JC DOWN
MOV DL,[SI+1]
XCHG [SI],DL
MOV [SI+1],DL

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

OUTER: MOV CL,04H


LEA SI,NUMBERS
INNER:

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

• It is generally used in conditional execution.


• subtracts one operand from the other for comparing whether
the operands are equal or not.
• It does not disturb the destination or source operands.
• It is used along with the conditional jump instruction for
decision making.
Example:

CMP DX, 00 ; Compare the DX value with zero


JE L7 ; If yes, then jump to label L7
.
.
L7:

CMP is often used for comparing whether a counter value has


reached the number of times a loop needs to be run.

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

• LOOPs are basically JMPs.


• It is possible to code loops without using LOOP
instruction, by using conditional JMPs and CMP.
• All LOOP instructions use CX register to count steps.
• CX can hold max value 65535 (216), and it is
possible to create nested LOOP structure and have
multiples of 65535, till infinity or end of RAM/
Stack.
LOOP Instruction
FACTORIAL
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM DB 07H
DATA ENDS
CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
LEA SI, NUM
MOV CX,[SI]
MOV AX,0001H
MOV BX, AX
LOOP1:
INC BX
MUL BX
CMP BX,CX
JNZ LOOP1
INT 3H
CODE ENDS
END START
THANK YOU

You might also like