Week 3 Assembly Language
Week 3 Assembly Language
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
10
Assembly Language
Low Level Language
English-like abbreviations
Represent basic operations of computer
11
General Format of Assembly Program
12
Assembly Language
General Format of Assembly Program
13
Assembly Language
General Format of Assembly Program
14
Assembly Language
General Format of Assembly Program
15
Assembly Language
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
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.
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
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
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 ?
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
44
Symbol Definition
EQU and SET
symbol is the name of symbol to define
45
Symbol Definition
Example
46
Symbol Definition
EQU and SET
Symbols defined with EQU and SET directive may be used
anywhere in operands, expressions, or addresses etc.
47
Symbol Definition - Address Symbols
48
Symbol Definition - Address Symbols
Format: BIT, DATA, XDATA
49
Symbol Definition - Address Symbols
Format: BIT, DATA, XDATA
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
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
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.
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.