4-instruction-set-8051

download 4-instruction-set-8051

of 23

Transcript of 4-instruction-set-8051

  • 8/7/2019 4-instruction-set-8051

    1/24

    8051 MicrocontrollerAssembly Language Programming

    Vaibhav V. Godbole

    Lecturer, Dept. of IT

  • 8/7/2019 4-instruction-set-8051

    2/24

    Objective

    8051 internal architecture

    Register Set

    Instruction SetMemory Map

    Intro to Stack, SFRs

    Assembly language programming

    2

  • 8/7/2019 4-instruction-set-8051

    3/24

    8051 Architecture

    Programmers View

    Register Set

    Instruction Set

    Memory map

    Hardware Designers View

    Pinout

    Timing characteristicsCurrent / Voltage requirements

    3

  • 8/7/2019 4-instruction-set-8051

    4/24

    Programmers View Register Set

    Registers

    A, B, R0 to R7 : 8 bit registers

    DPTR : [DPH:DPL] 16 bit register

    PC : Program Counter (Instruction Ptr) 16bits

    4 sets of register bank R0-R7

    Stack pointer SP

    P

    SW :P

    rogram Status Word (a.k.a Flags)SFR : Special Function Registers

    Control the on-board peripherals

    4

  • 8/7/2019 4-instruction-set-8051

    5/24

    Assembly Absolute Basics

    Intel Assembly format

    Operation destination source ; comment

    Values are to be preceded by a # sign

    #55, #32 etc

    Hex values are to be followed by H

    #55H, #32H

    If the first figure in a hex quantity is a letter (A-F) then a 0mustprecede it

    #0FFH, #0C1H, #0D2H

    5

  • 8/7/2019 4-instruction-set-8051

    6/24

    Register Set Accumulator A, ACC

    Commonly used for moving data around, logic

    and arithmetic operations on 8bit data

    Examples

    mov A, R0

    push ACC

    mov A, #10

    mov B, A

    mov A, 10mov A, 0xFF

    mov A, 0FFH

    21-Jan-03

    ;copy contents of R0 to A

    ;store A onto stack

    ;A 10

    ;B A

    ;A mem(10);A 0xFF

    ;same as above

    6

  • 8/7/2019 4-instruction-set-8051

    7/24

    Register Set B Register

    Commonly used as a temporary register, much

    like a 9th R register

    Used by two opcodes

    mul AB, div AB

    B register holds the second operand and will

    hold part of the result

    Upper8bits of the multiplication resultRemainder in case of division

    7

  • 8/7/2019 4-instruction-set-8051

    8/24

    Register Set R0 to R7

    Set of8 registers R0, R1, R7, each 8 bit wide

    Widely used as temporary registers

    Available in 4 banks (effectively 4x8 registers)

    Bank is chosen by setting RS1:RS0 bits in PSW

    Default bank (at power up) is the bank0

    Examples

    mov R0, A

    mov A, R0

    mov R1, #45

    ;R0A

    ;A R0

    ;R145

    8

  • 8/7/2019 4-instruction-set-8051

    9/24

    Registers - DPTR

    16 bit register, called Data Pointer

    Used by commands that access external

    memory

    Also used for storing 16bit values

    mov DPTR, #data16

    movx A, @DPTR

    ; setup DPTR with 16bit ext address

    ; copy mem[DPTR] to A

    DPTR is useful for string operations, look uptable (LUT) operations

    9

  • 8/7/2019 4-instruction-set-8051

    10/24

    Registers - PC

    PC is the program counter

    Referred to as the Instruction Pointer (IP) in

    other microprocessors

    PC points to the next program instruction

    always

    After fetching an instruction (1 or multi byte),

    PC is automatically incremented to point to thenext instruction

    10

  • 8/7/2019 4-instruction-set-8051

    11/24

    Registers - SP

    SP is the stack pointer

    SP points to the last used location of the stackpush will first increment SP and then copy data

    pop will first copy data and then decrement SP

    In 8051, stack grows upwards (from low mem to high

    mem) and can be in the internal RAM only

    On power-up, SP is at 07H

    Register banks 2,3,4 (08H to 1FH) is the default stack

    areaStack can be relocated by setting SP to the uppermemory area in 30H to 7FH

    mov SP, #32H

    11

  • 8/7/2019 4-instruction-set-8051

    12/24

    Registers - PSW

    Program Status Word is a bit addressable 8bitregister that has all the flags

    CY - Carry Flag

    Set whenever there is a carry in an arithmetic operation

    AC - Aux. Carry Flag

    Carry from D3 to D4. Used for BCD operation

    P- Parity Flag

    P=1 if A has odd number of1s

    Even parityOV - Overflow Flag

    Set if any arithmetic operation causes an overflow

    12

  • 8/7/2019 4-instruction-set-8051

    13/24

    Flags - Illustration

    Addition example

    mov A, #38

    add A, #2F

    38

    + 2F

    ---------

    67

    ---------

    CY =0

    AC =1

    P =1

    00111000

    00101111

    ---------------

    01100111

    ---------------

    13

  • 8/7/2019 4-instruction-set-8051

    14/24

    Registers - SFRs

    Control the operation

    of on-boardperipherals

    Special FunctionRegisters at directaddresses 80H to FFH

    8051 Clones may have

    additional SFRsAll registers have anaddress

    14

  • 8/7/2019 4-instruction-set-8051

    15/24

    8051 Memory map

    Separate Code and Data memory

    Code MemoryUpto 64K long (some maybe onboard) (0x0000 to 0xFFFF)

    PSEN is the controlling signal

    Can store Program only (Read only)

    Data Memory

    Upto 64K long (0x0000 to 0xFFFF)

    RD/WR are the controlling signals

    Can store data only (Read and Write)Internal RAM

    128 bytes 0x00 to 0x7F (includes register banks)

    SFRs 0x80 to 0xFF (not all available)

    15

  • 8/7/2019 4-instruction-set-8051

    16/24

    8051 - Memory Map

    Memory Start End Signal Instruction

    Type

    IRAM 0x00 0x7F mov A, xxH

    mov @Ri

    Data

    Code

    SFRs

    0x0000

    0x0000

    0x80

    0xFFFF

    0xFFFF

    0xFF

    RD, WR

    PSEN

    movx A, @DPTR

    movc A,@A+DPTR

    mov A, xxH

    Internal ROM is vendor dependant

    On power-up PC starts at 0000H inROM space

    Clones may have internal memory

    that may be used as bothCode+Data

    16

  • 8/7/2019 4-instruction-set-8051

    17/24

    8051 Instruction Set

    Data Transfer

    Move/Copy data from one location to another

    mov, movc, movx, push, pop, xch, xchd

    Logical

    Perform logic operations on dataanl, orl, xrl, clr, cpl, rl, rlc, rr, rrc, swap

    Arithmetic

    Perform arithmetic operations on data

    add, addc, subb, inc, dec, mul, div

    Program control

    Control the program flow (jumps, subroutine calls)

    jmp, ajmp, ljmp, sjmp, jc, jnc, jb, jnb, jbc, jz, jnz, acall, lcall,

    cjne, djnz, ret, reti

    NOP

    17

  • 8/7/2019 4-instruction-set-8051

    18/24

    8051 Instruction Set (contd.)

    Read through the instruction set

    Dont have to remember all the instructions

    Dont have to remember all possible cases

    Remember the types of instructions

    When writing code

    Write down the operation needed in simple English

    Lookup the instruction set to see which (combo) ofinstructions will do the job

    18

  • 8/7/2019 4-instruction-set-8051

    19/24

    Assembly Op-code

    Every assembly instruction translates to a unique

    binary Opcode

    Can be 1, 2 or 3 bytes long

    8051Family

    P

    rogrammers Guide has a listExample1: mov A, #data

    2 bytes, 1 cycles

    01110100 data8

    mov A, 0xAA 0111010010101010174 AA

    Example2: acall address11

    a10 a9 a810001 a7 a6 a5 a4 a3 a2 a1 a0

    acall 0x557 1011000101010111B157

    19

  • 8/7/2019 4-instruction-set-8051

    20/24

    Assembler Directives

    Assembly statement structure

    [label:] opcode [operands] [;comment]

    start: mov A, #D0H ;code starts here

    Assembler directives Instruct assembler to do a specialtask

    ORG xxxxH

    EQU

    count EQU 25

    : origin, start assembling at xxxxH

    : define a constant

    DB : define byte, defines allocation of storage

    DATA1:

    DATA2:

    DB

    DB

    28

    hello world

    END : end of assembly file

    20

  • 8/7/2019 4-instruction-set-8051

    21/24

    Assembly Design Flow

    Create the assembly source file test.asm

    Assemble the asm file

    C:\> as51 test.asm

    Assembler produces error and code list in test.lst If no errors, assembler produces .obj file

    Link the .obj files to produce an .abs file

    Create hex file from the .abs file

    Most assemblers directly produce the .hex fileDownload the .hex file onto the board or burn it into anEPROM.

    21

  • 8/7/2019 4-instruction-set-8051

    22/24

    Assembly Example #1Program to fill up the first 4 registers in the

    Target 8051 dev system

    Std 8051 device

    2K on-chip ROM running

    register bank with some numbers and find their

    sum

    ORG 0x30 ;skip the IVT area

    a monitor program

    32K external RAM ataddress 0x0000 to

    0x7FFF

    This RAM is both code

    Start:

    clearA:

    Addup:

    mov R0, #10

    mov R1, #0A5H

    mov R2, #1

    mov R3, #0x20

    mov A, #0 ;now A =0

    add A, R0 ;now A = A + R0

    and data

    First 0x30 locations in

    external RAM isdedicated for the Interrupt

    Vector Table (IVT)

    Done:

    add A, R1

    add A, R2

    add A, R3mov R4, A ;store sum in R4

    mov DPTR, #7FFF

    movx @DPTR, A ;store in ext. mem

    sjmp done ;loop here forever

    END

    22

  • 8/7/2019 4-instruction-set-8051

    23/24

    Class 2 Review

    What are the different views/models of a uP ?

    What are the registers available in the 8051 ?

    What are the functions of the8051

    registers ?What is stack, PC, SFR, PSW/Flags ?

    What is an instruction set ?

    What is a memory map ? Why is it needed ?

    What is an assembly language program ? How

    does it look ?

    23

  • 8/7/2019 4-instruction-set-8051

    24/24