Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Arduino

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

SECTION 1.

2: OVERVIEW OF THE 8051 FAMILY

I. 128 bytes of RAM, 4K bytes of on-ehip ROM, [our 8-bit JlO pons.
2. The 8052 has everything that the 8051 has, plus an extra timer, and the on-chip ROM is 8K
bytes instead of 4K bytes. The RAM in the 8052 is 256 bytes instead of 128 bytes.
3. Both the 8051 and the 8031 have 128 bytes of RAM and the 8052 has 256 bytes.
4. (a) 4K bytes (b) 8K bytes (c) OK bytes
5. 8
6. The main difference is the type of on-chip ROM. III the 8751 it is UV-EPROM; in the
AT89C51 it is flash; and in the DS89C420130 it is flash with a loader on the ehip.
7. True
8. True
9. 16K
10. 256

- CHAPTER 1: THE 8051 MICROCONTROLLERS 35

___ ...-z _
36
CHAPTER 2

8051 ASSEMBLY
LANGUAGE
PROGRAMMING
OBJECTIVES

Upon completion of this chapter, you will be able to:

» List the registers of the 8051 microcontroller


» Manipulate data using the registers and MOV instructions
» Code simple 8051 Assembly language instructions
» Assemble and run an 8051 program
» Describe the sequence of events that occur upon 8051 power-up
» Examine programs in ROM code of the 805]
» Explain the ROM memory map of the 805]
» Detail the execution of 8051 Assembly language instructions
» Describe 8051 data types
» Explain the purpose of the PSW (program status word) register
» Discuss RAM memory space allocation in the 8051
» Diagram the use of the stack in the 8051
» Manipulate the register banks ofthe 8051

37

--17 _
In Section 2.1 we look at the inside of the 8051. We demonstrate some of
the widely used registers of the 8051 with simple instructions such as MOV and
ADD. In Section 2.2 we examine Assembly language and machine language pro-
gramming and define terms such as mnemonics, opcode, operand, etc. The process
of assembling and creating a ready-to-run program for the 8051 is diseu sed in
Section 2.3. Step-by-step execution of an 8051 program and the role of the pro-
gram counter are examined in Section 2.4. In Section 2.5 we look at some widely
used Assembly language directives, pseudocode, and data types related to the
8051. In Section 2.6 we discuss the flag bits and how they are affected by arith-
metic instructions. Allocation of RAM memory inside the 8051 plus the stack and
register banks of the 8051 arc discussed in Section 2.7.

SECTION 2.1: INSIDE THE 8051

. In this section we examine the major registers of the 8051 and show their
use with the simple instructions MOV and ADD.

Registers
In the CPU, reg-
isters arc used to store
information temporarily. That information could be a byte of data to be proce d
or an address pomtmg to the data to be fetched. The vast rna' . es e ,
ters arc 8-blt registers In the 8051 th . I jonty of 8051 regis-
. ere IS on y one data type' 8 bits TI 8 bi f
a register arc shown in the diagram from the MSB .', ' ,le It 0
LSB (least significant bit) DO With 8 bi d (most significant bit) 07 to the
. 1 an - It ara type a d I
must be broken into 8-bit chunk be' . ' ny ata arger than 8 bits
. . s erore It IS process, d S'
number 01' registers in the 8051 'II eo. 1I1eethere arc a large
, we WI concentrate on f the wi
general-purpose registers and cover . I . some 0 t e widely u ed
r------=--~ A endi specia registers 111 future chapters, See
pp x A.2 for a complete list of 8051 .
I A .
I registers,

I B I DPTR
I RO I [_D_PH_I DPL

I RI I -----
PC
\ R2 I [ PC (program counter)

I R3 ] F'
Igure 2-1 (b). Some 8051 16-b't R .
I R4 ]

I Rsl
J
The most widel .
a\<cUm"I"fiO' D bOY used registers of the 8051
l
[
R6~''D

R7
]
l 1IJ l'lt
J
iIt._ ..", • RI R2 R
pointer), and Pc ' 3, R4, R,
0fhe above registers arc 8 ,{p gram COUn er . All
arc
6, R7,

'";1:::~-:-:-:----
1
program COUnter Iieaccuml-OilS, exce(lt DPTR a n d tne
fo' h
Figure 2-1 (a), Some 8-bit rail arithmetie and 10 ' , u ator, reglste A is used
Registers or the 8051 the use of th esc registersglc Instructions
. '
,0 understand
Context oftw' .' we will show th .
o Simple Instructions Mo em In the
38 , Vand ADD.
MOV instruction

Simply stated, the MOV instruction copies data from one location to anoth-
er, It has the following format:

MOV destinatlon,source ;copy source to dest.

This instruction tells the CPU to move (in reality, copy) the source operand
to the destination operand, For example, the instruction "MOV A, RO" copies the
contents of register RO to register A, After this instruction is executed, register A
will have the same value as register RO, The MOV instruction does not affect the
source operand, The following program first loads register A with value 55H (that
is 55 in hex), then moves this value around to various registers inside the CPU,
Notice the "#" in the instruction, This signifies that it is a value, The importance
of this will be discussed soon,

MOV A,#55H ;load value 55H into reg. A


MOV RO,A ;copy contents of A into RO
; (now A=RO=55H)
MOV Rl,A ;copy contents of A into Rl
; (now A=RO=Rl=55H)
MOV R2,A ;copy contents of A into R2
;now A=RO=Rl=R2=5SH)
MOV R3,#95H ;load value 95H into R3
; (now R3=95H)
MOV A,R3 ;copy contents of R3 into A
;now A=R3=95H)

