8051 Assembly Language Programming
8051 Assembly Language Programming
language
Programming
DEVELOPMENT ENVIRONMENT
Host
The PC used to develop the program, it usually has
Editor (edit source file)
Compiler (convert high level language to machine code *.ob
Assembler (convert assembly language to machine code *.ob
Linker (link several obj files into an absolute obj file)
Obj to Hex converter (convert obj file to Hex file)
Loader (load the hex file to target)
Target
The development hardware with embedded microcontroller.
It can be connected to Host through various interfaces
E.g. RS232, USB, JTAG, IEEE1394,
Host Target board
CONTENTS
PROGRAMMING STEPS
INSRUCTION SET
ADDRESSING MODES
ASSEMBLY PROGRAM EXAMPLES
Steps to Create a
Program
Editor Program
Allows user to type assembly language program with extension .asm
or .a51
Assembler Program
Converts assembly language code into machine language i.e opcodes
are generated for written instructions, that can be understood by
MCU. It will generate two files as follows
1. List file (.lst extension)
Contains list of all addresses & instructions .
2.Object File (.obj extension)
It is binary file contains binary code.
Linker Program
Combine one or more object file into absolute object file no extension.
Object to Hex converter
Converter absolute object file into file with extension Hex which can
be burn into ROM of 8051
Burn the hex file into ROM
Structure of Assembly
Language
ORG 0H
;start (origin) at location 0
MOV R5,#25H
;load 25H into R5
MOV R7,#34H
;load 34H into R7
MOV A,#0
;load 0 into A
ADD A,R5
;add contents of R5 to A
;now A = A + R5
ADD A,R7
;add contents of R7 to A
;now A = A + R7
ADD A,#12H
;add to A value 12H
;now A = A + 12H
HERE: SJMP HERE
;stay in this loop
END
;end of asm source file
ASSEMBLY: STRUCTURE
Structures of assembly language
1. A series of lines of assembly language instructions and/or
directives
ASSEMBLY: DIRECTIVES
Directives
A pseudo-code that cannot be translated into machine
code
Used to notify assembler of certain operations
E.g. END: notify assembler the end of the source
file.
Commonly used directives
ORG: origin
Indicate the beginning of the address
The number after ORG can be either in hex or
decimal
DB: define byte
Define an 8-bit data
Directive examples
ORG 500H
DATA1: DB
28
Hex)
DATA2: DB
00110101B
DATA3: DB
39H
ORG 510H
DATA4: DB
2591
;DECIMAL (1C in
;BINARY (35 in Hex)
;HEX
; ASCII NUMBERS
ORG 518H
DATA6: DB
My name is Joe ;ASCII
CHARACTERS
EQU: equate
Define a constant without occupying a memory location
It DOES NOT use any memory space!
The constant value can be used later in the program
To improve the readability of the program
Give a name to a constant
To improve code efficiency
If the same constants are used twice in the program,
EQU directive, we only need to change it in one location
value is changed
We should avoid using constant directly, and use the
directive as often as possible.
Example (Demo directives)
COUNT
EQU 25H
MOV R3, #COUNT
MOV A, #COUNT
EQU
Used to create symbols that can be used to represent
registers, numbers, and addresses
LIMIT
EQU 2000
SERIAL
EQU SBUF
SCK
EQU P1.3
MY_VAL
EQU 0x44
DATA
Used to define a name for memory locations
SP
DATA 0x81 ;special function registers
MY_VAL DATA 0x44 ;RAM location
cseg stands for code segment
Instruction Set
8051 instructions have 8-bit opcode
There are 256 possible instructions of
which 255 are implemented
Some instructions have one or two
additional bytes for data or address
There are
139
1-byte
instructions,
92
2-byte instructions, and
24
3-byte
instruction
Instruction Formats
Instruction :- It is specific task that
CPU can perform,it consists of
opcode & zero or more operands.
Opcode :- machine code for specific
operation to be perform to
microcontroller.
e.g ADD,SUB,ANL
Operands :- data or address used by
the instruction.
Types of operands
There are three types of operands or three
places from where data is available
3. memory
Reference to a location in memory
Memory address is encoded within the instruction,
or
Register holds the address of a memory location
Addressing Modes
Immediate Addressing
Register Addressing
Use register to hold the data to be
manipulated
Example
MOV A, R0
MOV R2, A
MOV R7, DPL
Notes
MOV R2, R5 is invalid.
MOV A, DPTR is invalid (why?)
Direct Addressing
Direct addressing can access any
on-chip memory location
Example: ADD A,55H
Example: MOV P1, A
Transfers the content of
accumulator to Port 1
(address 90H)
Relative Addressing
Relative addressing is used with certain jump instructions
Relative address (offset) is an 8-bit signed value (-128 to 127)
which is added to the program counter to form the address of
next instruction
Prior to addition, the program counter is incremented to the
address following the jump (the new address is relative to the
next instruction, not the address of the jump instruction)
This detail is of no concern to the user since the jump
destinations are usually specified as labels and the assembler
determines the relative offset
Advantage of relative addressing: position independent codes
Example here: sjmp here
Absolute Addressing
Long Addressing
Indexed Addressing
Addressing Modes
Types of instructions
Register related
MOV source,destionation
MOV a, byte
MOV byte, a
;with
Exchange instruction
XCH a, byte
;exchange
accumulator and
;byte
XCHD a, byte;exchange low nibbles of
;accumulator and
Stack related
byte
PUSH byte ;increment
;move byte
POP byte ;move from
;decrement
stack pointer,
on stack
stack to byte,
stack pointer
Data processing
Arithmetic
Logical
Boolean
Arithmetic Instructions
Example:
ADD A,7FH
ADD A,@R0
ADD A,R7
ADD A,#35H
All arithmetic instructions are
executed in one machine cycle except
INC DPTR (two cycles) and MUL AB
andDIV AB (four cycles)
Logical Instructions
8051 logical instructions perform Boolean
operations on
bytes of data on a bit-by-bit basis .
Example: lets assume A=00110101B.
Instruction ANL A,#01010011B will leave
00010001 in accumulator
Examples logical instructions:
ANL A,55H
ANL A,@R0
ANL A,R6
ANL A,#33H
RL A
SWAP A
RR A
Boolean Instructions
8051 contains a complete Boolean
processor for single-bit operations.
All bit accesses use direct addressing
Bits may be set or cleared in a single
instruction
Example:
SETB P1.7
CLR P1.7
CLR C
Branching Instructions
RET pops the last two bytes off the stack and places them in the
PC
Conditional Jump
Delay Calculation
Delay Calculation