Arduino
Arduino
Arduino
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
___ ...-z _
36
CHAPTER 2
8051 ASSEMBLY
LANGUAGE
PROGRAMMING
OBJECTIVES
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.
. 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:
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,
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,
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)
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.
__ .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.
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.
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.
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.
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.
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.
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.
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.
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
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.
. 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
8051
AT89C51 3FFF
DS89C420/30
7FFF
'------'
DS5000-32
In this section we look at some widely used data types and directives sup-
ported by the 8051 assembler.
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)
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
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"
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.
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
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.
Solution:
38 00111000
+ 2F 00101111
67 0110011 I
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
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