When programming the 8051 rnicrocontroller, the following points should


be noted:
I, Values can be loaded directly into any of registers A, B, or RO - R7, However,
to indicate that it is an immediate value it must be preceded with a pound sign
(#). This is shown next.

MOV A,#23H ;load 23H into A (A=23H)


MOV RO,#12H ;load 12H into RO (RO=12H)
MOV Rl,#lFH ;load IFH into Rl (Rl=lFH)
MOV R2,#2BH ;load 2BH into R2 (R2=2BH)
MOV B,#3CH ;load 3CH into B (B=3CH)
MOV R7,#9DH ;load 9DH into R7 (R7=9DH)
MOV R5, #OF9H ;load F9H into R5 (R5=F9H)
MOV R6,#12 ;load 12 decimal (OCH)
;into reg. R6 (R6=OCH)

otice in instruction "MOV R5, #0 F9H" that a 0 is used between the # and
F to indicate tbat F is a hex number and not a Ie er. In other words "MOV
R5, #F9H" will cause an error,

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 39


2. If values 0 to F are moved into an 8-bit register, the rest of the bits are assumhed
to be all zeros. For example, in "MOV A, #5" the result will be A = 05; t at
is, A = 00000 I0 I in binary.

3. Moving a value that is too large into a register will cause an error.
MOV A,#7F2H ;ILLEGAL: 7F2H > 8 bits (FFH)
MOV R2,#456 ;ILLEGAL: 456 > 255 decimal (FFH)

4. A value to be loaded into a register must be preceded with a pound sign (#).
Otherwise it means to load from a memory location. For example "MOV
A, 17H"means to move into A the value held in memory location 17H, which
could have any value. In order to load the value 17H into the accumulator we
must write "MOV A, # 1 7H"with the # preceding the number. Notice that the
absence of the pound sign will not cause an error by the assembler since it is a
valid instruction. However, the result would not be what the programmer
intended. This is a common error for beginning programmers in the 8051.
ADD instruction
The ADD instruction has the following format:

ADD A,source
;ADD the Source operand
;to the accumulator

The ADD instnletion tells the CPU to add the Source byte to register A and
put
b the result In register A. To add two numbers such as 25H and 34H h
e move d to a register
. and then added together: ,cae can

MOV A,#25H
;load 25H into A
MOV R2,#34H
ADD A,R2 ;load 34H into R2
;add R2 to accumulator
; (A = A + R2)

- Executing the program above results in A = 59H (25H + 34 =


R2 - 34H. Notice that the Content of R2 d h H 59H) and
be wntrcn
. .111 many ways depending 0 Ih'oes not c ange .' The p rogram a b ove can
' n e registers used. Another way might be:
MOV RS,#25H 'load 2SH .
MOV R7 #34H
, . oad 34H Into
'1
. R5 (R5=25H)
MOV A #0
ADD A,R5
,
'
'load
: dd
°. Into R7 (R7=34H)
Into A (A=O,clear A)
,a to A Content of R5
ADD A,R7 ;where A = A + R5
;add to A Content of R7
;where A = A + R7
. The program above reSUlts in A _
wruc the same program One . - 591-1.There are always many w
pro b . question that might ays to
gram a ovc, is Whether it is neeess COmeto mind after looking at the
40 ary to move both data items into registers
before adding them together. The answer is no, it is not necessary. Look at the fol-
lowing variation of the same program:

MOV A,#25H ;load one operand into A (A~25H)


ADD A,#34H ;add the .second operand 34H to A

In the above case, while one register contained one value, the second value
followed the instruction as an OReland. This is calle an immediate 0 erand. The
examples shown so far for the 00' nstructi n indicate that the source 0 erand
can be either a register or immediate data, but the destination must w ys be rcg
list r , he accumulator. n other words, an instruction such as "ADD R2, #12H"
is invalid since register A (accumulator) must be involved in any arithmetic oper-
ation. Notice that "ADD R4,A" is also invalid for the reason that A must be the
destination of any arithmetic operation. To put it simply: In the 8051, register A
must be involved and be the destination for all arithmetic operations. The forego-
ing discussion explains why register A is referred to as the accumulator. The for-
mat for Assembly language instructions, descriptions of their use, and a listing of
legal operand types are provided in Appendix A.I.
There are two 16-bit registers in the 8051: PC (program counter) and
OPTR (data pointer). The importance and use of the program counter are covered
in Section 2.3. The OPTR register is used in accessing data and is discussed in
Chapter 5 where addressing modes are covered.

Review Questions
I. Write the instructions to move value 34H into register A and value 3FH into
register B, then add them together.
2. Write the instructions to add the values 16H and COHo Place the result in reg-
ister R2.
3. True or false. No value can be moved directly into registers RO - I3-7.
4. What is the largest hex value that can be moved into an 8-bit register? What is
the decimal equivalent of the hex value?
5. The vast majority of registers in 8051 are __ bits.

SECllON 2.2: INTRODUCTION TO 8051 ASSEMBLY PROGRAMMING

In this section we discuss Assembly language format and define some.


widely used terminology associated with Assembly language programming.
While the CPU can work only in binary, it can do so at a very high speed.
For humans, however, it is quite tedious and slow to deal with Os and Is in order
to program the computer. A progJ.:amthat consists of Os and Is is called machine
/anguag In the early days of the computer, programmers coded programs in
machine language. Although the hexadecimal system was used as a more efficient
way to represent binary numbers, the process of working in machine code was still
cumbersome for humans. Eventually, Assembly languages were developed that
provided mnemonics for the machine code instructions, Ius other features that
made programming faster and less rone to error. [Ihe term mnemonic is frcque t-
IXused in comguter science and engineering literature to refer to codes and abbr -

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 41

__ .dtn-.__
viations that arc relatively easy to--lG!!!£.mber.ssembly language rograms must
be translated into machine code by a program called an assemoler Assembl J -
uage is referred 10as a 10 Ilanlmage because it deals directly with the inter-
nal tructure of the CPU. To program in Assembly language, the programmer must
know all the registers of the CPU and the size of each, as well as other dctai Is.
Today, one can use many different programming languages, such as
SA IC. Pascal. C, CH. Java>rnd numerous others. These languages are called
high-level languages cause t rogJ!mmer ooes not ha e toJ>~ cgneemed with
the internal details of the CPU. hereas an assembler is used to translate an
Assembly language program into machiPc code (somcti also called object
code or opcode for operation c~e), high- evel anguages are translated int
rftiIehinecode by a program called a compil . For instance, to write a program in
, one must usc a C compiler to trans ate t e program into machine language. Now
we look at 805I Assembly language format and use an 8051 assembler to create a
ready-to-run program.

Structure of Assembly language


, An Assembly language program consists of, among other things, a series
of lines of Assembly language instructions. An Assembly language instruction
consists of a mnemonic, optionally followed by one or two operands. The
operands arc the data Items being manipulated, and the mnemonics arc the com-
mands to the CPU, tellIng it what to do with those items.

ORG OH ;start (origin) at location


MOV RS,#2SH 0
;load 25H into R5
MOV R7,#34H ;load 34H into R7
MOV A, #0 ;load 0 into A
ADD A,R5 ;add contents of RS
to A
inow A = A + R5
ADD A,R7 ;add contents of R7
to A
jnow A = A + R7
ADD A,#12H ;add to A value
12H
inow A = A +
HERE:SJMP HERE 12H
;stay in this loop
END ;end of asm source
file

Program 2-1: Sample of an Assembly L


anguagc Program
A given Assembly language pro
rnents, or lines, which are either Assem~ra~~see Program 2-1) is a series of state-
MOV,. or statements called directives W~. guage Instructions such as ADD and
directives (al 0 called ps do-:
::I:~PI~~ .
"
SJ~v~
lie InstructIOns tell the CPU h
eu O-Instructlon)'
:~et~p~o~e program while the
,
~;;~~i~~
w at to do
to the assembler. Fo;
, RG and END arc direct'. . II1struCtionsare com-
42 rves to the assembler. ORG tells the
assembler to place the opeode at memory location 0 while END indicates to the
assembler the end of the source code. In other words, one is for the start of the pro-
gram and the other one for the end of the program.

An Assembly language instruction consists of four fields:

[label:] mnemonic [operands] [;comment]

Brackets indicate that a field is optional, and not all lines have them.
Brackets should not be typed in. Regarding the above format, the following points
should be noted.

I. The label field allows the program to refer to a line of code by name. Thc label
field cannot exceed a certain number of characters. Check your assembler for
the rule.

2. The Assembly ianguage mnemonic (instruction) and operand/s) fields togeth-


er perform the real work of the program and accomplish the tasks for which
the program was written. In Assembly language statements such as

ADD A, B
MOV A, #67

ADD and MOY are the mnemonics, which produce opcodes; and "A, B" and
"A, #67" are the operands. Instead of a mnemonic and an operand, these two
fields could contain assembler pseudo-instructions, or directives. Remember
that directives do not generate any machine code (opeode) and are used only
by the assembler, as opposed to instructions that are translated into machine
code (opcode) for the CPU to execute. In Program 2-1 the commands ORG
(origin) and END are examples of directives (some 8051 assemblers usc .ORG
and .END). Check your assembler for the rules. More of these pseudo-instruc-
tions are discussed in detail in Section 2,5.

3. The comment field begins with a semicolon comment indicator ";".


Comments may be at the end of a line or on a line by themselves, The assem-
bler ignores comments, but they are indispensable to programmers, Although
comments are optional, it is recommended that they be used to describe the
program and make it easier for someone else to read and understand, or for the
programmer to remember what they wrote.

4. Notice the label "HERE" in the label field in Program 2-1. Any label referring
to an instruction must be followed by a colon symbol, ":". In the SJMP (short
jump instruction), the 8051 is told to stay in this loop indefinitely, If your sys-
tem has a monitor program you do not need this line and it should be deleted
from your program, In the next section we will see how to create a ready-to-
run program.

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 43


Review Questions
. ?
I What is the purpose of pseudo-instructions. .
2.. are translated by the assembler into mach me code, whereas
are not.
J. -T-ru-e-o-rccfa-cls-e-.
A-:-s-s-em~blylanguage is a high-Ievcllanguage.
4. Which of the following produces opcode? OH (d) SJMP HERE
(a) ADD A,R2 (b) MOV A,#12 (c) ORG 200
5. Pseudo-instructions arc also called CU. If Th
6. True or false. Assembler directives are not used by the P itsclt. ey are
simply a guide to the assembler.
7. In question 4, which one is an assembler directive?

SECTION 2.3: ASSEMBLING AND RUNNING AN 8051


PROGRAM

Now that the basic form of an Assembly language program has been given,
the next question is: How it is created, assembled, and made ready to run? The
steps 10 create an executable Assembly language program are outlined as follows.

I. First we use an editor to type in a ED1TOR


program similar to Program 2-1. PROGRAM
Many excellent editors or word myfilc .asm
processors are available that can be
used to create and/or edit the pro-
ASSEMBLER
gram. A widely used editor is the
PROGRAM
MS-DOS EDIT program (or
Notepad in Windows), which comes my/ile. ISI.J
with all Microsoft operating systems.
Notice that the editor must be able to
my lile.obj
produce an ASCII file. For many
assemblers, the file names follow the
r
LINKER
OI her obj f les

usual DOS conventions, but the PROGRAM


source file has the extension 'as"!"
r "src", depending on which assco-
bier you are using. Check YOur
+
mynle.abs
a sembler for the convention. The
"asm" extension for the SOurce file is ~
used by an assembler in the next OH
step.
PROGRAM

2. The "asm'' so fil


urce 1 e Containing the +
my/ile.hex
program code created in step I is fed
to an 8051 assembler. The assembler
conVerts Ihe instructions into Figure 2-2. Steps to Create a Program

44
machine code. The assembler will produce an object file and a list file. The
extension for the object file is "obj' while the extension for the list file is "lst".

3. Assemblers require a third step called linliing. The liii:Kprogram taRes one or
more object files ana produces an a5soluteobject file with the extension "abs".
This ab de is used by 8051 trainers t at have a monitor program.

4. Next, the "abs" file is fed into a program called "OH" (object to hex convert-
er), which creates a file with extension "hex" that is ready to burn into ROM.
This program comes with all 8051 assemblers. Recent Windows-based assem-
blers combine steps 2 through 4 into one step.

More about "asm" and "obj" files


The "asm" file is also called the source file and for this reason some asscm-
biers require that this file have the "src" extension. Check your 8051 assembler to
see which extension it requires. As mentioned earlier, this file is created with an
editor such as DOS EDIT or Windows Notepad. The 8051 assembler converts the
asm file's Assembly language instructions into machine language and provides the
obj (object) file. In addition to creating the object file, the assembler also produces
the lst file (list file).

1stfile
The 1st (list) file, which is optional, is very useful to the programmer
because it lists all the ope odes and addresses as well as errors that the assembler
detected. Many assemblers assume that the list file is not wanted unless you indi-
cate that you want to produce it. This file can be accessed by an editor such as
DOS EDIT and displayed on the monitor or sent to the printer to produce a hard
copy. The programmer uses the list file to find syntax errors. It is only after fixing
all the errors indicated in the lst file that the obj file is ready to be input to the link-
er program.

1 0000 ORG OH ;start (origin) at 0


2 0000 7D25 MOV R5,#25H ;load 25H into R5
3 0002 7F34 MOV R7,#34H ;load 34H into R7
4 0004 7400 MOV A,#O ;load 0 into A
5 0006 2D ADD A,R5 ;add contents of R5 to A
;now A = A + R5
6 0007 2F ADD A,R7 ;add contents of R7 to A
;now A = A + R7
7 0008 2412 ADD A, #12H ;add to A value 12H
;now A = A + 12H
8 OOOA 80FE HERE: SJMP HERE ;stay in this loop
9 oooe END ;end of asm source file

Program 2-1: List File

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 45


Review Questions
True or false. The DOS program EDIT produces an ASCII ,~Ie." "src"
~: True or false. Generally, the extension °df thedsbourtehefi 11eE~~
l s p~~gram~
Which of the following files can be pro uce y e o O S .
3. (a) myprog.asrn (b) myprog.obj (c) myprog.exe (d) myprog.lst ry
Which of the following files IS produced hy an.8.051 assemblIT.
4. (a) myprog.asm (b).!!1yprog.obj (c) myprog.hex (d) myprog.lst
5. Which of the following files lists syntax errors? (d) It
(a) myprog.asm (b) myprog.obj (c) myprog.hex myprog. s

SECTION 2.4: THE PROGRAM COUNTER AND ROM


SPACE IN THE 8051

In this section we examine the role of the program counter (PC) register in
executing an 8051 program. We also discuss ROM memory space for various 8051
family members.

Program counter in the 8051


Another important register in the 8051 is the PC (program counter). The
program counter points to the address of the next instruction to be executed. As the
CPU fetches the opcodc from the program ROM, the program counter is incre-
mcnted to point to the next instruction. The program counter in thc 8051 is 16 bits
wide. This means that the 8051 can access program addresses 0000 to FFFFH, a
total of 64K bytes of code. However, not all members of the 8051 have the entire
64K bytes of on-chip ROM installed, as we will see soon. Where does the 8051
wake up when it is powered? We will discuss this important topic next.

Where the 8051 wakes up when it is powered up


One question that we must ask about any microcontroller (or microproces-
sor) is: At what address does the CPU wake up upon applying power to it? Each
microprocessor is different. In the case of the 8051 family (that is, all members
regardless of the maker and variation), the microcontroller wakes up at memory
address 0000 when it is powered up. By powering up we mean applying Vee to
the RESET pin as discussed in Chapter 4. In other words, when the 8051 is pow-
ered up, the PC (program counter) has the value of 0000 in it. This means that it
expects the first ope ode to be stored at ROM address OOOOH. For this reason in the
8051 system, the first opcode must be burned into memory location OOOOH of pro-
gram ROM smce this IS where it looks for the first instruction when it is booted.
We achieve. this by the ORG statement in the source program as shown earlier.
Next, we diSCUSSthe step-by-step action of the program counter in fctchin and
executll1g a sample program. g

Placing code in program ROM

To get a better understanding or the role of the progra n tcr i C


.111g an d executIng
. a program we examine the act' f h I coun cr 111 retch,
each instruction is fetched and executed Fi st Ion 0 t e program counter as
. rsr, we examine once more the list file
of the sample program and how the code is placed in the ROM of an 80S I chip.
As we can see, the opcode and operand for each instruction are listed on the left
side of the list file.

1 0000 ORG OH ;start at location a


2 0000 7025 MOV R5,#25H ;load 25H into R5
3 0002 7F34 MOV R7,#34H ;load 34H into R7
4 0004 7400 MOV A,#O ;load a into A
5 0006 20 ADD A,R5 ;add contents of R5 to A
inow A = A + R5
6 0007 2F ADD A,R7 ;add contents of R7 to A
inow A = A + R7
7 0008 2412 ADD A,#12H ;add to A value 12H
;now A = A + 12H
8 DaDA 80FE HERE: SJMP HERE ;stay 1n this loop
9 DaDe END ;end of asm source file

Program 2-1: List File


ROM Address Machine Language Assembly Language
0000 7025 MOV R5, #25H
0002 7F34 MOV R7,#34H
0004 7400 MOV A,#O
0006 20 ADD A, R5
0007 2F ADD A,R7·
0008 2412 ADD A, #12H
OOOA 80FE HERE: SJMP HERE

After the program is burned into ROM of an 80S 1 family member such as
875 I or AT89S 1 or DSSOOO, the opcode and operand are placed in ROM memory
locations starting at '0000 as shown in the list below.
The list shows that address 0000 con-
tains 7D, which is the opcode for moving a Program 2-1: ROM Contents
value into register RS, and address 000 J con- Address Code
tains the operand (in this case 2SH) to be 0000 7D
moved to RS. Therefore, the instruction "MOV 0001 25
R5, #25H" has a machine code of "7D2S", 0002 7F
where 7D is the opcode and 25 is the operand. 0003 34
Similarly, the machine code "7F34" is located 0004 74
in memory locations 0002 and 0003 and rep- 0005 00
resents the opcodc and the operand for the 0006 2D
instruction "MOV R7, #34H". In the same 0007 2F
way, machine code "7400" is located in mem- 0008 24
ory locations 0004 and 0005 and represents 0009 12
the opcode and the operand for the instruction OOOA 80
"MOV A, #0".The memory location 0006 has 0008 FE
the opcode of 2D, which is the opcode for the

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 47


instruction "ADD A, RS" and memory location 0007 has the content 2F,. wh,i,ch is
the opcode for the "ADD A, R7" instruction. The opcode for the mstrucnon ADD
A #12H" is located at address 0008 and the operand 12H at address 0009. The
memory location OOOAhas the ope ode for the SJMP instruction and its target
address is located in location OOOB. The reason the target address IS FE IS
explained in the next chapter.

Executing a program byte by byte


Assuming that the above program is burned into the ROM of an 8051 chip
(or 8751, AT895I, or DS5000), the following is a step-by-step description of the
action of the 8051 upon applying power to it.

I. When the 8051 is powered up, the PC (program counter) has 0000 and starts
to fetch the first opcodc from location 0000 of the program ROM. In the case
of the above program the first opeode is 70, which is the code for moving an
operand to R5. Upon executing the opcode, the CPU fetches thc value 25 and
places it in R5. Now onc instruction is finished. Then the program counter is
incremented to point to 0002 (PC = 0002), which contains opcode 7F, the
opcode for the instruction "MOV R7, .. ".
2. Upon cxeeuting the ope ode 7F, the value 34H is moved into R7. Then the pro-
gram counter is incremented to 0004.
3. ROM location 0004 has the opeodc for thc instruction "MOV A, #0". This
instruction is executed and now PC = 0006. Notice that all the above instruc-
tions arc 2-byte instructions; that is, each one takes two memory locations.
4. Now PC = 0006 points to the next instruction, which is "ADD A, RS". This is
a l-byte instruction. After the execution of this instruction, PC = 0007.
5. The location 0007 has the opeode 2F, which belongs to the instruction "ADD
A, R7". This also is a l-bytc instruClion. Upon execution of this instruction
PC is incremented to 0008. This process goes on until all the instructions are
fetched. and executed. The fact that the program counter points at the next
msrrucnon to be executed explains why some microprocessors (notably the
x86) call the program counter the ins/ruction pointer.

ROM memory map in the 8051 family

. As we saw in the last chapter, some family members have only 4K bytes
of on-chip ROM (e.g., 8751, AT895 J) and some, such as the AT89C52 have 8K
bytes of ROM. Dallas Semiconductor's DSSOOO-32 h 32K b ' .
ROM . 0 a llas Semi -
as emlconduetor also has an 8051 with 6'4K bas ytcs of . on-chIp
Th·· I f '1 ytes of on-chip ROM .
e pomt to remember IS that no member of the 8051
64K bytes of opcode since the program counter' ami y can access more than
(0000 to FFFF address range). It must be noted th~tn:he 8051 IS a 16-~lt register
gram ROM inside the 8051 has the address of 0000 hile the first location of pro-
ferent depending on the size of the ROM h ' the last locatior, can be dif-
members, the 875 J and AT895 J have 4K b on t fe chiP: Among the 8051 family
0
ROM memory has memory addresses of06~~St O~~ChIP ROM. This 4K bytes of
lion of on-chip ROM of this 8051 h 0 FH. Thereforc, the first loca-
the address ofOFFFH. Look at E as tc ;ddress 01'0000 and the last location has
xarnp e -I to see how this is computed.
48
Example 2-1

Find the ROM memory address of each of the following 8051 chips.
(a) AT89C51 with 4KB (b) DS89C420 with 16KB (c) DS5000-32 with 32KB

Solution:

(a) With 4K bytes of on-chip ROM memory space, we have 4096 bytes' (4 x 1024 =
4096). This maps to address locations of 0000 to OFFFH. Notice that 0 is always
the first location. (b) With 16K bytes of on-chip ROM memory space, we have
16,384 bytes (16 x 1024 = 16,384), which gives 0000 - 3FFFH. (c) With 32K bytes
we have 32,768 bytes (32 x 1024 = 32,768). Converting 32,768 to hex, we get
8000H; therefore, the memory space is 0000 to 7FFFH.

