Assembly Directives in 8051 Microcontroller-1

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

ASS£M8L£;< D/l?.

£Ci/V£S
-
I

ORC. :- oR;C.;...,lc -, dil,-,1. -ii., sJe,'-'i o ~ -IClf '1k 'P"'3 · /11 tp«IJ· ,_,,.,,.., ·
OJIG. Oi H ·) ,Siaf/f"' o,JrJ.,ub 11 0 001 tf
0~~ 12.3~H--> $4o14'·. ,. ~ ,-.,, /'2.3Y'1
-- - .J
Assembler directives:

•ORG
•END
•EQU
•DB
ORG (origin)

•The ORG directive is used to indicate the beginning of the address


• The number that comes after ORG can be either in hex and decimal
• If the number is not followed by H, it is decimal and the assembler will
convert it to hex

END

• This indicates to the assembler the end of the source (asm) file
• The END directive is the last line of an 8051 program
• Mean that in the code anything after the END directive is ignored by the
assembler
EQU (equate)

• This is used to define a constant without occupying a memory


location
• The EQU directive does not set aside storage for a data item but
associates a constant value with a data label
• When the label appears in the program, its constant value will be
substituted for the label
• Assume that there is a constant used in many different places in the
program, and the programme r wants to change its value
throughout
• By the use of EQU, one can change it once and the assembler will
change all of its occurrences

COUNT EQU 25
........
MOV R3, #COUNT
DB dlredives

• The DB directive is the most widely used data directive in the


assembler
• It is used to define the 8-bit data
• When DB is used to define data, the numbers can be in decimal,
binary, hex, ASCII formats

ORGSOOH
0828 ;DECIMAL (lC in Hex)
08 001101018 ;BINARY (35 in Hex)
DB39H ;HEX

DB " 2591" ;ASCII NUMBERS

DB "My name is Joe'-' ;ASCII CHARACTERS


t.,
Chapter : / 80 51 flSSEMBL~
9 •

LflNGOflGE PROGRflMMltiQ
~

INTRODUCTION TO 8051 ASSEMBLY PROGRAMMING


. bm'ary altho ugh it work at very high spee d (millions or
The CPU can work onlY m , • 1 d ·
operation in a second). But for hum an beings, it is very diffic
u t an l~ime ~onsuming to
write a prog ram using Os and ls. Also, if a prog ram is writ ten
m_a~ua Y us~ng Os~ ls,
'tt'
ch ances of comm1 mg error are v ----
ery = high and in case of error , 1t 1s very difficult to find
it out. A program that consists of Os and ls is calle d Mach .
i_?~ La ngua ge Prograrn.
Although tn e hexadecimal system ~n be used as a more
effic ient way to represent
binar y numbers, but the process of working in mach ine
code is still difficult and
confusing for hum ans. Eventually, assembly lang uage was
deve lope d that provided
English like words called mnemonics for the mach ine code
instr uctio ns, plus other
features that made programming faste r and less pron e
to error . Mnemonics are
abbreviations for operation codes and easy to reme mber . A com
pute r prog ram written
using mnemonics is called Assembly Language Prog ram. Asse
mbly lang uage is referred
to as a low-level language because it deals directly with the
inter nal struc ture of the
CPU. For assembly language programming, the prog ramm er
mus t know the internal
architecture of the CPU.
Assembly lang uage prog ram mus t be trans litted into mac hine
lang uage program
before the CPU can execute it. This work is done by Asse
mble r. Asse mble r is a
prog ram ~hat trans lates an assem bly lang uage prog ram
into mac hine c~ o
calle
fu · d obJect code) . In addit ion to trans Tatio n the asse
h . mbl ..._
er per f'orms vanou
nctions sue as error checking and mem_ory__!..... .. ·
alloc s
-
ation s. --- =-'
.....__
_ _ _ __ Jf

Assembly - - . . w·
Assembler
Language Program r---. -..~ Machine Code

Fig. 9.1 : Assembler


Similary, in case of high level .
Comp1·1er 1s
. 1angu ages hke C C
a prog ram that converts h' h , ++, com piler s are used·
prog ram. a ig
- - - - --- 1evel la
ngua ge prog ram into object codes

208
210 ISHAN'S Programmable Logic Controllers and M·
ICroeo
l'ltro11
9 .2 ASSEMB-LER OPERATIONS er1

~ ter writing the assembly language program, it is necessary to


