Chapter 2: 8051 Assembly Language Programming: - Microcontroller's
Chapter 2: 8051 Assembly Language Programming: - Microcontroller's
Summary:
- 8051 Microcontrollers (MC) assembly language programming structure will be demonstrate using simple
instructions like MOV and ADD
- Assembly language terms, such as, Op-code, operand ….. will be explained.
- Line by line execution of an 8051 program and the role of program counter will be examined.
- Assembly language directives and different data types supported by the MC will be explained.
- Program status word (PSW) and Fag bits and will be discussed.
- Allocation of internal memory (ROM and RAM) will be introduced.
• Microcontroller’s Registers are used to temporarily store information of the following types:
• A data byte to be processed by ALU
• A memory address of the stored data byte, needed to be fetched/stored before/after processing
• The vast majority of 8051 register are 8-bit registers, which can only store one byte (8-bit) of data
• The 8 bits of a register are shown from MSB=D7 to the LSB=D0. With an 8-bit data type, any data larger than
8 bits must be broken into 8-bit pieces before it is processed
1
Register Rn as destination. This can have the following formats:
1. MOV Rn, #data example: MOV R0, #25H
after execution, 25H R0
2. MOV Rn, A example: MOV R3, A
3. MOV Rn, direct example: MOV R0, 30H after execution,
data contents of PA=30HR0
Destination is DIRECT address. This can have the following formats:
1. MOV direct, #data example: MOV 30H, #25
2. MOV direct, A example: MOV 30H, A
3. MOV direct, Rn example: MOV 30H, R1
4. MOV direct, direct MOV 30H, 31H
5. MOV direct, @Ri (i=0 or 1) example: MOV 30H, @R0
Destination is INDIRECT address. This can have the following formats:
6. MOV @Ri (i=0 or 1), #data example: MOV @R0, #25
7. MOV @Ri (i=0 or 1), A example: MOV @R1, A
8. MOV @Ri (i=0 or 1), direct MOV @R0, 31H
2
Addition of signed ( +, -) numbers use OV flag (OV=set=1) to determine errors (either carry from D6D7
or D7 out, but not (OV=reset) if both carry’s present from D6D7 AND D7 out
• Problem: Write a program that will add 34H and 25H of byte data.
1) First we use an editor to type a program, many excellent editors Source Code
or word processors are available that can be used to create
and/or edit the program 8051
assembler
• Notice that the editor must be able to produce an ASCII file
• For many assemblers, the file names follow the usual DOS
conventions, but the source file has the extension “asm“ or
“src”, depending on which assembly you are using Machine Code
this file lists all
2) The “asm” source file containing the program code the Op-codes,
created in step 1 is fed to an 8051 assembler P. Addresses &
• Assembler converts instructions into machine code Assembler
• The assembler will produce an object and a list files errors (pg 45)
• The extension for the object file is “obj” while the
extension for the list file is “lst”
3) Assembler require a third step called linking
• The linker program takes one or more object code files and
produce an absolute object file with the extension “abs”
• This abs file is used by 8051 trainers that have a monitor
Object to Hex
program
Convert.
4) Next the “abs” file is fed into a program called “OH” (object to
hex converter) which creates a file with extension “hex” that is
ready to burn into ROM
• This program comes with all 8051 assemblers
• Recent Windows-based assemblers combine step 2
through 4 into one step.
3
Two Assembly language instructions exists: (1) That is assembled and (2) that is not assembled
Directives/Pseudo-instructions Not assembled (instruction for CPU. Ex: “ORG 00H” or “END”
instructions (some MC use .ORG) already discussed in 8088/8086 case.
Psedo-op
• The program counter points to the address of the next instruction to be executed
• Program counter (PC) is 16 bits wide (access 64K code) and auto increments after execution to point next ins.
• This means that it can access program addresses 0000 to FFFFH, a total of 64K bytes of code
• When powered up, all 8051 members start at memory address 0000 as P.C=0000H. This means the first op-code
to be executed is burned into ROM address 0000H and this is achieved by the ORG Directive in program code
ROM spaces: 8051 family has different on-chip ROM (up to 64 KB) for internal storage:
- Such as: AT89C51 4 Kbyte (KB); AT89C52 8 KB; DS89C420 16 KB; DS5000 32 KB;
• ‘DB’ Directives/Pseudo to initialize data:
- Same as 8086/8088 programming, the DB instruction can be used as follows: (remember need use ‘H’ for hex data)
ORG 500H
DATA1: DB 28 ; 1CH will be stored in PA=500H
DATA2: DB “25” ; ASCII code of 32H and 35H will be stored see table – next page
DATA3: DB “My Name is Ahmed” ; will store the ASCII codes of each ASCII letter.
4
ORG (origin) Directives/Pseudo
• 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 Directives/Pseudo
• 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) Directives/Pseudo This is used to define a constant without occupying a memory location
• This directive does not set aside storage for a data item but associates a constant value with a data label
• Can be changed in all the cases within the program by changing the definition
Section 2.6: 8051 FLAG bits and the PSW (program status word) Register:
• PSW or Program Status Word register, also referred to as the flag register, is an 8 bit register
• Six-bits out of eight-bits are used: four-bits are for conditional flags (PSW.7, PSW.6, PSW.2, PSW.0),
two-bits are used to change register banks (PSW.4, PSW.3) and two-bits are used for user defined general
purpose status flag bits (PSW.5, PSW.1).
• Out of 128D=7FH Byte RAM memory, 00H to 1FH (or 32D) bytes are used for Register Banks
Used with ADD/SUB for JC/JNC: SETB C set bit carry; CLR C clear carry
(Reg A only)
5
Ex1: Find how flags are affected for following two
line program: 0 1
MOV A, #38H 00111000B
ADD A, #2FH 00101111B
------------------------
register A 01100111 B
6
- So there is a problem of programming 8051 as BANK-1 uses the same RAM spaces of STACK. So we must not
use both at a time or use another RAM array to allocate for STACK (using SP register see pinnacle 52)
So if bank-1 is selected
MOV R1, #07H
is same as
MOV 09, #07H
As we have preciously
selected BANK-1 with
following instruction:
SETB PSW.3
- We need to move
STACK using “SP” reg
- SP register of CPU (default value=07H) access Bank 1 memory locations as 24 bytes of temporary stack.
7
Use of STACK Segment (default Bank 1): to temporary store data.
- PUSH (a) SPnew=SP+1; (b) Load Rn contents into PA SPnew .
- POP (a) load Rn contents, (b) SPnew=SP-1;
- If Bank-1 and STACK are both needed by program, then default SP=07H (PA08H to 1FH, 24 Bytes) cannot
be used. Other RAM locations are used as STACK MOV SP, #5FH PA of 1st stack location is 60H
Memory physical
Adresses
LAB Simulators:
8
Register A (Accumulator)
The most important of all special function registers, that’s the first comment about Accumulator which is also
known as ACC or A. The Accumulator (sometimes referred to as Register A also) holds the result of most of
arithmetic and logic operations. ACC is usually accessed by direct addressing and its physical address
isE0H. Accumulator is both byte and bit addressable. You can understand this from the figure shown below. To
access the first bit (i.e bit 0) or to access accumulator as a single byte (all 8 bits at once), you may use the same
physical address E0H. Now if you want to access the second bit (i.e bit 1), you may use E1H and for third bit E2H
and so on.
Register B
The major purpose of this register is in executing multiplication and division. The 8051 micro controller has a
single instruction for multiplication (MUL) and division (DIV). If you are familiar with 8085, you may now
know that multiplication is repeated addition, where as division is repeated subtraction. While programming
8085, you may have written a loop to execute repeated addition/subtraction to perform multiplication and
division. Now here in 8051 you can do this with a single instruction.
Ex: MUL A,B – When this instruction is executed, data inside A and data inside B is multiplied and answer is
stored in A.
Note: For MUL and DIV instructions, it is necessary that the two operands must be in A and B.
Note: Follow this link if you are interested in knowing about differences between a microprocessor and
microcontroller.
Register B is also byte addressable and bit addressable. To access bit o or to access all 8 bits (as a single byte),
physical address F0 is used. To access bit 1 you may use F1 and so on. Please take a look at the picture below.
Note: Register B can also be used for other general purpose operations.
Port Registers
As you may already know, there are 4 ports for 8051. If you are unfamiliar of the architecture of 8051 please read
the following article:- The architecture of 8051
So 4 Input/Output ports named P0, P1, P2 and P3 has got four corresponding port registers with same name P0,
P1, P2 and P3. Data must be written into port registers first to send it out to any other external device through
ports. Similarly any data received through ports must be read from port registers for performing any operation. All
9
4 port registers are bit as well as byte addressable. Take a look at the figure below for a better understanding of
port registers.
Stack Pointer
Known popularly with an acronym SP, stack pointer represents a pointer to the the system stack. Stack pointer is
an 8 bit register, the direct address of SP is 81H and it is only byte addressable, which means you cant access
individual bits of stack pointer. The content of the stack pointer points to the last stored location of system stack.
To store something new in system stack, the SP must be incremented by 1 first and then execute the “store”
command. Usually after a system reset SP is initialized as 07H and data can be stored to stack from 08H onwards.
This is usually a default case and programmer can alter values of SP to suit his needs.
10
From the figure below you can observe that there are 2 modes for this register :- Idle mode and Power down
mode.
Setting bit 0 will move the micro controller to Idle mode and Setting bit 1 will move the micro controller to
Power down mode.
11
3 RS0 D3 Register LSB of the register bank select bit. Look for
Bank explanation below this table.
select bit 0
4 RS1 D4 Register MSB of the register bank select bits.
Bank
select bit 1
5 F0 D5 Flag 0 User defined flag
6 AC D6 Auxiliary This bit is set if data is coming out from bit 3
carry to bit 4 of Acc during an Arithmetic
operation.
7 CY D7 Carry Is set if data is coming out of bit 7 of Acc
during an Arithmetic operation.
At a time registers can take value from R0,R1…to R7. You may already know there are 32 such registers. So how
you access 32 registers with just 8 variables to address registers? Here comes the use of register banks. There are
4 register banks named 0,1,2 and 3. Each bank has 8 registers named from R0 to R7. At a time only one register
bank can be selected. Selection of register bank is made possible through PSW register bits PSW.3 and PSW.4,
named as RS0 and RS1.These two bits are known as register bank select bits as they are used to select register
banks. The picture will talk more about selecting register banks.
So far we have discussed about all major SFR’s in 8051. There are many others. Please remember there are 21
SFR’s and we have discussed only 9 specifically. The table below lists all other 12 SFR’s.
12
SFR Physical Function
Special Address
DPH 83 Data pointer registers (High). Only byte addressing
possible.
DPL 82 Data pointer register (Low). Only byte addressing
possible.
IP B8 Interrupt priority. Both bit addressing and byte
addressing possible.
IE A8 Interrupt enable. Both bit addressing and byte addressing
possible.
TCON 88 Timer control. Both bit addressing and byte addressing possible.
13
TMOD 89 Timer mode select. Only byte addressing is possible.
14
15
16
17
18
19