0000
.. byte
..
0000
.. byte
. ,.
byte
..
0000

OFFF '--- __ ---J

8051
AT89C51 3FFF

DS89C420/30

7FFF
'------'
DS5000-32

Figure 2-3. 8051 On-Chip ROM Address Range


Review Questions
I. In the 8051, the program counter is bits wide.
2. True or false. Every member of the 8051 family, regardless of the maker,
wakes up at memory OOOOHwhen it is powered up.
3. At what ROM location do we store the first opcode of an 8051 program?
4. The instruction "MOV A, #44H" is a -byte instruction.
5. What is the ROM address space for the 8052 chip?g ~13

SECTION 2.5: 8051 DATA TYPES AND DIRECTIVES

In this section we look at some widely used data types and directives sup-
ported by the 8051 assembler.

8051 data type and directives


The 8051 microcontroller has only one data type. It is 8 bits, and the size
of each register is also 8 bits. It is the job of the programmer to break down data

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 49


larger than 8 bits (00 to FFH, or 0 to 255 in decimal) to be processed by the CPU.
For examples of how to process data larger than 8 bits,. see Chapter 6. The data
types used by the 8051 can be positive or neganvc. A discussion of SIgned num-
bers is given in Chapter 6.

DB (define byte)
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, or ASCII formats. For decimal, the "D" after the deci-
mal number is optional, but using "B" (binary) and "H" (hexadecimal) for the oth-
ers is required. Regardless of which is used, the assembler will convert the num-
bers into hex. To indicate ASCII, simply place the characters in quotation marks
('like this'). The assembler will assign the ASCII code for the numbers or charac-
ters automatically. The DB directive is the only directive that can be used to define
ASCII strings larger than two characters; therefore, it should be used for all ASCII
data definitions. Following are some DB examples:

