8051 assembly language programming

51
8051 assembly 8051 assembly language language Programming Programming

Upload: vishal-gudla-nagraj

Post on 25-Sep-2015

94 views

Category:

Documents


8 download

TRANSCRIPT

  • 8051 assembly language Programming

  • DEVELOPMENT ENVIRONMENT

    Host The PC used to develop the program, it usually has Editor (edit source file) Compiler (convert high level language to machine code *.obj) Assembler (convert assembly language to machine code *.obj) Linker (link several obj files into an absolute obj file) Obj to Hex converter (convert obj file to Hex file) Loader (load the hex file to target) Target The development hardware with embedded microcontroller. It can be connected to Host through various interfaces E.g. RS232, USB, JTAG, IEEE1394, Host Target board

  • CONTENTSPROGRAMMING STEPSINSRUCTION SETADDRESSING MODESASSEMBLY PROGRAM EXAMPLES

  • Steps to Create a Program

  • Editor Program Allows user to type assembly language program with extension .asm or .a51Assembler Program Converts assembly language code into machine language i.e opcodes are generated for written instructions, that can be understood by MCU. It will generate two files as follows1. List file (.lst extension) Contains list of all addresses & instructions .2.Object File (.obj extension) It is binary file contains binary code.Linker ProgramCombine one or more object file into absolute object file no extension.Object to Hex converterConverter absolute object file into file with extension Hex which can be burn into ROM of 8051Burn the hex file into ROM

  • Structure of Assembly LanguageORG 0H ;start (origin) at location 0 MOV R5,#25H ;load 25H into R5 MOV R7,#34H ;load 34H into R7 MOV A,#0 ;load 0 into A ADD A,R5 ;add contents of R5 to A ;now A = A + R5 ADD A,R7 ;add contents of R7 to A ;now A = A + R7 ADD A,#12H ;add to A value 12H ;now A = A + 12HHERE: SJMP HERE ;stay in this loop END ;end of asm source file Program 2-1:Sample of an Assembly Language Program

  • ASSEMBLY: STRUCTURE

    Structures of assembly language 1. A series of lines of assembly language instructions and/or directives 2. An assembly language instruction consists of up to 4 fields [label:] mnemonic [operands] [;comments] Label: allows the program to refer to a line of code by name E.g. HERE: SJMP HERE Mnemonic and operands The combination of mnemonic and operands will be translated to binary machine code E.g. MOV A, #23H ;Opcode: 0111 0100 0010 0011 (7423H)

  • 8051 Program Counter & ROM SpaceExecute a Program Byte by Byte PC=0000: opcode 7D fetched; 25 fetched; R525; PC+2

  • ASSEMBLY: DIRECTIVES

    Directives A pseudo-code that cannot be translated into machine code Used to notify assembler of certain operations E.g. END: notify assembler the end of the source file. Commonly used directives ORG: origin Indicate the beginning of the address The number after ORG can be either in hex or decimal DB: define byte Define an 8-bit data

  • Directive examples ORG 500HDATA1: DB 28 ;DECIMAL (1C in Hex)DATA2: DB 00110101B ;BINARY (35 in Hex)DATA3: DB 39H ;HEX

    ORG 510HDATA4: DB 2591 ; ASCII NUMBERS

    ORG 518HDATA6: DB My name is Joe ;ASCII CHARACTERS

  • Commonly used directives

    EQU: equate Define a constant without occupying a memory location It DOES NOT use any memory space! The constant value can be used later in the program To improve the readability of the program Give a name to a constant To improve code efficiency If the same constants are used twice in the program,EQU directive, we only need to change it in one locationvalue is changed We should avoid using constant directly, and use thedirective as often as possible. Example (Demo directives)COUNT EQU 25HMOV R3, #COUNTMOV A, #COUNT

  • More examples directives

    EQUUsed to create symbols that can be used to represent registers, numbers, and addressesLIMIT EQU 2000SERIAL EQU SBUFSCK EQU P1.3MY_VAL EQU 0x44DATAUsed to define a name for memory locationsSP DATA 0x81 ;special function registersMY_VAL DATA 0x44 ;RAM location

    cseg stands for code segment

  • Instruction Set

    8051 instructions have 8-bit opcode There are 256 possible instructions of which 255 are implemented Some instructions have one or two additional bytes for data or address There are 139 1-byte instructions, 92 2-byte instructions, and 24 3-byte instruction

  • Instruction FormatsInstruction :- It is specific task that CPU can perform,it consists of opcode & zero or more operands.Opcode :- machine code for specific operation to be perform to microcontroller. e.g ADD,SUB,ANLOperands :- data or address used by the instruction.

  • The 8051 Instruction FormatsThe 8051 instruction formats.

  • Types of operands

    There are three types of operands or three places from where data is available

    1 . direct data/immediate valuesConstant integer (8 bits)Constant value is stored within the instruction2. registersName of a register is specifiedRegister number is encoded within the instruction

    3. memoryReference to a location in memoryMemory address is encoded within the instruction, orRegister holds the address of a memory location

  • Addressing Modes

    The addressing mode means where and how the CPU gets the operands when the instruction is executed

    Supported by dedicated hardware components existing within the CPU as well as by the underlying Control unit i.e. Completely Architecture Dependent

    There are five addressing modes available in the 8051 , Immediate Register DirectRegister IndirectIndexed

  • Immediate addressing mode-works on constant data by using # Register addressing mode-woks with registers available in 8051Addressing mode for non memory access

  • Immediate Addressing

    The source operand is a constant E.g. MOV A, #25HMOV R3, #62MOV DPTR, #4521H ;MOV DPH, #45H MOV DPL, #21H ; The following instructions are illegalMOV DPTR, #9F235H ; require more than 16-bitMOV DPTR, #68975 ; FFFFH = 65535

  • Some special cases of immediate addressing mode

    1. Using the EQU directiveCOUNT EQU 25HMOV A, #COUNT ; opcode: 7425H

    2. Using address labels

    MOV DPTR, #MYDATA ; (DPTR) = 200HORG 200HMYDATA: DB 23H, 35H #MYDATA is the address of the contents 2335H in ROM NOTE: MOV DPTR, MYDATA is illegal

    3. ASCII codeMOV A, #A ; the ASCII code of A is loaded into register A.

  • Register Addressing

    Use register to hold the data to be manipulated ExampleMOV A, R0MOV R2, AMOV R7, DPL Notes MOV R2, R5 is invalid. MOV A, DPTR is invalid (why?)

  • Addressing mode for memory accessThere are 3 different addressing mode to access memory Direct: use the address of the memory Register indirect: use register to store RAM address Indexed: use DPTR register to store ROM address

  • Direct Addressing

    Direct addressing can access any on-chip memory location Example: ADD A,55H Example: MOV P1, A Transfers the content of accumulator to Port 1 (address 90H)

  • Register Indirect Addressing ModeR0 or R1 may operate as pointer registers (their content indicates an address in internal RAM where data are written or read) In 8051 assembly language, indirect addressing is represented by an @ before R0 or R1. Example: MOV A, @R0 Moves a byte of data from internal RAM at location whose address is in R0 to the accumulator Example:MOV R0, #60HLoop : MOV @R0,#0INC R0CJNE R0,#80H,Loop

  • Relative Addressing

    Relative addressing is used with certain jump instructions Relative address (offset) is an 8-bit signed value (-128 to 127) which is added to the program counter to form the address of next instruction Prior to addition, the program counter is incremented to the address following the jump (the new address is relative to the next instruction, not the address of the jump instruction) This detail is of no concern to the user since the jump destinations are usually specified as labels and the assembler determines the relative offset Advantage of relative addressing: position independent codesExample here: sjmp here

  • Absolute Addressing

    Absolute addressing is only used with ACALL and AJMP The 11 least significant bits of the destination address comes from the opcode and the upper five bits are the current upper five bits in the program counter(PC).The destination is in the same 2K (211) of the source

  • Long Addressing

    Long addressing is used only with the LCALL and LJMP instructions These 3-bytes instructions include a full 16-bitdestination address as bytes 2 and 3The full 64K code space is availableThe instruction is long and position dependent Example: LJMP, 8AF2H - Jumps to memory location 8AF2H

  • Indexed Addressing

    Indexed addressing uses a base register (either theprogram counter or data pointer) and an offset (theaccumulator) in forming the effective address for a JMPor MOVC instruction Example: MOVC A, @A+DPTR This instruction moves a byte of data from code memory to the accumulator. The address in code memory is found by adding the accumulator to the data pointer

  • Addressing Modes

  • Types of instructions Data transfer (moving data between registers ,internal & external memory)e.g mov a,r1Data processing (performing arithmetic & logical operations on data)e.g add a,r1Flow control (skipping some part of code depending upon logic developed, transfer flow of program to another location)e.g sjmp,lcall

  • Data transfer - InternalRegister related

    MOV source,destionation

    MOV a, byte ;move byte to accumulator

    MOV byte, a ;move accumulator to byte

    MOV Rn, byte ;move byte to register of ;current bankMOV direct, byte ;move byte to internal RAM

    MOV @Rn, byte ;move byte to internal RAM ;with address contained in RnMOV DPTR, data16 ;move 16-bit data into data ;pointer

  • Exchange instruction

    XCH a, byte;exchange accumulator and;byteXCHD a, byte;exchange low nibbles of ;accumulator and byteStack related

    PUSH byte;increment stack pointer, ;move byte on stackPOP byte;move from stack to byte, ;decrement stack pointer

  • Data Transfer Instructions - External

    16-bit addresses uses all Port 2 for high-byte and this portcannot be used for I/O

    MOVX is used for external data transfer

    Example: Read the content of external RAM locations 10F4H and 10F5H and place values in R6 and R7, respectively.

    MOV A,#00HMOV DPTR,#10F4HMOVX, A,@DPTRMOV R6,AINC DPTRMOVX A,@DPTRMOV R7,A

  • MOVC loads the accumulator with a byte from code (program)memory (Reading Data from Memory)

    The address of the byte fetched is the sum of the originalunsigned 8-bit accumulator contents and the content of a 16-bit register (either the data pointer or PC). In the latter case, the PC is incremented to the address of the followinginstruction before being added to the accumulator

    MOVC A, @A+DPTRMOVC A,@A+PC

    This instruction is useful in reading data from LUTs.

    DPTR or PC is initialized to the beginning of the LUT and the index number of the desired entry is loaded into the accumulator.

  • Data processingArithmeticLogicalBoolean

  • Arithmetic Instructions

    Example:ADD A,7FHADD A,@R0ADD A,R7ADD A,#35H All arithmetic instructions are executed in one machine cycle except INC DPTR (two cycles) and MUL AB andDIV AB (four cycles)

  • Logical Instructions

    8051 logical instructions perform Boolean operations on bytes of data on a bit-by-bit basis . Example: lets assume A=00110101B. Instruction ANL A,#01010011B will leave 00010001 in accumulator Examples logical instructions:ANL A,55HANL A,@R0ANL A,R6ANL A,#33HRL ASWAP ARR A

  • Boolean Instructions

    8051 contains a complete Boolean processor for single-bit operations. All bit accesses use direct addressing Bits may be set or cleared in a single instruction Example: SETB P1.7 CLR P1.7CLR C

  • Branching Instructions

    There are three versions of JMP instruction: SJMP, LJMP and AJMP.SJMP instruction specifies destination address as a relative offset. This instruction is 2 bytes and jump distance is limited to -128 to 127.

    LJMP specifies the destination address as a 16-bit constant. The destination address can be anywhere in the 64K program memory space

    AJMP specifies the destination address as an 11-bit constant. Destination must be within a 2K block of AJMP.

    In all cases, programmer specifies the destination address to the assembler (using label or a 16-bit constant) and theassembler puts destination address into correct format.

  • Subroutines and Interrupts

    There are two versions of the CALL instruction: ACALL and LCALL using absolute and long addressing. Generic CALL may be used if the programmer does not care which way the address is coded

    Either instruction pushes the contents of the PC on thestack and loads the PC with the address specified in the instruction. Note that the PC will contain the address of the instruction following the CALL instruction when it gets pushed on the stack

    The PC is pushed on the stack low-byte first, high-bytesecond

    Subroutines should end with an RET instruction

    RET pops the last two bytes off the stack and places them in the PC

    Jumping in or out of a subroutine any other way usuallyfouls up the stack and causes the program to crash

  • Conditional Jump

    The 8051 offers a variety of conditional jump instructions JZ and JNZ tests the accumulator for a particular conditionDJNZ (decrement and jump if not zero) is a useful instruction forbuilding loops To execute a loop N times, load a register with N and terminate theloop with a DJNZ to the beginning of the loop CJNE (compare and jump if not equal) is another conditional jumpinstruction CJNE: two bytes in the operand field are taken as unsignedintegers. If the first one is less than the second one, the carry is set Example: It is desired to jump to BIG if the value of the accumulatoris greater than or equal to 20HCJNE A,#20H,$+3JNC BIG $ is an assembler symbol representing the address of the current instruction Since CJNE is a 3-byte instruction, $+3 is the address of next instruction JNC

  • Delay Calculation

  • Delay Calculation

  • Increasing Delay Using NOP

  • Large Delay Using Nested Loop

  • 8051 Instructions (1)The 8051 Instruction set.

  • 8051 Instructions (2)The 8051 Instruction set.

  • 8051 Instructions (3)The 8051 Instruction set.

    *