programming of microprocessors

Upload: circuit-controlar

Post on 03-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Programming of MIcroprocessors

    1/14

    24-Oct-

    Programming of Microprocessors

    Slides Prepared By

    Hardeep Singh

    Lecturer/ECE

    SBBSIET, Padhiana

    Introduction A Computer can do only what the programmer asks

    to do.

    To perform a particular task the programmerprepares the sequence of instruction.

    A set of programs for a particular computer is calledsoftware of the computer.

    Program written in the form of 0s and 1s calledmachine language program.

    Problems related with machine language

    Difficult to understand or debug a program Programs are long

    Program writing is difficult and tiresome

    Chances of careless errors in writing the program

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 2

  • 8/12/2019 Programming of MIcroprocessors

    2/14

    24-Oct-

    To facilitate programmer machine codes canbe written in hexadecimal system.

    Hexadecimal can easily be written inhexadecimal system compared to binarysystem.

    Microprocessor kit converts hexadecimal intobinary as the microcomputer understands aprogram only in binary system

    The hexadecimal is also not convenient tounderstand so we develop mnemonics for

    each hexadecimal code which is in userfriendly language

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 3

    Assembly Language

    A programmer easily write a program inalphanumeric symbols instead of binary orhexadecimal.

    Meaningful and easily rememberable symbols arechosen for the purpose. Example: ADD for addition

    SUB for subtraction, etc.

    Such symbols are called mnemonics. Program written in mnemonics are called

    Assembly language program

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 4

  • 8/12/2019 Programming of MIcroprocessors

    3/14

    24-Oct-

    A microprocessor specific language is called low levellanguage.

    A program which translate as assembly languageprogram into machine language program is calledassembler

    Self Assembler

    present on microcomputer which translates the code

    Disadvantage is that it requires lot of memory space and time totranslates the code

    Cross Assembler

    Installed on desktop computer

    Converts code into machine level objects and transfer tomicrocomputer

    Disassembler Converts Machine code into assembly language program

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 5

    Addition of two numbers placed in two consecutive memory

    locations 2501H and 2502H

    Mnemonics Operands Memory

    Address

    Machine Codes Comments

    LXI H, 2501H 2000 21, 01, 25 Get the address of the 1st

    number in HL pair

    MOV A,M 2003 7E Get the 1st number in

    accumulator

    INX H 2004 23 Increment the content of

    HL pair

    ADD M 2005 86 Add the 1st and 2nd number

    STA 2503H 2006 23, 03, 25 Store sum in 2503

    HLT 2009 76 Stop

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 6

  • 8/12/2019 Programming of MIcroprocessors

    4/14

    24-Oct-

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 7

    Stack During the execution of a program sometimes it is

    necessary to save the contents of the certain registersbecause the registers are required for some otheroperation in subsequent steps.

    These contents moved to certain memory location tocertain memory locations by PUSH operations

    After completing these operations those contents whichwere saved in the memory are transferred back to theregisters by POP operations.

    Memory locations for this purpose is set aside by theprogrammer in the beginning.

    The set of these memory locations are called stack

    The last memory location of the occupied portion of thestack is called stacktop.

    A 16-bit special register (Stack Pointer) holds the address ofstacktop

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 8

  • 8/12/2019 Programming of MIcroprocessors

    5/14

    24-Oct-

    The stack pointer is initialized in the beginning

    of the program by LXI SP or SPHL instructions. Any of RAM can be used by stack.

    There is no restriction on the location of the

    stack in the memory.

    Data stored in the stack on last-in-first-out

    (LIFO) principle

    Stack access is faster than memory access

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 9

    Stack Related Instructions

    PUSH Rp

    POP Rp

    SPHL

    XTHL

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 10

  • 8/12/2019 Programming of MIcroprocessors

    6/14

    24-Oct-

    PUSH Rp

    Push the contents of register pair on to stack

    This instruction is used to write 16 bit data on the stack

    When this instruction is executed the contents ofspecified register pair are copied on the stack in thefollowing sequence. The stack pointer in decremented by one and the contents

    of higher order register of the specified register pair arecopied to the memory location pointed the stack pointer.

    The stack pointer is again decremented by 1 and thecontents of lower order of register pair are copied to

    memory location pointed by the stack pointer. Example: PUSH B

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 11

    POP Rp

    POP off stack contents to register pair

    When this instruction is executed, the contents ofmemory location pointed by the stack pointer registerare copied to the lower order byte of register pair

    The stack pointer in incremented by one and thecontents of that memory location are copied to thehigher order byte of register pair.

    The stack pointer is again incremented by one

    The process of POP is exactly opposite to PUSHoperation. So the contents stored by PUSH are takenback using POP instructions.

    Example POP B

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 12

  • 8/12/2019 Programming of MIcroprocessors

    7/14

    24-Oct-

    SPHL

    Load stack pointer with HL register paircontents

    When this instruction is executed, thecontents of HL pair are transferred to the stackpointer

    The contents of H register is copied to higherorder byte of stack pointer and the contents

    of L register are copied to lower byte of stackpointer

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 13

    XTHL

    Exchange HL with top of stack

    When this instruction is executed the contents

    of L register are exchanged with stack location

    pointed by the stack pointer. The contents of

    H register are exchanged with the next stack

    location

    The contents of the stack pointer are not

    altered.

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 14

  • 8/12/2019 Programming of MIcroprocessors

    8/14

    24-Oct-

    LXI SP

    This instruction initialized the stack pointer

    with 16 bit address

    The stack point can be intialized by two

    methods

    Direct method: LXI SP, Data 16 Bit

    Indirect method : LXI H, Data 16 Bit SPHL

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 15

    Some other Commands

    INX SP

    This instruction increments the Stack Pointer by 1.

    DAD SP

    This instruction adds the contents of SP with the

    contents of HL pair. The result of addition is stored

    into the HL pair

    DCX SP

    This instruction decrements the stack pointer by 1

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 16

  • 8/12/2019 Programming of MIcroprocessors

    9/14

    24-Oct-

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 17

    2604

    2605

    2606

    2607 SP

    2608 6B 2608

    2609

    2610

    Subroutines

    While writing a program certain operations may occurseveral times and they are not available as individualinstructions.

    Example is multiplication of numbers, as this programis used again & again

    The concept of subroutines is developed to avoid therepetition of smaller programs.

    The small program for a particular task is calledsubroutine.

    Subroutine written separately & stored in the memory They are called at various points of the main program

    by CALL instruction where they required.

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 18

  • 8/12/2019 Programming of MIcroprocessors

    10/14

    24-Oct-

    The last instruction of subroutine is RET

    After completion of subroutine, the main programbegins from the instruction immediately followingthe CALL instruction

    When program jumps from the main program tosubroutine , the counter of the program counter issaved in the stack so that the program executionmay come back to the main program as the proper

    point after the completion of the subroutine.

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 19

    Introduction to Programming

    Step 1: Define the problem

    Step 2: Solution Plan

    Step 3: Flowchart

    Step 4: Program

    Step 5: Check the result

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 20

  • 8/12/2019 Programming of MIcroprocessors

    11/14

    24-Oct-

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 21

    Direction Arrow

    Input & Output Unit

    Process

    Operation

    Decision Unit

    Predefined

    Process/

    Subrotines

    Examples of Assembly Programming

    Example 1: Place 05 in register B

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 22

    Memory

    Address

    Machine

    Codes

    Mnemonics Operands Comments

    FC00 06, 05 MVI B, 05 GET 05 IN

    REGISTER B

    FC02 76 HLT STOP

  • 8/12/2019 Programming of MIcroprocessors

    12/14

    24-Oct-

    Example 2: Get 05 in register A and

    then move it to register BMemory

    Address

    Machine Codes Mnemonics Operands Comments

    FC00 3E, 05 MVI A, 05 Get 05 in

    register A

    FC01 47 MOV B, A Transfer 05

    from register A

    to register B

    FC02 76 HLT Stop

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 23

    Place 05 in the accumulator. Increment it by one and

    store the results in the memory location FC50 H

    Memory

    Address

    Machine Codes Mnemonics Operands Comments

    FC00 3E, 05 MVI A, 05 Get 05 in the

    accumulator

    FC01 3C INR A Increment the

    contentof the

    accumulator by

    one

    FC03 32, 50, FC STA FC05 H Store result in

    FC05 H

    FC06 76 HLT Halt

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 24

  • 8/12/2019 Programming of MIcroprocessors

    13/14

    24-Oct-

    8 Bit SubtractionMemory

    Address

    Machine

    Codes

    Mnemonics Operands Comments

    2000 21, 01, 25 LXI H, 2501 H Get address of 1st number in H-

    L pair

    2003 7E MOV A, M 1st number in accumulator

    2004 23 INX H Content of H-L pair increases

    from 2501 H to 2501 H

    2005 96 SUB M 1st Number 2nd Number

    2006 23 INX H Content of H-L pair becomes

    2503 H

    2007 77 MOV M, A Store result in 2503 H

    2008 76 HLT Halt

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 25

    Problem

    Write a program to provide the given traffic lights

    (Green, Yellow, Red) and two pedestrian (Walk

    and Dont Walk)

    The signal lights and signs turned on/off by the

    data bits of an output port as shown below

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 26

    Lights Data Bits On Time (Seconds)

    Green DO 15

    Yellow D1

    5

    Red D2 20

    Walk D3 15

    Dont Walk D4 25

  • 8/12/2019 Programming of MIcroprocessors

    14/14

    24-Oct-

    Problem Analysis

    The problem is primarily concerned with providing varioustime delays for a complete sequence of 40 seconds.

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 27

    Timein

    Sequence

    Dont

    Walk

    Walk Red Yellow Green Hex

    Code

    D7 D6 D5 D4 D3 D2 D1 D0

    0

    15 0 1 0 0 0 0 0 1 41H

    20 1 0 0 0 0 1 0 0 86H

    40 1 0 0 1 0 0 0 0 90H

    24-Oct-11 Hardeep Singh/Lecturer/SBBSIET 28

    START

    Turn on Green Light