Chapter4_Addressaing mode.pdf

  • Upload
    asit619

  • View
    229

  • Download
    0

Embed Size (px)

Citation preview

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    1/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 1

    Addressing Mode MC 8051 It is a way the MC to access data /

    operand from internal memory orexternal memory, registers or specificports.

    The use of efficient modes of aprogram (addressing mode) will

    increase the processing speed of CPUas well as processing time can beshortened.

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    2/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 2

    These two examples do the same job.

    However the second instruction set (3 bytes) have beenreduced the total bytes used compared to the firstinstruction (4 bytes), in addition it processing is fasterthan the previous command.

    Addressing Mode MC 8051

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    3/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 3

    Eight modes of addressing are available with the

    MC 8051

    The different addressing modes determine howthe operand byte is selected

    Type of Addressing Mode MC 8051

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    4/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 4

    The direct and indirect addressing modes are

    used to distinguish between the SFR space anddata memory space.

    The relative instructions are based on the value of

    the program counter.

    The absolute instructions operate in the samemanner.

    Indexed instructions use a calculation to generatethe address used as part of the instruction.

    Type of Addressing Mode MC 8051

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    5/34

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    6/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 6

    2. Direct Addressing mode

    This mode allows you to specify the operand by giving its actual

    memory address (typically specified in hexadecimal format -00Hto 7FH and also P0, P1, P2, P3 and PSW registers).

    Used for SFR accesses such as P0, P1, P2, P3 and PSW

    MOV A, P3 ;Transfer the contents of Port 3 to theaccumulator

    MOV A, 020H ;Transfer the contents of RAM location 20H tothe accumulator

    MOV P1, A ; Transfer the contents of A to Port 1

    MOV 20H, 40H; Transfer the contents of the address 40H tothe address 20H

    ADD A, 55H ; Add the contents of A with the contents ofthe address 55H

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    7/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 7

    Addressable memory used by

    direct addressing mode from 00to 7F and also address specificby special register such as

    P1(80H),

    P1(90H),P2(A0H),

    P3(B0H) and

    PSW(D0H)

    Valid memory Address for DirectAddressing Mode

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    8/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 8

    3. Indirect Addressing mode

    In the Indirect Addressing mode, a register is used to hold

    the effective address of the operand.

    This register, which holds the address, is called the pointerregister and is said to point to the operand.

    Only registers R0, R1 and DPTR can be used as pointerregisters.

    R0 and R1 registers can hold an 8-bit address whereasDPTR can hold a 16-bit address.

    DPTR is useful in accessing operands which are in theexternal memory.

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    9/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 9

    3. Indirect Addressing mode (contd)

    Examples:

    MOV @R0,A ;Store the content of accumulator into thememory location pointed to by the contents of registerR0. R0 could have an;8-bit address, such as 60H.

    MOVX A,@DPTR ;Transfer the contents from the memorylocation pointed to by DPTR into the accumulator. DPTRcould have a 16-bit adress, such as 1234H.

    MOV A, @R0 ; move the contents of RAM at locationdesignated by R0 into accumulator A

    MOV @R1, A; transfer content of Acc. A into a RAMlocation designated by the R1

    INC @R1: add one into the content designated by the R1

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    10/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 10

    3. Indirect Addressing mode (contd)

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    11/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 11

    4. Immediate Addressing mode

    This mode of addressing uses either an 8- or 16-bit

    constant value as the source operand

    This constant is specified in the instruction, rather thanin a register or a memory location

    The destination register should hold the same data sizewhich is specified by the source operand

    Examples:ADD A,#030H ;Add 8-bit value of 30H to the

    accumulator register (which is an 8-bit register).MOV DPTR,#0FE00H ;Move 16-bit data constant

    FE00H into the 16-bit Data Pointer Register.

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    12/34

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    13/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 13

    5. Relative Addressing mode

    This mode of addressing is used with some type ofjump instructions, like SJMP (short jump) and

    conditional jumps like JNZ.

    These instructions transfer control from one part of aprogram to another.

    The destination address must be within -128 and+127 bytes from the current instruction addressbecause an 8-bit offset is used (28 = 256).

    Example:GoBack: DEC A ;Decrement A

    JNZ GoBack ;If A is not zero, loop back

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    14/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 14

    5. Relative Addressing mode (contd)

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    15/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 15

    6. Indexed Addressing mode

    The Indexed addressing is useful when there

    is a need to retrieve data from a look-uptable

    A 16-bit register (data pointer/program

    counter DPTR/PC) holds the base addressand the accumulator holds an 8-bitdisplacement or index value

    The sum of these two registers forms theeffective address for a JMP or MOVCinstruction

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    16/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 16

    6. Indexed Addressing mode (contd)

    Example:MOV A,#08H ;Offset from table start

    MOV DPTR,#01F00H ;Table start address

    MOVC A,@A+DPTR ;Gets target value from the tablestart address + offset and puts it in A.

    After the execution of the above instructions,the program will branch to address 1F08H

    (1F00H+08H) and transfer into theaccumulator the data byte retrieved from thatlocation (from the look-up table)

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    17/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 17

    6. Indexed Addressing mode (contd)

    Examples of programs:

    MOV R7, # 00

    MOV DPTR, # TABLE;

    REPEAT: MOV A, R7

    MOVC A, @ A + DPTRMOV P1, A

    TABLE: DB 3FH, 06H, 5BH, 4FH, 66H, 6DH;

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    18/34

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    19/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 19

    Tutorial

    2. What happens to the accumulator (A) if all of

    the the commands below executed sequentially?

    MOV 40H, #50H ;

    MOV 50H, #40H ;

    MOV R0, #30H ;

    MOV 30H, 40H ;

    MOV R1, #40H ;

    (Ans: 50H)

    (Ans: 50H)

    (Ans: 40H)

    (Ans: 40H)

    (Ans: 80H)

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    20/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 20

    Tutorial

    3. Given the content of A is 44H and the Carry bit is 0. How many

    times have ADD instruction executed after the following programbe implemented?

    LOOP: ADD A, #3FH

    JNC LOOP

    END

    4. Given the content of A is 5AH. What are the contents of A and thebit-bit Carry (C), Auxiliary Carry (AC) and Parity (P) after thefollowing command executed?

    XRL A,#0FFH

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    21/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 21

    Tutorial

    4. Given the content of A is 29H. What are the contents of A and the

    bit-bit Carry (C), Auxiliary Carry (AC) and Parity (P) after thefollowing command executed?

    ORL A, #47H

    5. Given the content of A is A5H. What are the contents of A and thebit-bit Carry (C), Auxiliary Carry (AC) and Parity (P) after thefollowing command executed?

    ANL A, #87H

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    22/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 22

    Programming Construction

    Steps in Building a program inassembler language;

    I. Problem Definition

    II. Logic Design

    III. Programming

    IV. Assembling, Testing and Debugging

    V. Documentation

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    23/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 23

    i. Problem Definition

    Define the problem

    Reading, thinking, writing,discussions and exploratoryexperiments - to synthesize this

    information, add and tie togetherthings learned, gaining realunderstanding of the existingknowledge

    Predict the alternative solution

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    24/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 24

    ii. Logic Design

    In order to formed the sistematic solution the logic design/flow

    chart should be designed called algorithm. This technic will guide the programmer how to write the proper

    program to solve the problem that has been identified.

    Logic design simbols as below are the common symbols used todesign the flow chart.

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    25/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 25

    iii. Programming

    Translate the problem solutions in to the computer instruction

    using the programming language such as low level language(assembler) or high lever language ( Visual C, C++ etc)

    Computer instruction should be followed the flowchart/algorithmhas design before.

    This program need to be tested and may need some modificationbefore the reliability is confirmed and be used in the real system.

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    26/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 26

    iv. Assembling, Testing and Debugging

    Assembling>> The program written in

    assemble language has to compiled toproduced the object code (machine code)

    The machine code will be used by

    microcontroller to perform the processhas be design (the code understood bythe computers)

    Testing and Debugging >> Error in theprogram will be detected and removed

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    27/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 27

    v. Documentation

    Internal documentation of programs is very important, not just to you butto the NEXT programmer who will be modifying your program. Outside of

    academia, most programming consists of fixing bugs or adding features toexisting code.

    The comments you put in your source code files should be written to helpother programmers navigate through your code easily in order to find bugsor to determine where to add new features. Thus, it is important to pay

    attention to how the code is laid out on the screen (indentation matching)and to use meaningful variable names, as well as to write comments thatare concise yet clear.

    Programmer documentation should be concise so the person who reads itdoesn't have to spend too much time to find what he or she is looking for.

    All the programs and flowcharts design are keep in record as well as can beused or modified to meet user requirements for the future need.

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    28/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 28

    Building The Programming Flowchart

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    29/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 29

    Programing Structure

    Flow chart intended to show the structure of the program in the form ofpictorial

    It uses standard symbols and it is easily understood by people who are notproficient in writing program.

    There are four types of structure the program as follows: Sequence structure Looping structure Branch/Jump structure Sub-program and Subroutine structure

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    30/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 30

    Sequence Structure and Looping Structure

    The sequence structure is trivial. Simply list the statements to execute in

    the order in which they should execute.

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    31/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 31

    Brunch/Jump and Subroutine Structure

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    32/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 32

    An Example of Project Design Case Study: A robot will move forward when the sensor 1 (S1) enabled.

    When the sensor 2 (S2) is activated,) it will make a round of U (right andstop for 20 seconds, then continue to move forward. Until the sensor 3

    (S3) is activated, then the robot will stop. Draw a flow chart to illustratethe process.

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    33/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 33

    Tutorial 4: Draw a flow chart to show the processes below:

    Study Case 1: When the switch 1 (S1) is pressed, themotor 1 is ON then the water flow into tank 1. When the sensor 2 isactivated, motor 1 will be OFF and at the same time motor 2 is active to fillthe water in to tank 2. Motor 2 will OFF when the sensor S3 is ON.

    S1

    S2

    S3

    MOTOR1

    MOTOR2

    tangki 1tangki 2

  • 8/13/2019 Chapter4_Addressaing mode.pdf

    34/34

    Chapter 4: 8051 Addressing Modeby Mohd Daud bin Isa JKE, PKB. 34

    Tutorial 4: Draw a flow chart to show the processes below:

    a. Case Study 2: Bila sius kesan suhu di-ONkan, kipas di dapur di-ONkan. Kipas itu akan di-ONkan selama 3 minit dan akan di-OFFkan

    selepas itu. Pada masa itu Penghawa dingin akan di-ONkan. Penghawadingin itu akan di-OFFkan apabila suis X ditekan.

    b. Case Study 3: Dalam sebuah bilik kuliah, kipas dan lampu

    akan di-ONkan apabila suis 1 ditekan. Selepas 30 minit lampuakan di-OFFkan. Pada masa yang sama skrin LCD projectorakan diturunkan dan LCD projector akan di-ONkan. Penghawadingin akan di-ONkan selepas 4 minit LCD projector di-ONkan.Kipas, LCD projector dan penghawa dingin akan di-OFFkan 5minit selepas suis Z ditekan.