0% found this document useful (0 votes)
83 views

Lecture MP 3 PDF

The document discusses assembly language programming for the 8051 microcontroller. It covers topics like instruction set, assembler directives, using Keil to write assembly code, and debugging assembly code. Week 3 focuses on assembly language, including the instruction set, software tools like Keil, and assembler directives.

Uploaded by

Muaz Shahid
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
83 views

Lecture MP 3 PDF

The document discusses assembly language programming for the 8051 microcontroller. It covers topics like instruction set, assembler directives, using Keil to write assembly code, and debugging assembly code. Week 3 focuses on assembly language, including the instruction set, software tools like Keil, and assembler directives.

Uploaded by

Muaz Shahid
Copyright
© © All Rights Reserved
You are on page 1/ 52

Microprocessor and Microcontroller

Week 3 – 8051 Microcontroller

Assembly Language Programming

2
Course Outline
Week Topic
1 Introduction
8051Microcontroller
2
 Architecture
3  Assembly Language
4  Assembly Language Contd.
 Timers and Counters
5, 6  Serial Port
 Interrupt
7  Design and Interface Examples
8086 / 8088 Microprocessor
8
 Introduction and Architecture
9
10  Hardware and Design
10 Midterm Exam
11  Programming in Assembly
PIC 18 F Microcontroller
12
 Introduction, Architecture, I/O Pins
13  Programming
14  Timers
15, 16  Peripherals of PIC 18F Microcontrollers
17 Revision

3
Week 3 – Assembly Language

 Introduction
 Instruction Set
 Software - Keil
 Assembler Directives

4
Programming Languages
Assembly is a Low Level Language
FORTRAN
 High Level Pascal
COBOL
BASIC

C++
 Middle Level C

 Low Level Assembly

5
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

6
General Format of Assembly Program

7
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

8
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

9
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

10
Assembly Language
Comments

 Comment begins with a semicolon (;)


 Assembler ignores the comments

Example:
; This is a comment

11
Instruction Set
 8051 Instructions have 8 bit Opcode
 Combinations = 28 = 256
 255 are implemented
 139 1 Byte instructions
 92 2 Byte instructions
 24 3 Byte instructions

 1 Machine cycle = 12 clock cycles


 Could take 1, 2 or 4 Machine Cycles to execute an
instruction

12
Instruction Set Summary

13
Instruction Set

14
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

15
Keil µVision

Keil for Assembly Programming of 8051

16
Using Keil for Assembly Programming

17
New Project

18
Select Device

19
New File (for code)

20
Save File as filename.a51

21
Add Code to Project

22
Debug Mode

 Write Code
 Save (Ctrl + S)
 Build Target File (F7)
 Debug Mode
 Ctrl + F5

23
Debug Mode

24
Debug Mode

Registers

Accumulator

PSW

25
Disassembly (only in debug mode)

26
Disassembly (only in debug mode)

27
Disassembly (only in debug mode)

28
Assembly Programming for 8051

29
A51 Assembler
Assembler: Converts Assembly code to machine language
code

A51 – Two Pass Assembler


 First Pass
 Symbols and Labels are collected
 Length of each instruction is determined
 Symbol Table is made

 Second Pass
 Forward references are resolved by using symbol table

30
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

31
Assembler Directives

1. Address Control
2. Symbol Definition
3. Memory Initialization
4. Others

32
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

33
Address Control

 Allows the control of Address Location counter

34
Address Control
ORG
 Changes location counter of currently active segment
 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 assembles calculates
value of the expression and changes location counter of current
segment
 Examples
 ORG 100H
 ORG RESTART
35
Address Control
USING
 4 Register Banks
 USING specifies which Register Bank is active
 Format
USING expression

where
 expression is the register bank number and it must be a
value from 0 and 3

36
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

37
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 may not haven been
previously defined by EQU and can not be redefined
 The SET directive allows later redefinition of symbols

38
Symbol Definition
EQU and SET
 Formats of SET / EQU statements are:

symbol EQU expression


symbol EQU register

symbol SET expression


symbol SET register

39
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)

40
Symbol Definition
Examples

 LIMIT EQU 1200


 VALUE EQU LIMIT-200
 SERIAL EQU SBUF

 VALUE SET 100


 COUNTER SET R1
 TEMP SET COUNTER
 TEMP SET COUNTER*VALUE

41
Symbol Definition
EQU and SET
 Symbols defined with EQU and SET directive may 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

42
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

43
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

44
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

 xdata_address is an xdata memory address in range 0 to


65535

45
Symbol Definition – SFR Symbols
sfr, sbit
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

46
Symbol Definition – SFR Symbols
sfr, sbit
Examples

 sfr mine = P0 ; SFR symbol

 sbit motor = P3^0 ; SFR Bit symbol

47
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

48
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’
49
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

50
Number Representation
 D (or nothing) Decimal
 H for Hex
 Q for Octal
 B for Binary

51
Acknowledgement

Material used with permission of

 Dr. Javaid Iqbal


 Head of Department, Mechatronics Engineering
 College of EME, NUST

I am extremely thankful to Dr. Javaid who has been a great


teacher and still helps and supports me

52

You might also like