ORG 500H
DATAl: DB 28 ;DECIMAL(lC in hex)
DATA2: DB 00110101B ;BINARY (35 in hex)
DATA3: DB 39H ;HEX
ORG 510H
DATA4: DB 11259111 ;ASCII NUMBERS
ORG 518H
DATA6: DB "My name is Joel! ;ASCII CHARACTERS

Either
C'
single or double
.
quotes can be used around ASCII strings . Thi1 scan
b e use ful tor stnngs, which contain a single quote such as "O'Leary". DB is also
used to allocate memory in byte-sized chunks.

Assembler directives

The following arc some more widely used directives of the 8051.

ORG (origin)

The ORG directive is used to indicate the be' .


number that comes after ORG can be cith . h ginning of the address. The
er 111 ex or In dee' I If h
not followed by H, it is decimal and the assernbl . una .. t e number is
assemblers usc" . ORG" (notice the dot) instead ofe,~wllI"convert tl to hex. Some
heck your assembler. ORG for the ongin dIrective.
EQU (equate)

This . is used
. to define a constant WIithout oec .
TI ic EQ U directive does not set aside stora e uPY1l1ga memory location.
stant value with a data label so that h g for a data item but associates a con-
. W cn the label a .
I
Slant va ue will be substituted for the label ' ppears In the program, its can-
er constant and then the consta t" . The follOWing uses EQU for the
n ISused to load the R3 . count-
register.
o
COUNT EQU 25

