8051 Programming
1
Assembly Language Programming
2
Syntax:
[label:] Mnemonic [operands] [;comments]
Label: Allows the program to refer to a
line of code by name.
The Mnemonic, operand fields perform the
real work of the program
The comment field begins with ‘;’. Just for
understanding or to remember what
programmer wrote.
Assembling & Running an 8051
Program
3
Editor Program
MS-DOS Editor in windows used to create
source files with extension .asm or .src etc.
Assembler
Converts the instruction to machine code.
Produces .obj, .lst files
Linker
It takes one or more object files and
produces an absolute object file with
extension .abs
Object to Hex Converter
It takes .abs file and creates .hex file that is
ready to burn into ROM of 8051
8051 Data types
4
It has only one data type it is 8-bit.
Define Byte (DB)
Used to define 8-bit data
Numbers can be in decimal, hex or ASCII formats
Ex:
data1: DB 28 ;decimal
data2: DB 00110101 B ;Binary
data3: DB 39H ;hexadecimal
data4: DB “2579” ;ASCII number
data5: DB “my name is joe” ;ASCII
characters
8051 Assembler Directives
5
ORG (Origin)
Indicate the beginning of the address
Ex: ORG 510H
ORG $+100H
EQU ( Equate)
Used to define a constant without occupying memory location
Ex: Count EQU 25
When that label appears in the program, its constant value is
substituted for the label.
• i.e; MOV R3, #Count ;R3 = 25
END (End)
Indicates to the assemble that this is the end of the source file.
Any thing after the end directive will be ignored by the
assembler
8051 Programming in ‘C’
6
Advantages
It is easier and less time consuming
‘C’ is easier to modify and update
You can use code available in function libraries
‘C’ code is portable to other microcontrollers with
little modification or no modification
Disadvantages:
It produces hex file size is much larger than the hex
file that is produced if we program in assembly
‘C’ Data types
7
Unsigned Char
8-bit data type that can takes a value in the range of
0-255 (00-FFH)
Signed Char
Also 8-bit data type, gives the values in the range of -
128 to +127.
Default is “Signed”
Ex: unsigned char z;
for (z = 0; z<=255; z++)
P1=z;
‘C’ Data types (Contd..)
8
Unsigned int
16 – bit data type
Can takes a value in the range of 0 to 65535 (0000- FFFFH)
This data byte takes 2 bytes of RAM
Signed int
16 – bit data type
Can takes a value in the range of -32768 to +32767
Sbit
Used to define a single bit
It allows access to the bits of SFR’s which are bit addressable.
Ex: sbit mybit = P0^0;
Bit
This data type allows access to single bit of bit addressable RAM 20
– 2FH.
Sfr
Used to access the byte size SFRs
‘C’ Data types (Contd..)
9
Data Type Size in bits Data Range/Usage
Unsigned char 8 – bit 0 to 255
(signed) char 8 – bit -128 to +127
Unsigned int 16 – bit 0 to 65535
(signed) int 16 – bit -32768 to +32767
Sbit 1 – bit SFR bit addressable
only
Bit 1 – bit RAM bit
addressable only
Sfr 8 – bit SFRs