Week 3 – 8051 Microcontroller
Assembly Language Programming
7
Week 3 – Assembly Language
Introduction
Instruction Set
Software - Keil
Assembler Directives
9
Programming Languages
Assembly is a Low Level Language
FORTRAN
High Level Pascal
COBOL
BASIC
C++
Middle Level C
Low Level Assembly
10
Assembly Language
Low Level Language
English-like abbreviations
Represent basic operations of computer
Translated to machine language
Assemblers convert to machine language
High speed conversion
Easier for human interpretation as compared to Machine
language
Still tedious and difficult
Many instructions for simple tasks
These problems led to High Level languages
11
General Format of Assembly Program
12
Assembly Language
General Format of Assembly Program
mnemonics operand , [operands]
destination, Source
[label]: mnemonics operand , [operands]
destination, Source
Brackets shows that the component is optional
13
Assembly Language
General Format of Assembly Program
[label]: mnemonics operand , [operands]
destination, Source
Labels: are used to represent address of a line with a
name
Labels must end with a colon
Operand: on which the operation is performed
14
Assembly Language
General Format of Assembly Program
[label]: mnemonics operand , [operands]
destination, Source
Mnemonics: Instruction or command
Example:
mov A,#67h
mov is mnemonic
A and #67h are operands
15
Assembly Language
Comments
Comment begins with a semicolon (;)
Assembler ignores the comments
Example:
; This is a comment
16
Instruction Set
8051 has 111 instructions in the instruction set.
Instructions have 8-bit Opcode
Combinations = 28 = 256
255 are implemented
139 1 Byte instructions
92 2 Byte instructions
(139+92+24=255 instructions)
24 3 Byte instructions
1 Machine cycle = 12 clock cycles
Could take 1, 2 or 4 Machine Cycles to execute an
instruction
17
Instruction Set Summary
18
Instruction Set
19
Instruction Definition
mov a, #data
Bytes 2
Cycles 1
Encoding 0111 0100 dddd dddd
Operation (A) #data
Example
mov a, #3
7403
0111 0100 0000 0011
20
Assembly Programming for 8051
34
A51 Assembler
Assembler: Converts Assembly code to Why a Two-Pass Assembler?
The main reason for a two-pass assembly is
machine language code to handle forward references in assembly
code. A forward reference occurs when a
label is used before it is defined.
A51 – Two Pass Assembler In a single-pass assembler, it would not
know the address of the label the first time
it encounters it. By using two passes, the
First Pass assembler can gather all necessary
information in the first pass and resolve it in
Symbols and Labels are collected the second pass.
Length of each instruction is determined
Symbol Table is made
MOV A, #3 ; Move immediate value 3
Second Pass to the accumulator
In the second pass, the assembler uses JMP Start ; Jump to the label 'Start’
the information gathered in the first
pass to generate the actual machine Start: ; Label definition
ADD A, #5 ; Add 5 to the accumulator
code.
35
Assembler Directives
Change state of assembler
Define Symbols, add information to the object file etc
Must not be confused with Instructions. They do not
produce executable code
For exp: ORG, END, USING, EQU, SET etc.
Assembler directives (also known as pseudo-instructions) are special
instructions used in assembly language programming that do not
generate any machine code but help the assembler organize the
program, allocate memory, define constants, and control the assembly
process.
36
Assembler Directives
1. Address Control
2. Symbol Definition
3. Memory Initialization
4. Others
37
Assembler Directives
1. Address Control
ORG, USING
2. Symbol Definition
Generic Symbols: EQU, SET
Address Symbols: BIT, DATA, XDATA
SFR Symbols: sfr, sbit
3. Memory Initialization
DBIT, DB, DW
4. Others
END
38
Address Control
Allows the control of Address Location counter
39
Address Control
ORG
Sets the starting address for the subsequent code or data.
Sets new origin for subsequent statements
Format:
ORG expression
Where,
expression must be an absolute address without any forward references
Only absolute addresses and symbols in current segment can be used
When ORG statement is encountered, the assembler calculates
value of the expression and changes location counter of current
segment
Examples
ORG 100H
ORG RESTART
40
Address Control
USING
4 Register Banks
USING specifies which Register Bank to use.
Format:
USING expression
where,
expression is the register bank number, and it must be a
value from 0 and 3
41
Symbol Definition
Symbols and labels can be composed of 31 characters
from the following list:
A -Z, a - z, 0 - 9, _ and ?
A symbol can start with any of these except the digits 0 –
9
A51 is not case sensitive
42
Symbol Definition
EQU and SET
Used to create symbols that represent registers, numbers
and addresses
Similar to Define in C
Assign a numeric value / register symbol to the specific
symbol name
Difference between EQU and SET
Symbols defined with EQU can not be redefined as it
creates fixed constants.
The SET directive allows later redefinition of symbols
43
Symbol Definition
EQU and SET name EQU value
Formats of SET / EQU statements are: PI EQU 3.14
symbol EQU expression
symbol EQU register
symbol SET expression name SET value
symbol SET register COUNT SET 10
44
Symbol Definition
EQU and SET
symbol is the name of symbol to define
expression specified will be substituted for each
occurrence of the symbol used in program. expression is
numeric expression which has no forward references
register is one of the 8 registers (in the active bank)
45
Symbol Definition
Example
LIMIT EQU 1200
VALUE EQU LIMIT-200
SERIAL EQU SBUF
VALUE SET 1000
COUNTER SET R1
TEMP SET COUNTER
TEMP SET COUNTER*VALUE
46
Symbol Definition
EQU and SET
Symbols defined with EQU and SET directive may be used
anywhere in operands, expressions, or addresses etc.
Symbols that are defined as register name can be used
anywhere a register is allowed
Assembler replaces each occurrence of defined symbol in
assembly program with the specified numerical value or
register name
47
Symbol Definition - Address Symbols
The BIT, DATA, and XDATA directives assign an address
value to the specified symbol
Symbols defined with the BIT, DATA and XDATA directives
can not be changed or redefined
48
Symbol Definition - Address Symbols
Format: BIT, DATA, XDATA
symbol BIT bit_address ; defines a BIT symbol
symbol DATA data_address ; defines a DATA symbol
symbol XDATA xdata_address ; defines XDATA symbol
49
Symbol Definition - Address Symbols
Format: BIT, DATA, XDATA
Symbol: name of symbol to be defined
Bit_address: is the address of a bit in internal data memory in
the area 20h to 2Fh
Data_address: data memory address in the range 0 to 127 or a
special function register (SFR) address in the range 128 .. 255.
xdata_address is an external memory address in range 0 to
65535
50
Symbol Definition – SFR Symbols
sfr, sbit:
sfr (Special Function Register) and sbit (Special Function Bit)
are used to define and manipulate control registers and their
individual bits in microcontrollers.
sfr
sfr symbol = address
sbit
sbit symbol = bit-address
sfr_symbol is the name of symbol to be defined.
address is an SFR address with which the symbol is to be
associated
bit-address is address of an SFR Bit
51
Symbol Definition – SFR Symbols
sfr, sbit
Examples
sfr mine = P0 ; SFR symbol
sbit motor = P3^0 ; SFR Bit symbol
52
Memory Initialization
Memory initialization directives are used to initialize code
or constant space in byte units
DBIT
Used to define a bit
DB
Used to define byte – 8 bit
DW
Used to define Word – 16 bits
53
Memory Initialization - DB
DB
The DB directive initializes code memory with byte (8-bit)
values
Format
label: DB expression, expression
where
label is the symbol that is given the address of the initialized
memory
expression is a byte value. Each expression may be a value or a
symbol etc
Example
ORG 100h
REQ: DB 1,2,3,4,’A’
54
Assembler Directives - Others
END
Signals the end of assembly module
Any text that appears after END is ignored
END directive is required in every assembly module
If missing, a warning message is generated by assembler
55
Number Representation
D (or nothing) Decimal
H for Hex
Q for Octal
B for Binary
56
M icrocontrollers and E mbedded Systems: Course Project Overview Welcome to a course where
your project could be the first step toward becoming a tech entrepreneur! Many successful companies
began with simple ideas, driven by passion and resourcefulness. Now it’s your turn to embrace that
spirit. For your project, you'll design and develop a microcontroller-based system. Think of it as building
something you’d want to sell in the next few years. Identify a clear goal for your product and focus on
delivering a real solution.
The theme is flexible—you can choose what excites you, like creating an innovative children's toy.
Replicate a toy’s core functionality and enhance it with features that add value or excitement. Get
creative and use materials you already have at home.
Your project must:
- The project shall be done in groups (4 students/group)
- Submit a project proposal and get an approval about the content of the project by 21-Oct-24.
- Implement features like timers, interrupts, or communication protocols to ensure complexity.
- Generate at least one PWM signal to control an actuator.
- Assessment will be done on the basis of:
- Oral exam (viva)
- Functional Prototype Demo
- A short Youtube video describing the project
This will challenge you to explore microcontroller workings, like the 8051 architecture (but feel free to
use any controller). You’ll build a functional system and push your creative limits. This project is a step
toward
57 your future as a builder, innovator, and entrepreneur.