Roadmap to Membership of RICS - Pathways and Routes
8051assembly language
1.
2. 8051 Assembly Language8051 Assembly Language
ProgrammingProgramming
Hisham Mat Hussin
Microprocessor Fundementals
UniKL BMI
3. 8051 Assembly Language
Programming
OutlinesOutlines
8051 programming model
Assembly language syntax
Operation codes and operands
How 8051 interprets binary data
Machine instructions
Example of Assembly language program
8051 Instruction Set
4. Programming Model
A programmer should;
Know the internal structure of the 8051
Understand the “Programming model”
i.e. how programmers “see” the 8051
Know how to access the registers and
accumulators with programs
5. Binary Data
Unlike human, computers do not know verbal
instructions; they only know 0s and 1s.
Binary data: program in a stream of 0s and 1s
It is also called “Machine Language”
For convenient purpose, machine language
programs are usually expressed in hexadecimal
(i.e. base-16) format
11. Assembly Language
Syntax Syntax = format/rule
[label:] mnemonic [operands] [;comment]
Items in square brackets are optional
Example:
Thus, Machine Code = Operation Code (Opcode) + Operand
12. Mnemonic/Operation Code
(Opcode)
Program = a set of instructions
All computers work according to the
program
All instructions contain a “verb” , which
tells the computer what to do
This “verb” is called mnemonic/operation
code e.g. MOV R0, #12h –– “MOV” is the
opcode
13. Operand
Apart from Opcode, an instruction also includes
object to act on.
The object is called an operand.
Operand is optional. Instructions can have one,
two or no operands.
e.g.
MOV R0, #12h --- R0 and #12h are two operands
ADD R1 --- R1 is the only one operand
NOP --- no operand follows
14. Pseudo-
Instructions/DirectivesAssembler state controlAssembler state control
ORG (origin)
indicates the beginning of the instructions. The number
that either hex or decimal.
END
Indicates the end of assembly instructions.
EQU (equate)
used to define a constant without occupying a memory
location. It does not set aside storage for a data item but
associates a constant value with a data label so that
when the label appears in the program. Its constant
value will be substituted for the label.
Eg. COUNT EQU 25H
15. Define Byte (DB) and Data TypesDefine Byte (DB) and Data Types
Used to define 8-bit data and store them in
assigned memory locations. Define data can be in
decimal, binary, hex, or ASCII formats.
ORG 500HORG 500H
DATA1: DB 28DATA1: DB 28 ;DECIMAL (1C in Hex);DECIMAL (1C in Hex)
DATA2: DB 00110101BDATA2: DB 00110101B ;BINARY (35 in Hex);BINARY (35 in Hex)
DATA3: DB 39H ;DATA3: DB 39H ;HEXHEX
ORG 510HORG 510H
DATA4: DB “2591”DATA4: DB “2591” ; ASCII NUMBERS; ASCII NUMBERS
ORG 518HORG 518H
DATA6: DB “My name is Deen”DATA6: DB “My name is Deen” ;ASCII CHARACTERS;ASCII CHARACTERS
17. Programming is both aProgramming is both a
science and art !!science and art !!
Science – rules of grammar,Science – rules of grammar,
punctuation, spelling &punctuation, spelling &
structure.structure.
Art – how the words areArt – how the words are
arrangedarranged
18. How to tell a lump of sand what to do:How to tell a lump of sand what to do:
1. study common programming techniques
2. analyze example programs
3. write many practice programs
[ Read chapter 4 from Ayala for revision on basic assembly language concepts.Read chapter 4 from Ayala for revision on basic assembly language concepts.]
21. Assembler/LinkerAssembler/Linker
AssemblerAssembler
Change mnemonic code into machine codeChange mnemonic code into machine code
Label can be used to represent symbolicLabel can be used to represent symbolic
address/dataaddress/data
Directives : like pre-processing operator (#) in CDirectives : like pre-processing operator (#) in C
language.language.
Linkage EditorLinkage Editor
Link objective code into executable fileLink objective code into executable file
(*.obj(*.obj →→ *.exe)*.exe)
22. List File For Test Program (Assembly)List File For Test Program (Assembly)
Opcodes Assembly language
Address/
Program Pointer
23. MOV InstructionMOV Instruction
MOV destination, source ; copy source toMOV destination, source ; copy source to
dest.dest.
MOV A,#55HMOV A,#55H ;load value 55H into reg. A;load value 55H into reg. A
MOV R0,AMOV R0,A ;copy contents of A into R0;copy contents of A into R0
;(now A=R0=55H);(now A=R0=55H)
MOV R1,AMOV R1,A ;copy contents of A into R1;copy contents of A into R1
;(now A=R0=R1=55H);(now A=R0=R1=55H)
MOV R2,AMOV R2,A ;copy contents of A into R2;copy contents of A into R2
;(now A=R0=R1=R2=55H);(now A=R0=R1=R2=55H)
MOV R3,#95HMOV R3,#95H ;load value 95H into R3;load value 95H into R3
;(now R3=95H);(now R3=95H)
MOV A,R3MOV A,R3 ;copy contents of R3 into A;copy contents of R3 into A
;now A=R3=95H;now A=R3=95H
24. Notes on ProgrammingNotes on Programming
Value (proceeded with #) can be loadedValue (proceeded with #) can be loaded
directly to registers A, B, or R0 – R7directly to registers A, B, or R0 – R7
MOV R5, #0F9HMOV R5, #0F9H
If values 0 to F moved into an 8-bitIf values 0 to F moved into an 8-bit
register, the rest assumed all zerosregister, the rest assumed all zeros
MOV A, #5MOV A, #5
A too large value causes an errorA too large value causes an error
MOV A, #7F2HMOV A, #7F2H
25. Structure of AssemblyStructure of Assembly
LanguageLanguageORG 0HORG 0H ;start (origin) at location 0;start (origin) at location 0
MOV R5,#25HMOV R5,#25H ;load 25H into R5;load 25H into R5
MOV R7,#34HMOV R7,#34H ;load 34H into R7;load 34H into R7
MOV A,#0MOV A,#0 ;load 0 into A;load 0 into A
ADD A,R5ADD A,R5 ;add contents of R5 to A;add contents of R5 to A
;now A = A + R5;now A = A + R5
ADD A,R7ADD A,R7 ;add contents of R7 to A;add contents of R7 to A
;now A = A + R7;now A = A + R7
ADD A,#12HADD A,#12H ;add to A value 12H;add to A value 12H
;now A = A + 12H;now A = A + 12H
HERE: SJMP HEREHERE: SJMP HERE ;stay in this loop;stay in this loop
ENDEND ;end of asm source file;end of asm source file
Program 2-1:Sample of an Assembly Language Program
29. Mnemonic Operand(s) Description
ACALL addr11 Absolute subroutine call
ADD A,Rn Add register to Accumulator
ADD A,direct Add direct byte to Accumulator
ADD A,@Ri Add indirect RAM to Accumulator
ADD A,#data Add immediate data to Accumulator
ADDC A,Rn Add register to Accumulator with carry
ADDC A,direct Add direct byte to Accumulator with carry
ADDC A,@Ri Add indirect RAM to Accumulator with carry
ADDC A,#data Add immediate data to Accumulator with carry
AJMP addr11 Absolute jump
ANL A,Rn AND Register to Accumulator
ANL A,direct AND direct byte to Accumulator
ANL A,@Ri AND indirect RAM to Accumulator
ANL A,#data AND immediate data to Accumulator
ANL direct,A AND Accumulator to direct byte
ANL direct,#data AND immediate data to direct byte
ANL C,bit AND direct bit to carry
ANL C,/bit AND complement of direct bit to carry
CJNE A,direct,rel Compare direct byte to Acc and jump if not equal
CJNE A,#data,rel Compare immediate to Acc and jump if not equal
CJNE RN,#data,rel Compare immediate to register and jump if not equal
CJNE @Ri,#data,rel Compare immediate to indirect and jump if not equal
CLR A Clear Accumulator
CLR C Clear carry
CLR bit Clear direct bit
CPL A Complement Accumulator
CPL C Complement carry
CPL bit Complement direct bit
DA A Decimal Adjust Accumulator
DEC A Decrement Accumulator
DEC Rn Decrement Register
DEC direct Decrement direct byte
DEC @Ri Decrement indirect RAM
DIV AB Divide A by B
DJNZ Rn,rel Decrement register and jump if not zero
DJNZ direct,rel Decrement direct byte and jump if not zero
INC A Increment Accumulator
INC Rn Increment register
INC direct Increment direct byte
INC @Ri Increment indirect RAM
INC DPTR Increment Data Pointer
JB rel Jump if direct bit is set
JBC bit,rel Jump if direct bit is set and clear bit
JC rel Jump if carry is set
JMP @A+DPTR Jump indirect relative to the DPTR
JNB rel Jump if direct bit is not set
JNC rel Jump if carry not set
JNZ rel Jump if Accumulator is not zero
JZ rel Jump if Accumulator is zero
LCALL addr16 Long subroutine call
LJMP addr16 Long jump
MOV A,Rn Move register to Accumulator
MOV A,direct Move direct byte to Accumulator
MOV A,@Ri Move indirect RAM to Accumulator
MOV A,#data Move immediate data to Accumulator
MOV Rn,A Move Accumulator to register
MOV Rn,direct Move direct byte to register
MOV RN,#data Move immediate data to register
MOV direct,A Move Accumulator to direct byte
MOV direct,Rn Move register to direct byte
MOV direct,direct Move direct byte to direct
MOV direct,@Ri Move indirect RAM to direct byte
MOV direct,#data Move immediate data to direct byte
MOV @Ri,A Move Accumulator to indirect RAM
MOV @Ri,direct Move direct byte to indirect RAM
MOV @Ri,#data Move immediate data to indirect RAM
MOV DPTR,#data16 Load Data Pointer with a 16-bit constant
MOV C,bit Move direct bit to carry
MOV bit,C Move carry to direct bit
MOVC A,@A+DPTR Move Code byte relative to DPTR to Accumulator
MOVC A,@A+PC Move Code byte relative to PC to Accumulator
MOVX A,@Ri Move external RAM (8-bit addr) to Accumulator
MOVX A,@DPTR Move external RAM (16-bit addr) to Accumulator
MOVX A,@Ri,A Move Accumulator to external RAM (8-bit addr)
MOVX @DPTR,A Move Accumulator to external RAM (16-bit addr)
MUL AB Multiply A and B
NOP none No operation
ORL A,Rn OR register to Accumulator
ORL A,direct OR direct byte to Accumulator
ORL A,@Ri OR indirect RAM to Accumulator
ORL A,#data OR immediate data to Accumulator
ORL direct,A OR Accumulator to direct byte
ORL direct,#data OR immediate data to direct byte
ORL C,bit OR direct bit to carry
31. Move Data concepts:Move Data concepts:
• data is stored at a source address
moved to (actually, data is copied)
a destination address.
= Addressing modesAddressing modes.
43. ArithmeticArithmetic
ADD A,Rn Add register to Accumulator
ADD A,direct Add direct byte to Accumulator
ADD A,@Ri Add indirect RAM to Accumulator
ADD A,#data Add immediate data to Accumulator
ADDC A,Rn Add register to Accumulator with carry
ADDC A,direct Add direct byte to Accumulator with carry
ADDC A,@Ri Add indirect RAM to Accumulator with carry
ADDC A,#data Add immediate data to Accumulator with carry
DIV AB Divide A by B
MUL AB Multiply A and B
SUBB A,Rn Subtract Register from Accumulator with borrow
SUBB A,direct Subtract direct byte from Accumulator with borrow
SUBB A,@Ri Subtract indirect RAM from Accumulator with borrow
SUBB A,#data Subtract immediate data from Acc with borrow
44. LogicLogic
ANL A,Rn AND Register to Accumulator
ANL A,direct AND direct byte to Accumulator
ANL A,@Ri AND indirect RAM to Accumulator
ANL A,#data AND immediate data to Accumulator
ANL direct,A AND Accumulator to direct byte
ANL direct,#data AND immediate data to direct byte
ANL C,bit AND direct bit to carry
ANL C,/bit AND complement of direct bit to carry
ORL A,Rn OR register to Accumulator
ORL A,direct OR direct byte to Accumulator
ORL A,@Ri OR indirect RAM to Accumulator
ORL A,#data OR immediate data to Accumulator
ORL direct,A OR Accumulator to direct byte
ORL direct,#data OR immediate data to direct byte
ORL C,bit OR direct bit to carry
ORL C,/bit OR complement of direct bit to carry
45. LogicLogic
XRL A,Rn Exclusive-OR register to Accumulator
XRL A,direct Exclusive-OR direct byte to Accumulator
XRL A,@Ri Exclusive-OR indirect RAM to Accumulator
XRL A,#data Exclusive-OR immediate data to Accumulator
XRL direct,A Exclusive-OR Accumulator to direct byte
XRL direct,#data Exclusive-OR immediate data to direct byte
Example: ANL A,1AH
ANL 22H,#11H
ORL A,R1
ORL A,@R1
XRL A,4FH
XRL 2AH,#AAH
Simulation
47. Program Control - JumpProgram Control - Jump
AJMP addr11 Absolute jump
JB rel Jump if direct bit is set
JBC bit,rel Jump if direct bit is set and clear bit
JC rel Jump if carry is set
JMP @A+DPTR Jump indirect relative to the DPTR
JNB rel Jump if direct bit is not set
JNC rel Jump if carry not set
JNZ rel Jump if Accumulator is not zero
JZ rel Jump if Accumulator is zero
LJMP addr16 Long jump
SJMP rel Short jump (relative addr)