MOV R3,#COUNT

_When executing the instruction "MOV R3, #COUNT", the register R3 will
be loaded with the value 25 (notice the # sign). What is the advantage of using
EQU? Assume that there is a constant (a fixed value) used in many different places
in the program, and the programmer wants to change its value throughout. By the
use of EQU, the programmer can change it once and the assembler will change all
of its occurrences, rather than search the entire program trying to find every occur-
rence.

END directive

Another important pseudocode is the END directive. This indicates to the


assembler the end of the source (asm) file. The END directive is the last line of an
8051 program, meaning that in the source code anything after the END directive
is ignored by the assembler. Some assemblers use" . END" (notice the dot) instead
of "END".

Rules for labels in Assembly language


By choosing label names that are meaningful, a programmer can make a
program much easier to read and maintain. There are several rules that names must
follow. First, each label name must he unique. The names used for labels in
Assembly language programming consist of alphabetic letters in both uppercase
and lowercase, the digits 0 through 9, and the special characters question mark C),
period (.), at (@), underline U, and dollar sign ($). The first character of the label
must be an alphabetic character. In other words it cannot be a number. Every
assembler has some reserved words that must not be used as labels in the program.
Foremost among the reserved words are the mnemonics for the instructions. For
example, "MOY" and "ADD" are reserved since they are instruction mnemonics.
In addition to the mnemonics there are some other reserved words. Check your
assembler for the list of reserved words.

