2. Assembly LanguageAssembly Language
• Provides ‘English like’ words called ‘mnemonics’Provides ‘English like’ words called ‘mnemonics’
for the machine codes.for the machine codes.
• For e.g.For e.g.
– 0010 11110010 1111 (2FH), is a 8-bit instruction in(2FH), is a 8-bit instruction in MachineMachine
languagelanguage of 8051 to add two numbersof 8051 to add two numbers
– ADD A, R7ADD A, R7 is anis an Assembly languageAssembly language instructioninstruction
of 8051 to add two numbersof 8051 to add two numbers
3. High-level LanguagesHigh-level Languages
• These languages areThese languages are machine-independentmachine-independent
• Programmer may not know the internal detailsProgrammer may not know the internal details
of the CPUof the CPU
• For e.g.For e.g. Embedded CEmbedded C, Embedded Java etc., Embedded Java etc.
• High-level language programs are translated toHigh-level language programs are translated to
machine code using a program called ‘machine code using a program called ‘CompilerCompiler’’
4. Programming using ‘Assembly’Programming using ‘Assembly’
• A program called ‘A program called ‘AssemblerAssembler’ is needed to’ is needed to
translate an Assembly Language program totranslate an Assembly Language program to
machine languagemachine language
• Assembly language is ‘Assembly language is ‘machine dependentmachine dependent’,’,
i.e. ai.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
5. • 8-bit Registers of 80518-bit Registers of 8051
• TheseThese CPU RegistersCPU Registers are used toare used to
store data temporarilystore data temporarily
• TheThe Accumulator, register AAccumulator, register A, is, is
used for all Arithmetic & Logicused for all Arithmetic & Logic
operations.operations.
• Registers R0 – R7 are for generalRegisters R0 – R7 are for general
useuse
Programming Model of 8051Programming Model of 8051
AA
BB
R0R0
R1R1
R2R2
R3R3
R4R4
R5R5
R6R6
R7R7
6. Assembly Language programAssembly Language program
• Consists of a series of Assembly languageConsists of a series of Assembly language
instructionsinstructions
• 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 25H in R5;load 25H in R5
MOVMOV A, #5A, #5 ;load 5 in A;load 5 in A
ADDADD A, R5A, R5 ;add A & R5;add A & R5
7. 80518051 FlagsFlags andand PSWPSW RegisterRegister
• The flag register in 8051 is calledThe flag register in 8051 is called Program StatusProgram Status
WordWord (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), P(Parity)AC(Auxiliary Carry), OV(Overflow), P(Parity)
CYCY ACAC F0F0 RS1RS1 RS0RS0 OVOV ---- PP
D7D7 D6D6 D5D5 D4D4 D3D3 D2D2 D1D1 D0D0
8. 8051 Flags8051 Flags
• CY(Carry Flag)CY(Carry Flag) is SETis SET
– if aif a carrycarry is generated afteris generated after additionaddition
– if aif a borrowborrow is generated afteris generated after subtractionsubtraction
• Can be SET/CLEARED by usingCan be SET/CLEARED by using
instructions such asinstructions such as
SETB 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 D4If there is a carry from bit D3 to D4
– Used for BCD arithmeticUsed for BCD arithmetic
9. 8051 Flags8051 Flags
• P(Parity Flag)P(Parity Flag) reflects no. of 1’s in register Areflects no. of 1’s in 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-order bit toDetects the overflow of the higher-order bit to
the sign bitthe sign bit
10. 8051 Register Banks8051 Register Banks
• 8051 has 4 register banks8051 has 4 register banks
• Each bank has 8 registers named R0 to R7Each bank has 8 registers named R0 to R7
• Only one register bank can be accessed byOnly one register bank can be accessed by
8051 at a time8051 at a time
• BitsBits RS0RS0 && RS1RS1 ofof PSWPSW register are used toregister are used to
select any one of the four register banksselect any one of the four register banks
12. RAM space allocation in 8051RAM space allocation in 8051
• 8051 has8051 has 128 bytes128 bytes of RAMof RAM
• RAM address space isRAM address space is 00H00H toto 7FH7FH
• 8051 RAM is divided into 3 groups8051 RAM is divided into 3 groups
– Register banks and StackRegister banks and Stack
– Bit-addressable memoryBit-addressable memory
– Scratch Pad memoryScratch Pad memory
18. Assembler DirectivesAssembler Directives
• are instructions to the Assembler programare instructions to the Assembler program
• For e.g.For e.g.
ORGORG 0H0H ;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
19. Assembler Directives Cont.Assembler Directives Cont.
• ORGORG tells Assembler to place the followingtells Assembler to place the following
opcodes starting at memory location 0opcodes starting at memory location 0
• ENDEND indicates to the Assembler the end ofindicates to the Assembler the end of
the source codethe source code
• Assembler directives are also calledAssembler directives are also called pseudo-pseudo-
instructionsinstructions
• No opcodesNo opcodes are generated by assembler forare generated by assembler for
assembler directivesassembler directives
20. Assembling & Running a 8051 programAssembling & Running a 8051 program
Editor Program
Assembler Program
Linker Program
OH Program
Hex file