ch03 addressing modes

Upload: hanin-baker-abubaker

Post on 08-Apr-2018

262 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/7/2019 Ch03 Addressing Modes

    1/27

    1Dr. Bassel SoudanProcessors & Assembly Language

    Chapter 38088/8086 Assembly Language Programming

    The Addressing Modes

    2Dr. Bassel SoudanProcessors & Assembly Language

    Assembly Language

    The native language for all microprocessors ismachine language.

    However, machine language is very difficult todeal with.

    Therefore, a matching symbolic language wascreated Assembly Language.

    The assembly language instructions are identical tothose of the processors machine language except insymbolic form.

    Each machine language instruction has a single uniquematching assembly language instruction (and viceversa).

  • 8/7/2019 Ch03 Addressing Modes

    2/27

    3Dr. Bassel SoudanProcessors & Assembly Language

    Assembly Language Instruction

    Every assembly language instruction is made up of 2parts:

    Mnemonic (Opcode) Symbolic name for the operation the

    instruction performs. Operands Specifiers for where the data for the instruction

    is coming from (sources) and where are the results going togo (destination).

    Two optional parts can be added to the instruction forreadability:

    Label Symbolic name for the address of the instruction.

    Comment textual description of what is the instructionbeing used for.

    4Dr. Bassel SoudanProcessors & Assembly Language

    Example Instructions

    ADD AX, BX ; Add variable X to Y

    The Opcode for this instruction is ADD.

    The sources for the data are the AX and BX registers.

    The destination for the result is AX.

    The meaning of the instruction is:

    Add the value in register BX to the value in register AX.

    The comment indicates that register BX has the value ofvariable X and it is being added to variable Y which is in AX.

    Opcode Operands Comment

  • 8/7/2019 Ch03 Addressing Modes

    3/27

    5Dr. Bassel SoudanProcessors & Assembly Language

    Example Instructions

    START: MOV AX, BX ; Keep a copy of X

    An instruction may be preceded by a label. A label is a symbolic name for the address of this

    instruction.

    It allows other parts of the program to refer to this pointwithout having to know the exact address.

    Extremely useful for loopsThis instruction copies the contents of BX to AX.

    The comment indicates that this is being done for safekeeping.

    Label

    Opcode

    Operands Comment

    6Dr. Bassel SoudanProcessors & Assembly Language

    The 8088/8086 Instruction Set

    The 8088 and 8086 can implement 117 differentinstructions.

    These instructions are grouped into six major

    categories:Data Transfer Instructions

    Arithmetic Instructions

    Logic Instructions

    String Operation Instructions

    Control Transfer Instructions

    Processor Control Instructions.

  • 8/7/2019 Ch03 Addressing Modes

    4/27

    7Dr. Bassel SoudanProcessors & Assembly Language

    The 8088/8086 Addressing Modes

    Each instruction may operate on data in differentlocations.

    For example, an instruction may operate on data that exists

    in a register or in a memory location. The method of specifying the location of the data is calledthe addressing mode of the instruction.

    The 8088 and 8086 instructions can handle three types ofdata:

    Data stored in registers Register Operands

    Data given as part of the instruction

    Immediate Operands

    Data stored in memory

    Memory Operands

    8Dr. Bassel SoudanProcessors & Assembly Language

    The MOV Instruction

    To assist in explaining the different addressingmodes, we will use the MOV instruction.

    The MOV instruction allows copying data from thesource to a destination.

    A very versatile instruction that allows operatingon many data types and on data in basically allpossible locations.

    None

    Flags affected

    D SMOV D, SCopyMOV

    operationFormatMeaningMnemonic

  • 8/7/2019 Ch03 Addressing Modes

    5/27

    9Dr. Bassel SoudanProcessors & Assembly Language

    Register Operand Addressing

    Most of the operations are done on values storedin the internal registers of the microprocessor.

    Therefore, almost every assembly languageinstruction will need a way to access values inregisters.

    Values in registers are accessed by specifyingthe registers name in the operands section of aninstruction.

    Some instructions operate only on data in

    registers while others combine data in registerswith other types of data.

    Instructions that operate only on data in registers arecalled register addressing mode instruction.

    10Dr. Bassel SoudanProcessors & Assembly Language

    Register Addressing Mode

    All sources of the instruction are registers. The destination of the result is also a register.

    All user accessible registers can be specified assource or destination.

    Only the Data Registers can be accessed as byte(8 bits) or word (16 bits).

    All other registers can only be accessed as words(16 bits).

    Operand sizes must match.

  • 8/7/2019 Ch03 Addressing Modes

    6/27

    11Dr. Bassel SoudanProcessors & Assembly Language

    Register Mode Example

    xx02001

    xx02000

    Next Instruction01102

    CA01101

    MOV DX, CS8C01100

    InstructionMemory

    Content

    Address

    Main Memory0100 IP

    0100 CS0200 DS

    ES

    SS

    AX

    BX

    CX

    xxxx DX

    SP

    BP

    SI

    DI

    MPU

    12Dr. Bassel SoudanProcessors & Assembly Language

    Register Mode Example

    xx02001

    xx02000

    Next Instruction01102

    CA01101

    MOV DX, CS8C01100

    InstructionMemory

    Content

    Address

    Main MemoryIP

    CS

    DS

    ESSS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

  • 8/7/2019 Ch03 Addressing Modes

    7/27

    13Dr. Bassel SoudanProcessors & Assembly Language

    Register Mode Exception

    Some instruction use registers implicitly.For example, multiplication results always go to

    registers AX and DX. Can not change thiscombination.

    Reason: To save machine languagecombinations.

    Disadvantage: programmer/compiler restricted.

    Some of these were removed in later versions ofthe x86 architecture.

    14Dr. Bassel SoudanProcessors & Assembly Language

    Immediate Addressing Mode

    MOV AX, 15H

    One of the operands is available immediately as

    part of the instruction.The immediate operand is constant data.

    The data is fixed at the time the program is written andwill not change from execution to execution.

    Used to implement high level language initializationstatements like: a = 21;

    Immediate should match size of other operand.

    Opcode Immediate operand

  • 8/7/2019 Ch03 Addressing Modes

    8/27

    15Dr. Bassel SoudanProcessors & Assembly Language

    Immediate Mode Example

    Next Instruction01002

    1501001

    MOV AH, 15HB001000

    InstructionMemory

    Content

    Address

    Main Memory0000 IP

    0100 CS

    DS

    ES

    SS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

    xx

    16Dr. Bassel SoudanProcessors & Assembly Language

    Immediate Mode Example

    Next Instruction01002

    1501001

    MOV AH, 15HB001000

    InstructionMemory

    Content

    Address

    Main MemoryIP

    CS

    DS

    ESSS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

  • 8/7/2019 Ch03 Addressing Modes

    9/27

    17Dr. Bassel SoudanProcessors & Assembly Language

    Immediate Mode Example

    Write a set of instructions to store the value1245H into register CX.

    18Dr. Bassel SoudanProcessors & Assembly Language

    Addressing Operands in Memory

    There are five modes for addressing operandsstored in memory:

    1. Direct Addressing

    2. Register Indirect Addressing3. Based Addressing

    4. Indexed Addressing

    5. Based-indexed Addressing

  • 8/7/2019 Ch03 Addressing Modes

    10/27

    19Dr. Bassel SoudanProcessors & Assembly Language

    Addressing Operands in Memory

    To reference an operand in memory, the8088/8086 must calculate the physical address

    (PA) for the operand and then initiate a read orwrite operations of this storage location

    PA is computed from a Segment Base Address(SBA) and an offset.

    The different memory addressing modes differ inhow you specify the SBA and the offset.

    20Dr. Bassel SoudanProcessors & Assembly Language

    Direct Addressing Mode

    MOV AX, [1234H]

    The offset is a constant determined at program

    time and is given explicitly in the instruction. The brackets around the number tell the assembler that

    this is a direct mode instruction and not an immediatemode.

    The offset is taken to be from the start of the DataSegment.

    Therefore, the PA addressed in this instruction isDS:1234H

  • 8/7/2019 Ch03 Addressing Modes

    11/27

    21Dr. Bassel SoudanProcessors & Assembly Language

    Direct Mode Example

    xx02001

    FE03235

    xx01004

    ED03234

    xx02000

    1201003

    3401002

    0E01001MOV CX, [1234H]8B01000

    InstructionMemory

    Content

    Address

    Main Memory

    0000 IP

    0100 CS0200 DS

    ES

    SS

    AX

    BX

    xxxx CX

    DX

    SP

    BP

    SI

    DI

    MPU

    22Dr. Bassel SoudanProcessors & Assembly Language

    Direct Mode Example

    02001

    03235

    xx01004

    03234

    02000

    1201003

    3401002

    0E01001

    MOV CX, [1234H]8B01000

    InstructionMemory

    Content

    Address

    Main Memory

    IP

    CS

    DS

    ESSS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

  • 8/7/2019 Ch03 Addressing Modes

    12/27

    23Dr. Bassel SoudanProcessors & Assembly Language

    Register Indirect Addressing Mode

    MOV AX, [SI]

    The offset is taken from a register.The offset is no longer fixed. It is now dynamic.

    Only a specific set of registers can be used to holdthe offset:

    BX, BP, SI, and DI.

    The offset is taken from the start of the Stack Segment ifBP is used. Otherwise its the Data Segment.

    Useful for implementing pointer operations in highlevel languages:

    a = *b;

    24Dr. Bassel SoudanProcessors & Assembly Language

    Register Indirect Mode Example

    MOV AX, [SI]8B01000

    0401001

    xx01002

    xx01003

    xx02001

    FE03235

    xx01004

    ED03234

    xx02000

    InstructionMemory

    Content

    Address

    Main Memory

    0000 IP

    0100 CS0200 DS

    ESSS

    xxxx AX

    BX

    CX

    DX

    SP

    BP

    1234H SI

    DI

    MPU

  • 8/7/2019 Ch03 Addressing Modes

    13/27

    25Dr. Bassel SoudanProcessors & Assembly Language

    Register Indirect Mode Example

    MOV AX, [SI]8B010000401001

    xx01002

    xx01003

    xx02001

    FE03235

    xx01004

    ED03234

    xx02000

    InstructionMemory

    Content

    Address

    Main Memory

    IP

    0100 CS0200 DS

    ES

    SS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

    26Dr. Bassel SoudanProcessors & Assembly Language

    Based Addressing Mode

    The offset is generated by adding either a director indirect displacement to a base register.

    Allows for accessing elements of an array. To implement a[6] in a high level language, a base

    register is used to hold the address of a[0] and a directoffset (6) is used to reach the actual element.

    To implement a[i], a base register is used to hold theaddress of a[0] and an indirect displacement is used tospecify the i.

    To access the next element in the array, the registerholding the indirect displacement is simply incremented.

  • 8/7/2019 Ch03 Addressing Modes

    14/27

    27Dr. Bassel SoudanProcessors & Assembly Language

    Based Addressing Mode

    MOV [BX]+1234H, AL

    The offset is generated by adding either a director indirect displacement to a base register.

    Only the base register BX or base pointer BP canbe used as the base.

    The displacement can be:

    A 16 bit direct displacement.

    An indirect displacement from one of the 4 registers BX,

    BP, SI, and DI. The offset is taken from the start of the Stack Segment if

    BP is used. Otherwise its the Data Segment.

    28Dr. Bassel SoudanProcessors & Assembly Language

    Based Addressing Mode Example 1

    MOV [BX]+1234H, AL8801000

    8701001

    3401002

    1201003

    xx02001

    xx03235

    xx01004

    xx03234

    xx02000

    InstructionMemory

    Content

    Address

    Main Memory

    0000 IP

    0100 CS0200 DS

    ESSS

    BE AX

    1000 BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

    ED

  • 8/7/2019 Ch03 Addressing Modes

    15/27

    29Dr. Bassel SoudanProcessors & Assembly Language

    Based Addressing Mode Example 1

    MOV [BX]+1234H, AL88010008701001

    3401002

    1201003

    02001

    03235

    xx01004

    03234

    02000

    InstructionMemory

    Content

    Address

    Main Memory

    IP

    0100 CS0200 DS

    ES

    SS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

    30Dr. Bassel SoudanProcessors & Assembly Language

    Based Addressing Mode Example 2

    MOV [BX]+[SI], AX8801000

    8701001

    3401002

    1201003

    xx02001

    xx03235

    xx01004

    xx03234

    xx02000

    InstructionMemory

    Content

    Address

    Main Memory

    0000 IP

    0100 CS0200 DS

    ESSS

    BEED AX

    1000 BX

    CX

    DX

    SP

    BP

    1234 SI

    DI

    MPU

  • 8/7/2019 Ch03 Addressing Modes

    16/27

    31Dr. Bassel SoudanProcessors & Assembly Language

    Based Addressing Mode Example 2

    MOV [BX]+[SI], AX88010008701001

    3401002

    1201003

    02001

    03235

    xx01004

    03234

    02000

    InstructionMemory

    Content

    Address

    Main Memory

    0000 IP

    0100 CS0200 DS

    ES

    SS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

    32Dr. Bassel SoudanProcessors & Assembly Language

    Indexed Addressing Mode

    MOV AL, [SI]+1234H

    The offset is generated by either adding a direct

    or indirect displacement to an index register.Only the Source Index (SI) or Destination Index

    (DI) registers can be used.

    The displacement can be:

    A 16 bit direct displacement.

    An indirect displacement from one of BX, BP, SI, and DI.

    The offset is taken from the start of the Stack Segment ifBP is used. Otherwise its the Data Segment.

    Another method for accessing array elements.

  • 8/7/2019 Ch03 Addressing Modes

    17/27

    33Dr. Bassel SoudanProcessors & Assembly Language

    Indexed Addressing Mode Example 1

    MOV AL, [SI]+1234H8A010008401001

    3401002

    1201003

    xx02001

    xx05235

    xx01004

    BE05234

    xx02000

    InstructionMemory

    Content

    Address

    Main Memory

    0000 IP

    0100 CS0200 DS

    ES

    SS

    xx AX

    BX

    CX

    DX

    SP

    BP

    2000 SI

    DI

    MPU

    xx

    34Dr. Bassel SoudanProcessors & Assembly Language

    Indexed Addressing Mode Example 1

    MOV AL, [SI]+1234H8A01000

    8401001

    3401002

    1201003

    02001

    05235

    xx01004

    05234

    02000

    InstructionMemory

    Content

    Address

    Main Memory

    IP

    0100 CS0200 DS

    ESSS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

  • 8/7/2019 Ch03 Addressing Modes

    18/27

    35Dr. Bassel SoudanProcessors & Assembly Language

    Based-Indexed Addressing Mode

    MOV AH, [BX][SI]+1234H

    The offset is generated by adding Base registerand Indexed register and an optional immediatedisplacement.

    Useful for high level language array operationslike a[i+5].

    The base register would be set to the address of a[0].

    The index register would be used for i.

    The immediate displacement would be the constantvalue 5.

    The offset is taken from the start of the StackSegment if BP is used. Otherwise its the DataSegment.

    36Dr. Bassel SoudanProcessors & Assembly Language

    Based-Indexed Addressing Mode Example 1

    MOV AH, [BX][SI]+1234H8A01000

    A001001

    3401002

    1201003

    xx02001

    ED06235

    xx01004

    BE06234

    xx02000

    InstructionMemory

    Content

    Address

    Main Memory

    0000 IP

    0100 CS0200 DS

    ESSS

    xx AX

    1000 BX

    CX

    DX

    SP

    BP

    2000 SI

    DI

    MPU

    xx

  • 8/7/2019 Ch03 Addressing Modes

    19/27

    37Dr. Bassel SoudanProcessors & Assembly Language

    Based-Indexed Addressing Mode Example 1

    MOV AH, [BX][SI]+1234H8A01000A001001

    3401002

    1201003

    02001

    06235

    xx01004

    06234

    02000

    InstructionMemory

    Content

    Address

    Main Memory

    0000 IP

    0100 CS0200 DS

    ES

    SS

    AX

    BX

    CX

    DX

    SP

    BP

    SI

    DI

    MPU

    38Dr. Bassel SoudanProcessors & Assembly Language

    Based-Indexed Addressing Examples

    Assume

    CS = 0000H

    BX = 0040H

    SI = 0002H

    ORG 0040HTBL: DB 9, 8, 7 ,6 ,5

    MOV AX, CS

    MOV DS, AX DS = 0000H

    MOV AL, [BX]+1

    PA = 00000 + 0040 + 1 = 00041 AL = 08

    MOV AL, [SI]+TBL

    PA = 00000 + 0002 + 0040 = 00042 AL = 07

    MOV AL, [BX+SI]+1PA = 00000 + 0040 + 0002 + 1 = 00043 AL = 06

    0500044

    0600043

    0700042

    0800041

    0900040

  • 8/7/2019 Ch03 Addressing Modes

    20/27

    39Dr. Bassel SoudanProcessors & Assembly Language

    Memory Addressing Summary

    Direct Addressing

    { }Direct:DSPA =

    40Dr. Bassel SoudanProcessors & Assembly Language

    Memory Addressing Summary

    Register Indirect Addressing

    =

    DI

    SI

    BP

    BX

    :DS

    SSPA

  • 8/7/2019 Ch03 Addressing Modes

    21/27

    41Dr. Bassel SoudanProcessors & Assembly Language

    Memory Addressing Summary

    Based Addressing

    +

    =ntdisplacemebit16

    ntdisplacemebit8

    BP

    BX:

    DS

    SSPA

    42Dr. Bassel SoudanProcessors & Assembly Language

    Memory Addressing Summary

    Indexed Addressing

    +

    =ntdisplacemebit16

    ntdisplacemebit8

    DI

    SI:DSPA

  • 8/7/2019 Ch03 Addressing Modes

    22/27

    43Dr. Bassel SoudanProcessors & Assembly Language

    Memory Addressing Summary

    Based Indexed Addressing

    +

    +

    =ntdisplacemebit16

    ntdisplacemebit8

    DI

    SI

    BP

    BX:

    DS

    SSPA

    44Dr. Bassel SoudanProcessors & Assembly Language

    The very versatile MOV instruction

    Seg-regMem16

    Seg-regReg16

    Mem16Seg-reg

    Reg16Seg-reg

    ImmediateMemory

    ImmediateRegister

    RegisterMemory

    MemoryRegister

    RegisterRegister

    MemoryAccumulator

    AccumulatorMemory

    SourceDestination

    None

    Flags affected

    D SMOV D, SCopyMOV

    operationFormatMeaningMnemonic

  • 8/7/2019 Ch03 Addressing Modes

    23/27

    45Dr. Bassel SoudanProcessors & Assembly Language

    Development of an Assembly Language Program

    An assembly language program passes throughseveral stages.

    Edit Time Writing of the source programSymbolic Assembly Language

    Output is prog.asm

    The program will contain symbolic names such aslabels, named constants, initialized memoryarrays, reserved memory areas, etc.

    Will use symbolic names for instructions.

    What appears to be the same instruction may translate todifferent machine codes when addressing mode info isincorporated.

    46Dr. Bassel SoudanProcessors & Assembly Language

    Development of an Assembly Language Program

    Assembly Time Translation of the symbolic assemblylanguage to the matching machine language.

    Labels, symbolic constants, etc. are replaced with theirequivalent values.

    Two output files: prog.lst Listing file

    Text file that shows the actual translation of the assemblylanguage to the matching machine language.

    prog.obj Object file

    Binary program file that will be executed by the processor.

    Link Time (Optional) combine several files together tocreate one executable program.

    Output file prog.exe

    Run Time Actual execution of the machine language onthe microprocessor.

  • 8/7/2019 Ch03 Addressing Modes

    24/27

    47Dr. Bassel SoudanProcessors & Assembly Language

    Assembler Directives

    Directives are commands given to the assembler to tell itwhat to do.

    They are not part of the program.

    The ORG directiveORG 1000H

    This directive tells the Assembler where in memory the firstinstruction should go.

    The location of all other instructions is determined based onthis.

    Every program needs to start with an ORG.

    It is possible to have multiple ORG directives.

    This will place different sections of the program in differentlocations in memory.

    48Dr. Bassel SoudanProcessors & Assembly Language

    Assembler Directives

    EQU directiveLEN EQU 32

    Used for defining named constants.

    Everywhere the assembler sees the sequenceLEN in the following program, it will replace it with32.

    This is done before instructions are translated to machinecode.

    Allows for quickly changing sizes of arrays or lists.

  • 8/7/2019 Ch03 Addressing Modes

    25/27

    49Dr. Bassel SoudanProcessors & Assembly Language

    Assembler Directives

    DB DirectiveMSG DB hello

    Define Byte reserve a sequence of bytes and set them tothe values indicated.

    The first byte will be reserved wherever this directiveappears relative to the ORG.

    Example:ORG 1000H

    MSG: DB helloSTART: MOV AX, MSG

    This program segment will reserve 5 memory locationsstarting at offset 1000H and fill them up with the ASCII codefor the characters h e l l o respectively.

    The instruction at the label START sets the AX register tothe address associated with the label MSG.

    The address of the first of the 5 bytes.

    50Dr. Bassel SoudanProcessors & Assembly Language

    Assembler Directives

    DW DirectiveMSG DW 4455H

    Define Word reserve a sequence of words and set them tothe values indicated.

    The first byte will be reserved wherever this directiveappears relative to the ORG.

    Example:ORG 1000H

    CONST: DW 4455H

    START: MOV AX, MSG

    This program segment will reserve the memory location atoffset 1000H and fill it with the value 55H and location at

    offset 1001H and fill it with the value 44H.

  • 8/7/2019 Ch03 Addressing Modes

    26/27

    51Dr. Bassel SoudanProcessors & Assembly Language

    Assembler Directives

    RESB DirectiveINPUT RESB 5

    Reserve a number of byte sized memorylocations.

    This directive simply reserves the locations. Itdoes not set their contents.

    Useful for reserving storage space for values thatwill be generated during program execution.

    Example usage:ORG 1000H

    INPUT: RESB 5START: MOV AX, INPUT

    MOV [AX], BL ; Save 8 bit value in reserved word

    52Dr. Bassel SoudanProcessors & Assembly Language

    Assembler Directives

    RESW DirectiveINPUT RESW 5

    Reserve a number of word sized memorylocations.

    This directive simply reserves the locations. Itdoes not set their contents.

    Useful for reserving storage space for values thatwill be generated during program execution.

    Example usage:ORG 1000H

    INPUT: RESW 5

    START: MOV AX, INPUT

    MOV [AX], BX ; Save 16 bit value in reserved word

  • 8/7/2019 Ch03 Addressing Modes

    27/27

    53Dr. Bassel SoudanProcessors & Assembly Language

    Operand Specification

    The assembler is smart enough to do some translationsand/or calculations.

    An operand may be specified as one of the following: A decimal value (-390 is an acceptable value)

    An ASCII code (a, 0)

    A symbolic name (LEN)

    A simple operation (LEN+2)

    The assembler will replace LEN with its value, add 2 to thatvalue and use the result in coding the equivalent machine

    instruction.

    LEN + 2 is not calculated during assembly not execution.

    The assembler will automatically translate all of these toHexadecimal before coding the machine instructions.