Review Questions
I. The directive is always used for ASCII strings.
2. How many bytes are used by the following?
DATA 1: DB "AMERICA"
3. What ~ the advantage in using the EQU directive to define a constant value?
4. How many bytes are set aside by each of the following directives?
(a) ABC DATA: DB "1234" (b) MY_DATA: DB "ABC1234"
5. State the contents of memory locations 200H - 205H for the following:
ORG 200H
MYDATA: DB "ABC123"

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 51


SECTION 2.6: 8051 FLAG BITS AND THE PSW REGISTER

Like any other microprocessor, the 8051 has a flag register to indicate
arithmetic conditions such as the carry bit. The flag register in the 8051 is called
the program status word (PSW) register. In this section we discuss various bits of
this register and provide some examples of how it is altered.

PSW (program status word) register


The program status word (PSW) register is an 8-bit register. It is also
referred to as the flag register. Although thc PSW register is 8 bits wide, only 6
bits of it arc used by the 8051. The two unused bits are user-definable flags. Four
of thc flags arc called conditional flags, meaning that they indicate some condi-
tions that result after an instruction is executed. These four are CY (carry), AC
(auxiliary carry), P (parity), and OV (overflow).
As seen from Figure 2-4, the bits PSW.3 and PSW4 are designated as RSO
and RS I, respectively, and are used to change the bank registers. They are
explained in the next section. The PSWS and PSWl bits are general-purpose sta-
tus flag bits and can be used by the programmer for any purpose. In other words,
thcy are user definable. See Figure 2-4 for the bits of the PSW register.

