1347 assembly language programming of 8051
DESCRIPTION
visit: www.techbed.blogspot.comTRANSCRIPT
Assembly Assembly Language Language
Programming of Programming of 80518051
Assembly LanguageAssembly Language
• Provides ‘English like’ words called Provides ‘English like’ words called ‘mnemonics’ for the machine codes.‘mnemonics’ for the machine codes.
• For e.g. For e.g. – 0010 11110010 1111 (2FH), is a 8-bit (2FH), is a 8-bit
instruction in instruction in Machine languageMachine language of of 8051 to add two numbers8051 to add two numbers
– ADD A, R7ADD A, R7 is an is an Assembly languageAssembly language instruction of 8051 to add two instruction of 8051 to add two numbersnumbers
High-level LanguagesHigh-level Languages
• These languages are These languages are machine-machine-independentindependent
• Programmer may not know the Programmer may not know the internal details of the CPUinternal details of the CPU
• For e.g. For e.g. Embedded CEmbedded C, Embedded Java , Embedded Java etc.etc.
• High-level language programs are High-level language programs are translated to machine code using a translated to machine code using a program called ‘program called ‘CompilerCompiler’’
Programming using Programming using ‘Assembly’‘Assembly’
• A program called ‘A program called ‘AssemblerAssembler’ is ’ is needed to translate an Assembly needed to translate an Assembly Language program to machine Language program to machine languagelanguage
• Assembly language is ‘Assembly language is ‘machine machine dependentdependent’, i.e. a ’, i.e. a low-levellow-level programming languageprogramming language
• A programmer must know theA programmer must know the– Internal details of CPUInternal details of CPU– Instruction setInstruction set
• 8-bit Registers of 80518-bit Registers of 8051• These These CPU RegistersCPU Registers are are
used to store data used to store data temporarilytemporarily
• The The Accumulator, register Accumulator, register AA, is used for all , is used for all Arithmetic & Logic Arithmetic & Logic operations.operations.
• Registers R0 – R7 are for Registers R0 – R7 are for general usegeneral use
Programming Model of Programming Model of 80518051
AA
BB
R0R0
R1R1
R2R2
R3R3
R4R4
R5R5
R6R6
R7R7
Assembly Language Assembly Language programprogram
• Consists of a series of Assembly Consists of a series of Assembly language instructionslanguage instructions
• Structure of Assembly LanguageStructure of Assembly Language– Has four fieldsHas four fields[label:][label:] mnemonicmnemonic [operands][operands]
[;comments][;comments]start:start: MOVMOV R5, #25HR5, #25H ;load ;load
25H in R525H in R5 MOVMOV A, #5A, #5;load 5 in A;load 5 in AADDADD A, R5A, R5 ;add A ;add A
& R5& R5
8051 8051 FlagsFlags and and PSWPSW RegisterRegister
• The flag register in 8051 is called The flag register in 8051 is called Program Status WordProgram Status Word (PSW)register (PSW)register
• PSW is a 8-bit registerPSW is a 8-bit register• 8051 has 4 conditional flags – CY(Carry), 8051 has 4 conditional flags – CY(Carry),
AC(Auxiliary Carry), OV(Overflow), AC(Auxiliary Carry), OV(Overflow), P(Parity)P(Parity)
CYCY ACAC F0F0 RS1RS1 RS0RS0 OVOV ---- PP
D7D7 D6D6 D5D5 D4D4 D3D3 D2D2 D1D1 D0D0
8051 Flags8051 Flags
• CY(Carry Flag)CY(Carry Flag) is SET is SET – if a if a carrycarry is generated after is generated after additionaddition – if a if a borrowborrow is generated after is generated after
subtractionsubtraction• Can be SET/CLEARED by using Can be SET/CLEARED by using
instructions such asinstructions such asSETB C, CLR CSETB C, CLR C
• AC(Auxiliary Carry Flag) is SETAC(Auxiliary Carry Flag) is SET– If there is a carry from bit D3 to D4 If there is a carry from bit D3 to D4 – Used for BCD arithmeticUsed for BCD arithmetic
8051 Flags8051 Flags
• P(Parity Flag)P(Parity Flag) reflects no. of 1’s reflects no. of 1’s in register Ain register A– SET if ODD no. of 1’sSET if ODD no. of 1’s– RESET if EVEN no. of 1’sRESET if EVEN no. of 1’s
• OV(Overflow Flag)OV(Overflow Flag)– Used with signed numbersUsed with signed numbers– Detects the overflow of the higher-Detects the overflow of the higher-
order bit to the sign bitorder bit to the sign bit
8051 Register Banks8051 Register Banks
• 8051 has 4 register banks8051 has 4 register banks• Each bank has 8 registers named Each bank has 8 registers named
R0 to R7R0 to R7• Only one register bank can be Only one register bank can be
accessed by 8051 at a timeaccessed by 8051 at a time• Bits Bits RS0RS0 & & RS1RS1 of of PSWPSW register register
are used to select any one of the are used to select any one of the four register banksfour register banks
Register Bank Selection Register Bank Selection bitsbits
CYCY ACAC F0F0 RS1RS1 RS0RS0 OVOV ---- PP
D7D7 D6D6 D5D5 D4D4 D3D3 D2D2 D1D1 D0D0
Register Register BankBank
RSRS00
RS1RS1 AddressAddress
00 00 00 00 – 07H00 – 07H
11 00 11 08 – 0FH08 – 0FH
22 11 00 10H – 10H – 17H17H
33 11 11 18H – 18H – 1FH1FH
RAM space allocation in RAM space allocation in 80518051
• 8051 has 8051 has 128 bytes128 bytes of RAM of RAM• RAM address space is RAM address space is 00H00H to to
7FH7FH• 8051 RAM is divided into 3 8051 RAM is divided into 3
groupsgroups– Register banks and StackRegister banks and Stack– Bit-addressable memoryBit-addressable memory– Scratch Pad memoryScratch Pad memory
00
07
08
0F
10
17
18
1F
20
2F
30
7F
Register Bank 0
Register Bank 1 (Stack)
Register Bank 2
Register Bank 3
Bit Addressable RAM
Scratch Pad RAMR
AM
Sp
ace a
llocati
on
in
80
51
Default Register BankDefault Register Bank
77
66
55
44
33
22
11
00
RR77
RR66
RR55
RR44
RR33
RR22
RR11
RR00
RR77
RR66
RR55
RR44
RR33
RR22
RR11
RR00
RR77
RR66
RR55
RR44
RR33
RR22
RR11
RR00
11FF
11EE
11DD
11CC
11BB
11AA
1199
1188
1177
1166
1155
1144
1133
1122
1111
1100
FF
EE
DD
CC
BB
AA
99
88
RR77
RR66
RR55
RR44
RR33
RR22
RR11
RR00
Bank 0
Bank 1
Bank 2
Bank 3
Using RAM addressesUsing RAM addresses
MOV R2, #3FHMOV R2, #3FH MOV 02, #3FHMOV 02, #3FH
MOV R1, #85HMOV R1, #85H MOV 01, #85HMOV 01, #85H
MOV R0, #99HMOV R0, #99H MOV 00, #99HMOV 00, #99H
77
66
55
44
33
22
11
00
RR77
RR66
RR55
RR44
RR33
RR22
RR11
RR00
Bank 0
Q. Which register bank is used by default, when 8051 is powered up?
Ans. Register Bank 0
How to switch Register How to switch Register banks?banks?
CYCY ACAC F0F0 RS1RS1 RS0RS0 OVOV ---- PP
D7D7 D6D6 D5D5 D4D4 D3D3 D2D2 D1D1 D0D0
Register Register BankBank
RSRS00
RS1RS1 AddressAddress
00 00 00 00 – 07H00 – 07H
11 00 11 08 – 0FH08 – 0FH
22 11 00 10H – 10H – 17H17H
33 11 11 18H – 18H – 1FH1FH
Program Status Word Register
SETB PSW.4SETB PSW.4 ; select bank 2; select bank 2
MOV R0, #99HMOV R0, #99H
MOV R1, #85HMOV R1, #85H
MOV R2, #3FHMOV R2, #3FH
MOV 10, #99HMOV 10, #99H
MOV 11, #85HMOV 11, #85H
MOV 12, #3FHMOV 12, #3FH
RR77
RR66
RR55
RR44
RR33
RR22
RR11
RR00
1177
1166
1155
1144
1133
1122
1111
1100
Bank 2
Assembler DirectivesAssembler Directives
• are instructions to the Assembler are instructions to the Assembler programprogram
• For e.g.For e.g.ORGORG 0H 0H ;start at location 0;start at location 0
begin:begin: MOV R2, #5MOV R2, #5 ;load 5 in R2;load 5 in R2
MOV R3, #6MOV R3, #6 ;load 6 in R3;load 6 in R3
MOV A, R2MOV A, R2 ;copy R2 in A;copy R2 in A
ADD A, R3ADD A, R3 ;add A & R3;add A & R3
MOV R4, AMOV R4, A ;store result in R4;store result in R4
ENDEND ;end of asm source file;end of asm source file
Assembler Directives Assembler Directives Cont.Cont.
• ORGORG tells Assembler to place the tells Assembler to place the following opcodes starting at following opcodes starting at memory location 0memory location 0
• ENDEND indicates to the Assembler the indicates to the Assembler the end of the source codeend of the source code
• Assembler directives are also called Assembler directives are also called pseudo-instructionspseudo-instructions
• No opcodesNo opcodes are generated by are generated by assembler for assembler directivesassembler for assembler directives
Assembling & Running a 8051 Assembling & Running a 8051 programprogram
Editor Program
Assembler Program
Linker Program
OH Program
Hex file