You are on page 1of 10

Chapter 3: Assembly language and Instruction Set

Assembly Language of 8051 is quite similar to 8085 except that it has


instructions for MUL/D! an" other #it $elate" instructions% #efore
"iscussion of assembly language first &e &ill loo'up the a""ressing
mo"es of the 8085%
(
8051 A""ressing Mo"es
8051 has follo&ing fi)e a""ressing mo"es*
1% mme"iate A""ressing Mo"e
+% Direct Memory A""ressing Mo"e
,% $egister A""ressing Mo"e
-% $egister n"irect A""ressing Mo"e
1% mme"iate A""ressing Mo"e
mme"iate a""ressing is so.name" because the )alue to be store" in
memory imme"iately follo&s the operation co"e in memory% /hat is to
say0 the instruction itself "ictates &hat )alue &ill be store" in memory%
1or example0 the instruction2
M3! A0 4+0h
/his instruction uses mme"iate a""ressing because the Accumulator &ill
be loa"e" &ith the )alue that imme"iately follo&s5 in this case +0
6hexa"ecimal7% mme"iate a""ressing is )ery fast since the )alue to be
loa"e" is fixe" at comp at compile 8time it is not change"%
+% Direct A""ressing
Direct A""ressing is so 8name" because the )alue to be store" in memory
is obtaine" by "irectly retrie)ing it form another memory location% 1or
example
M3! A0 ,0h
/his instruction &ill rea" the "ata out of nternal $AM a""ress ,0
6hexa"ecimal7 an" store it in the Accumulator% Direct a""ressing is
generally fast since0 although the )alue to be loa"e" isn9t inclu"e" in the
instruction0 it is quic'ly accessible since it is store" in the 80519s nternal
$AM% t is also much more flexible than mme"iate A""ressing since the
)alue to be loa"e" is foun" at the gi)en a""ress:&hich may be )ariable%
Also0 it is important to note that &hen using "irect a""ressing any
instruction &hich refers to an a""ress 00h an" ;1h is referring to nternal
memory% Any instruction &hich refers to an a""ress bet&een 80h an" 11h
is referring to the% <1$ control registers that control the 8051
microcontroller itself%
,% $egister $eference nstruction
n these type of instructions the "ata is store" in the register an" the
instruction contains reference to that register% 1or example2
M3! A0 $1
/his instruction &ill copy content of $1 register in the Accumulator% =e
can use $egisters $0 to $;0 an" <1$ in such a type of instructions% 1or
example2
M3! A0 ><=
-% $egisters n"irect A""ressing Mo"e
n"irect a""ressing is a )ery po&erful a""ressing mo"e &hich in many
cases pro)i"es an exceptional le)el of flexibility% n"irect a""ressing
appears as follo&s2
M3! A0?$0
/his instruction causes the 8051 to analy@e the )alue of the $0 register%
/he 8051 &ill then loa" the accumulator &ith the )alue from nternal
$AM &hich is foun" at the a""ress in"icate" by $0% 1or example0 let9s
say $0 hol"s the )alue -0h an" nternal $AM a""ress -0h hol"s the
)alue A;h% =hen the abo)e instruction is execute" the 8051 &ill chec'
the )alue of $0% <ince $0 hol"s -0h the 8051 &ill get the )alue out of
nternal $AM a""ress -0h 6&hich hol"s A;h7 an" store it in the
Accumulator% /hus0 the Accumulator en"s up hol"ing
A;h%
n"irect a""ressing al&ays refers to nternal $AM5 it ne)er refers to an
<1$% /hus0 in a prior example &e mentione" that <1$ BBh can be use" to
&rite a )alue to the serial port% /hus one may thin' that the follo&ing
&oul" be a )ali" solution to &rite the )alue C19 to the serial port5
M3! $004BBh 5 Loa" the a""ress of the serial port
M3! ?$00401h 5<en" 01 to the serial port:=$3DEFF
/his is not )ali"% <ince in"irect a""ressing al&ays refers to nternal $AM
these t&o instructions &oul" &rite the )alue 01h to nternal $AM a""ress
BBh on an 805+% 3n an 8051these t&o instructions &oul" pro"uce an
un"efine" result since the 8051only has 1+8 bytes of nternal $AM%
>rogram 1lo&
=hen 8051 is first initiali@e"0 it resets the >G to 0000h% /he 8051 then
begins to execute instructions sequentially in memory unless a program
instruction cause the >G to be other&ise altere"% /here are )arious
instructions that can mo"ify the can mo"ify the )alue of the >G5
specifically con"itional branching instructions0 "irect Humps an" calls0
an" IreturnsJ from subroutines% A""itionally0 interrupts0 &hen enable"0
can cause the program flo& to "e)iate from it9s other&ise sequential
scheme% /here are t&o types of #ranching*
1% Gon"itional #ranching
+% uncon"itional #ranching
Abo)e t&o type of branching instructions are "i)i"e" into three
categories*
<hort Hump Absolute Kump Long Kump
/hese three type of Humps can be seen in follo&ing figure
Kump /ype $ange A""ress /ype
<hort Kump .1+8 to L1+; #ytes 6MA7 $elati)e A""ress
Hump Absolute memory +N #loc' of 6<A7 <hort A""ress
Long Kump 0000O to 1111O 6AA7 Absolute A""ress
/he range specifie" is gi)en form the a""ress location &here instruction
is store"% Gon"itional #ranching is al&ays of short type an"
uncon"itional branch is of three types Pou can also get the "etail from
follo&ing paragraph%
6i7 Gon"itional #ranching
/he 8051 contains a suite of instructions &hich0 as a group0 are
referre" to as Icon"itional branchingJ instructions% /hese instructions
cause program execution to follo& a non.sequential path if a certain
con"ition is true% /a'e for example the K# instruction% /his instruction
means IKump if #it <et%J An example of the K# instruction might be2
K# -5h0 OMLL3
D3>
OMLL32 *
n this case the 8051 &ill analy@e the contents of bit -5h% f the bit is
set program execution &ill Hump imme"iately to the label OMLL30
s'ipping the D3> instruction% f the bit is not set the con"itional
branch fails an" program execution continues0 as usual0 &ith the D3>
instruction &hich follo&s%
Gon"itional branching is really the fun"amental buil"ing bloc' of
program logic since all I"ecisionsJ are accomplishe" by using
con"itional branching% Gon"itional branching can be
/hough of as the I1*/OMDJ structure in 8051 assembly language%
An important note &orth mentioning about con"itional branching is
that the program many only branch to instructions locate" &ithin 1+8
bytes prior to or 1+; bytes follo&ing the a""ress &hich follo&s the
con"itional branch instruction% /his means that in the abo)e example
the label OMLL3 must be &ithin L/.1+8 bytes of the memory a""ress
&hich contains the con"itional branching instruction%
Direct Humps
=hile con"itional branching is extremely important0 it is often
necessary to ma'e a "irect branch to a gi)en memory location &ithout
basing it on a gi)en logical "ecision% /his is gi)en memory a""ress
&ithout consi"ering any con"itions%
/his is accomplishe" in the 8051using IDirect Hump an" GallJ
instructions% As illustrate" in the last paragraph0 this suite of
instructions causes program flo& to change uncon"itionally%
Gonsi"er the example2
LKM> DM=QADD$M<<
%
+,
%
%
DM=QADD$M<<2*
/he LKM> instruction in this example means JLong Kump%J =hen the
8051 executes this instruction the >G is loa"e" &ith the a""ress of
DM=QADD$M<< an" program execution continues sequentially from
there% /he ab)ious "ifference bet&een the Direct Hump an" Gall
instructions an" the con"itional branching is that &ith
Direct Humps an" Galls program flo& al&ays changes% =ith
con"itional branching program flo& only changes if a certain
con"ition is true% t is &orth mentioning that0 asi"e from LKM>0 there
are t&o other instructions &hich cause a "irect Hump to occur2 the
<KM> an" AKM> comman"s% 1unctionally0 these t&o comman"s
perform the exact same function as the LKM> comman":that is to
say0 they al&ays cause program flo& to continue at the a""ress
in"icate" by the comman"% Oo&e)er0 <KM> an" AKM> "iffer in the
follo&ing &ays2
/he <KM> comman"0 li'e the con"itional branching instructions0 can
only Hump to an a""ress &ithinL/.1+8 bytes of the <KM> comman"%
/he AKM> comman" can only Hump to an a""ress that is in same +'
bloc' of memory as the AKM> comman"% /hat is to say0 if the AKM>
comman" is at co"e memory location A50h0 it can only "o a Hump to
a""resses 0000h through 0;11h 60 through +0-;0 "ecimal7%
Pou may be as'ing yourself0 I=hy &oul" &ant to use the <KM> or
AKM> comman" &hich ha)e restrictions as to ho& far they "o the
same thing as the LKM> comman" &hich can Hump any&here in
memoryRJ /he ans&er is simple2 /he LKM> comman" requires three
bytes of co"e memory &hereas both the <KM> an" AKM> comman"s
require only t&o% /hus0 if you are "e)eloping an application that has
memory restrictions you can often sa)e quite a bit of memory using
the +.byte AKM>/<KM> instructions instea" of the ,.byte instruction%
$ecently0 &rote a program that require" +100 bytes of memory but
ha" a memory restriction
3f +' 6+0-8 bytes7% "i" a search /replace changing all LKM>s to
AKM>s an" the program shrun' "o&n to 1B50 bytes% /hus0 &ithout
changing any logic &hatsoe)er in my program sa)e" 150 bytes an"
&as able to meet my +0-8 byte memory restriction%
D3/M2 <ome quality assemblers &ill actually "o the abo)e con)ersion
for you automatically% /hat is theyCll automatically change your
LKM>s to <KM>s &hene)er possible% /his is a nifty an" )ery po&erful
capability that you may &an to loo' for in an assembler if you plan to
"e)elop many proHects that ha)e relati)ely tight memory restrictions%
Direct Galls
Another operation that &ill be familiar to seasone" programmers is the
LGALL instruction% /his is similar to a IEosubJ comman" in #asic%
=hen the 8051 executes an LGALL instruction it imme"iately pushes
the current the current >rogram Gounter onto the stac' an" then
continues executing co"e at the a""ress in"icate" by the LGALL
instruction%
$eturns from $outines
Another structure that can case program flo& to change is the I$eturn
from <ubroutineJ instruction0 'no&n as $M/ in 8051 Assembly
Language% /he $M/ instruction0 &hen execute"0 returns to the a""ress
that is store" on the stac'%
/he $M/ comman" is "irect in the sense that it al&ays changes
program flo& &ithout basing it on a con"ition0 but is )ariable in the
sense that &here program flo& continues can be "ifferent each time
the $M/ instruction is execute" "epen"ing on from &here the
subroutine &as calle" originally%
nterrupt is a special feature &hich allo&s the 8051 to pro)i"e the
illusion of Imulti.tas'ingJ0 although in reality the 8051 is only "oing
one thing at a time the &or'e" IinterruptJ can often be substitute"
&ith the &or" Ie)ent I An interrupt is triggere" &here)er a
correspon"ing e)ent occurs% =hen the e)en occurs0 the 8051
temporarily puts Jon hol"J the normal execution of the program an"
executes a special section of co"e referre" to as an interrupt han"ler%
/he interrupt han"ler performs &hate)er special are require" to han"le
the e)ent an" then returns control to the 8051 at &hich point program
execution continues as if it ha" ne)er been interrupte"%
/he topic of interrupts is some&hat tric'y an" )ery important% 1or
that reason0 an entire chapter &ill be "e"icate" to the topic% 1or no&0
suffice it to say that nterrupts can cause program flo& to change%
nstruction <et0 /iming0 an" Lo& 8Le)el nformation
n or"er to un"erstan":an" better ma'e use of 8the 80510 it is necessary
to un"erstan" some un"erlying information concerning timing% /he 80510
operates base" on an external crystal% /his is an electrical "e)ice &hich
&hen energy is applie" emits pulses at a fixe" frequency% 3ne can fin"
crystals of )irtually any frequency "epen"ing on the application
requirements% =hen using an 80510 the most common crystal frequencies
are 1+ megahert@ an" 11%05B megahert@ &ith 11%05B being much more
common% =hy &oul" anyone pic' such an o"".ball frequencyR /here9s
real reason for it has to "o &ith generating bau" rates an" &e9ll tal' more
about it in the <erial Gommunication chapter% 1or the remain"er of this
"iscussion &e9ll assume that &e9re using an 11%05B MO@ crystal%
Microcontroller 6an" many other electrical systems7 use crystals to
synchroni@e operations% /he 8051 uses the crystal for precisely that2 to
synchroni@e it9s operation% Mffecti)ely0 the 805A1 operates using &hat are
calle" Imachine cycles% I A single machine cycle is the minimum amount
of time in &hich a single 8051 instruction can be execute"% Although
many instructions ta'e multiple cycles%
A cycle is0 in reality0 1+ pulses of the crystal% /hat is to say0 if an
instruction ta'es one machine cycle to execute0 it &ill ta'e 1+ pulses of
the crystal to execute% <ince &e 'no& the crystal is pulsing 11005B0000
times per secon" an" that one machine cycle is 1+ pulses0 &e can
calculate ho& many instruction cycles the 8051 can execute per secon"2
1105B0000/1+SB+1058,
/his means that the 8051 can execute B+10 58, single.cycle instructions
per secon"% <ince a large number of 8051 instructions are single 8cycle
instruction it is often consi"ere" that the 8051 can execute roughly 1
million instructions per secon"0 although in reality it is less:an"
"epen"ing on the instructions being use"0 an estimate of about A000000
instructions per secon" is more realistic%
1or example0 if you are using exclusi)ely +.cycle instructions you &oul"
fin" that the 8051 &oul" execute -A00;B1 instructions per secon"% /he
8051 also has t&o really slo& instructions youC" fin" performance to be
about +,00,B5 instructions per secon"%
t is again important to emphasi@e that not all instructions execute in the
sa)e others require t&o machine 6+- crystal pulses07 an" the t&o )ery
slo& math operations require four machine cycles 6-8 crystal pulses7%

You might also like