L-,C_Y__ A_C_,--_FO_I RSl I RSO I OV P

CY pswL Carry flag.


AC PSW6 Auxiliary carry flag.
FO PSWS
Available to the user for general purpose.
RSI PSW.4
RegIster Bank selector bit I.
R 0 PSW.3 Register Bank selector bit O.
OV PSW2 Overflow flag.
PSWI User-definable bit.
P PSW.O
Parity flag. Set/cleared by hardware each instuction cycle
10indicate an odd/even number of I bits in the accumulator.

RSt RSO Register Bank


0 0 Address
0
0 I OOH - 07H
I
I 0 08H - OFH
2
I I 10H - 17H
3
18H - IFH

The fOllOWingis a bricf ex I .


istcr, Thc impact of instructions o~ ~natlon of four of the flag bits of the PSW reg-
cse registers IS then discussed
2 .
CY, the carry flag

This flag is set whenever there is a carry out from thb D7 bit. This flag bit
is affected after an 8-bit addition or subtraction. It can also be set to 1 or 0 direct-
ly by an instruction such as "SETB C" and "CLR . C" where "SETB C" stands for
"set bit carry" and "CLR C" for "clear carry". More about these and other bit-
addressable instructions will be given in Chapter 8.
A C, the auxiliary carry flag