assembly, linkage and debugaing (errors produced are to be analyzed and carry 011
. h. 1
This whole process produces a hex file called mac ine anguage programreino
bA
w ."ed)
down loaded into 8051 ROM. . hich h
The steps to create an executable Assembly language program are as foll
1. First type in the program using an editor.
. An di h. ows .
Ye tor w ich can Produ ·
ASCII file like MS-DOS EDIT program, Notepad can be used for ce_a1
and/or editing the program. Then save the fil . h- · creat1
i e wit extension name " n,
or "src" or as per the assembler specifiicat·ion you are using.
· asrn
2. The "asm" source file containing the program code created in step 1 is ~ d
. t . te t
t h e 8051 assembler. 'f!1e assembler_coi:iverts t h e i~s ructio:11s into :tnachin
code. The assembler willproduce an obJect file (.obJ) and a hst file (.1st). Th
list file lists all the opcodes and address as well as error~ that the assemb!~
detected. If there is any error, remove the error and again assemble it.
3. Assembler require a third step called linking. The link program takes one
0
more object files and produces an abs<?lu~e object file with the extension "abs"
4. Next: .the "abs" file is f~d into ~ obj~ct to hex converter progr-;m ("OH
Program), which cre~tes a hex file with extension hex. This file is burnt int
ROM_of 805_1. Many recent ~~semb~er combine step 2, 3 and 4 into one step.
~ , '

~ Editor
Program

filename.asm
~

~ -
· Assembler
Program 1filename.1st
filename.obj
other obj flies
• ♦
Linker
P.rogram

filename.abs
'•
Object to Hex
Progra!"

filename.hex

ROM Sumer/Loader

Fig. 9.2 : Steps to Create a Program


blY programming
.,,1 AssefTl th 1
:· Give e supp Y to the 8051
5. counter has 000.,Q_H and st control cir~ 0 211
location 0000H-0f the pro arts execution t~ fe~ ~ower ~P, the program
ROM are used by interrugram ROM. Since the c t~e first opcode from
m 0030H ~ nd the fi ~t vectors, hence starting area of program
fro . - irst instructio normally user program starts
the interrupt vector locations. n at O000H is LlMP 0030H to bypass

HEX File Format =-~


-LL
~,2,
1
. (JJ ' f)-1 _-1-l- ~ ~
The HEX file format normally u d £
se or progra .
clJ- 1 , ~
vi ~
. CC AAAA TT DDD .... ........ D SS mmmg 8051 is as follows: ,_:_
: Each line starts with colo'n .
CC Tells loader how many bytes a . .
rem the hne (00 t O 16) AAAA for address by
. . data byte
nr:;t • •
rr TT= 00 if not last hne·-and Ol if 1·t 1s. l ast line
DD..... D Data or code
ss Checksum byte of everything in that line
To form checksum byte, bytes are added togeth hil dr .
.,,. complement of total sum gives the checksum ebrytwes ' SeS . oDpp1~g carhry.kThen
.~ . . urmg c ec -sum
!peration, all the bytes i n cluding checksum byte are added and the result must be
:ero.

9.3 ASSEMBLER DIRECTIVES ( \).{L ~t!-) ~V) ~ (o~ <,-


An assembly language program also contains certain directions to assemblers in
addition to the program instruction s which helps assembler in program memory
allocation and makes assembly language programmin g easy. These directions for
a55embler are called. Assembler Directives or Pseudo-Operations. These Assembler
directives are only used by assembler for assembly purpose .and are not converted to
anymachine code for CPU to execute. Thus Assembler Directives may be defined as :
Assembler Directives are the directions to the assembler and are used
by the assembler only. Assembler directives are also called Pseudo-
operations because these are processed before ,the actual start of
translation of assembly language instruction s into machine code by tlie
assembler.
Different assemblers may have different syntax for their assembler_directives
but functionally they are of similar type. The following are some widely used
assembler directives of the 8051. ~ ~

o~eccl.e - vJ\ivi 01 l t-C~ -\-b q~~~, , k


n, _ ·- ..., _ ~ ~ o~eQ.~ \S ~ t--e
O rotu'v' f~~ .
mmable Logic Controllers and Microco
214 ISHAN'S Progra ntroue,,

9.✓ow (define word) Directive


/ S~tax: DW XXXX
(.DW XXX:X for some assemblers) b XXXX ·
The DW (Define Word) is used to place 16 bit num er in memory. nw is
16 bit version of DB.
Example:
ORG013FH
DW6A39H
becomes:
Address Hex
013 F 6A
0140 39

~ d Directive
. Syntax : END
(.END for some assemblers)
· The END directive tells the assembler to stop assembling. The END directive
should be the last.line of the-8051 program as any line written after END directive is
ignored by the assembler.

9.4 NUMBERS
In assembly language programming, a :nwnber must start with a number from 0
to 9. If the number is in decimal, nothing is required to be post fixed with number.
But if the number is in hexaqecimal i,ystem, then 'H' should be post fixed with the
number. If a hex:a~d~ci:rp.al numb~:r !=!tart with digit from A to F, then a 0 (Zero) is
prefixed wit~ the num,µer otherwise the assembler will treat the number as label
and will give error. ··
Example
693
4168H
3AH
0BC3DH

9.5 SYMBOLS
.Normally assembl~rs inclu~es sy~bols for special function registers and for bits
Jf bit-addressabl e special function registers. This helps the programmer to write the

You might also like