If there is a carry from D3 to D4 during an ADD or SUB operation, this bit


is set; otherwise, it is cleared. This flag is used by instructions that perform BCD
(binary coded decimal) arithmetic. See Chapter 6 for more information.
P, the parity flag

The parity flag reflects the number of Is in the A (accumulator) register


only. If the A register contains an odd number of Is, then P = I. Therefore, P = 0
if A has an even number of 1 s.
OV, the overflow flag

This flag is set whenever the result of a signed number operation is too
large, causing tbe high-order bit to overflow into the sign bit. In general, the carry
flag is used to detect errors in unsigned arithmetic operations. The overflow flag
is only used to detect errors in signed arithmetic operations and is discussed in
detail in Chapter 6.

ADD instruction and PSW ,


Table 2-1: Instructions That Affect
Next we examine the impact Flag Bits
of the ADD instruction on the flag bits
CY, AC, and P of the PSW register. Instruction CY OV AC
Some examples should clarify their ADD X X X
status. Although the flag bits affected ADDC X X X
by the ADD instruction are CY (carry SUBB X X X
flag), P (parity flag), AC (auxiliary MUL 0 X
carry flag), and OV (overflow flag) DIV 0 X
we will focus on flags CY, AC, and P DA X
for now. A discussion of the overflow RRC X
flag is given in Chapter 6, since it RLC X
relates only to signed number arith- SETB C J

metic. How the various flag bits are CLRC 0


used in programming is discussed in CPLC X
future chapters in the context of many ANL C, bit X
ANLC, /bit X
applications.
ORL C, bit X
See Examples 2-2 through 2-4
ORL C, /bit X
for the impact on selected flag bits as
MOVC, bit X
a result of the ADD instruction.
CJNE X
Note: X can be 0 or I .

CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 53


Example 2-2
Show the status of the CY, AC, and P flags after the addition of 38H and 2FH in the fol-
lowing instructions.
MOV A,#38H
ADD A,#2FH ;after the addition A=67H, CY=O

Solution:
38 00111000
+ 2F 00101111
67 0110011 I

CY = 0 since there is no carry beyond the 07 bit.


AC = 1 since there is a carry from the 03 to the 04 bit.
P = I since the accumulator has an odd number of Is (it has five Is).
Example 2-3

Show the status of the CY, AC, and P flags after the addition of9CH and 64H in the fol-
lowing instructions.
MOV A,#9CH
ADD A,#64H ;after addition A=OO and
CY=l
Solution:
9C 1001 I 100
+ 64 01100100
100 00000000

CY = I since there is a carry beyond the 07 bit.


AC = 1 since there is a carry from the 03 to the 04 bit.
p = 0 since the accumulator has an even number of Is (it has zero Is).
Example 2-4

Show
lowmgthe status of the CY, AC, and P flags after the addition of 88H and 93H in the II0 1-
mstruetlons.
MOV A,#88H
ADD A,#93H
;after the
addition A=lBH,CY=l
Solution:
88 10001000
+ 93 100100] I
liB
00011011

Y = I since there is a carry beyond the 07 bi


A_ = ~ since there is no carry from the 03 to ~~e .
p - 0 smee the accumulator has a D4 bit,
n even number of Is (it has four Is).

You might also like