INSTRUCTION SET AND ADDRESSING MODES - DOC

Document Sample
INSTRUCTION SET AND ADDRESSING MODES - DOC Powered By Docstoc
					HUMBER COLLEGE
8086-ADDRESSING MODES                                                                   PAGE -1
                 INSTRUCTION FORMAT AND ADDRESSING MODES

MACHINE INSTRUCTIONS

Machine instructions are the codes that tell the CPU what to do. The major types of instructions
in the 8086 instruction set are:
              Data Movement instructions
              Arithmetic instructions
              Logic instructions
              Program Control and loop instructions
              CPU control and flag instructions
              String instructions

       General Instruction Format
       The general instruction format is:

               [label:] instruction [operand(s)] [; comments]

       The square brackets indicate optional items. Depending on the specific instruction there
       may be 0, 1, or 2 operands.

       Instructions with no operands
       These are instructions that do not use data or ones for which the operand is implied.
       E.g.           NOP ;no operation
                      CLC ;Clear Carry Flag
                      RET ;return from subroutine

       Instructions with one operand
       E.g.           PUSH BX                ;put contents of BX on stack
                      CALL ADDLIST           ;call subroutine ADDLIST
                      INC memory             ;increment
                      JNE SAME               ;jump if not equal

       Instructions with two operands
       The order of the operands is always: destination, source. If the two operands are used by
       the operation the result is placed in the first operand (the destination). The 8086 CPU
       does not support two memory address operations therefore only one of the operands can
       be a memory address the other a register or immediate data. The only exception is with
       the special string instructions.

       E.g.,          ADD     AX, BX         ; AX = (AX)+(BX)
                      MOV     BP, SP         ; BP = (SP)
HUMBER COLLEGE
8086-ADDRESSING MODES                                                                     PAGE -2
ADDRESSING MODES

While the instruction informs the CPU what to do, the operands specify the data to use. Usually
we specify the address of the data rather than the actual data. The address is called the effective
address (EA) or offset. It is the offset into the appropriate segment.

The different ways of specifying the operands are called addressing modes. These different
addressing modes give a lot of power to assembly programs and provide support to many
highlevel language features. The names of the different modes are not important but you should
understand how they work. The assembler will usually accept a number of different forms of the
addressing mode. The forms shown in Figure 5-1 are the ones used by DEBUG when it
unassembles code.

Remember that CPU is not capable of memory-to-memory operations. Therefore, you must use
memory-to-register and register-to-memory operations when required.

       Immediate
       The data is a constant given immediately as part of the instruction.
       E.g.
              VALUE                        EQU 9
              CHARS_ON_SCREEN              EQU 80*25

                       MOV       CX,    CHARS_ON_SCREEN
                       MOV       AH,    9
                       ADD       AH,    4
                       SUB       AH,    VALUE

       It is much better style to define names for constants rather than using "magic numbers" in
       your programs.

       NOTE: The CPU does not allow the use of the immediate mode with segment registers.
       For example, MOV DS, 1000 is an illegal instruction.

       Register
       The operand is the specified register.
       E.g.          MOV         BX, SI ;move contents of SI into BX
                     ADD         CL, DH
                     SUB         SI, DI

       Remember the format is: instruction destination, source.

       Direct
       The address of the data to be used is coded directly into the instruction. EA = constant

       Assuming that the following data has been declared in the data segment:

               SOME_WORDS          DW      345,?
HUMBER COLLEGE
8086-ADDRESSING MODES                                                                    PAGE -3
              SOME_BYTES          DB      02FH,"a",0

      The following instructions all use direct addressing:

                      MOV       AX, SOME_WORDS
                      MOV       SOME_WORDS+2, BX
                      CMP       DL, SOME_BYTES+1
                      SUB       BX, [100]


ADDRESSING MODE                        POSSIBLE ARRANGEMENTS

      Direct                           [constant]
      Register                         register
      Immediate                        constant
      Indirect                         [BX]
                                       [BP]
                                       [SI]
                                       [DI]
      Based                            [BX+n]
                                       [BP+n]
      Indexed                          [SI+n]
                                       [DI+n]
      Based Indexed                    [BX+SI]
                                       [BX+DI]
                                       [BP+SI]
                                       [BP+DI]
      Based Indexed                    [BX+SI+n]
      with Displacement                [BX+DI+n]
                                       [BP+SI+n]
                                       [BP+DI+n]

       String        automatically use SI and DI
Figure 5-1. Addressing Modes. (The arrangements shown are those used by DEBUG.)

      Indirect
      The register specified holds the offset of (points to) the memory location to be used. The
      square brackets ([ ]) indicate indirection, i.e., that the contents are used as a pointer to
      data. Only BX, BP, SI, DI can be used. This mode is often used to access one-
      dimensional arrays or tables of values. EA = (reg)

      E.g.
                      MOV    AX, [SI]        ;move content of memory location
                                             ;SI is pointing to into AX
                      ADD    AX, [BX]
                      MOV    [DI], AX
HUMBER COLLEGE
8086-ADDRESSING MODES                                                                 PAGE -4
    Because the CPU is not capable of memory-to-memory operations the following
    instructions are illegal:

                   MOV      [SI], [DI]            ;illegal
                   ADD      [BX], [SI]            ;illegal

    Based or Indexed
    Based or indexed modes are the same as indirect except that a constant displacement is
    added to the contents of the indirect register to determine the offset of the memory
    location to be used. Only BX, BP, SI, or DI can be used. The assembler will accept a
    number of different formats. EA = (reg) + displacement

    NAME_FIELD        EQU      12

                    MOV AX, [BX+12]
                    MOV AX, [BX]+12
                    MOV AX, 12[BX]
                    MOV AX, [BX].12
                    MOV AX, 4[BX]+8
                    MOV AX, [BX].NAME_FIELD
    All of these instructions do the same thing.

    This mode is often used to access one-dimensional arrays or tables of values. For
    example, the displacement could be used to point to the start of the array and the register
    as the offset into the array. For example, the third byte in an array called LIST could be
    accessed with:

                   MOV SI, 3
                   MOV AX, LIST[SI]

    Base Indexed
    The contents of one base and one index register are combined to determine the offset of
    the memory location to be used. EA = (base reg.) + (index reg)

                   MOV      AX, [BX+SI]
                   MOV      [BP][SI], CL

    Based Indexed With Displacement
    The contents of one base and one index register are combined with a constant
    displacement to determine the offset of the memory location to be used. EA = (base reg.)
    + (index reg)

    NAME_FIELD              EQU     12

                   MOV      AX, [BX+SI+12]
                   MOV      AX, [BX][SI]+12
                   MOV      AX, 12[BX][SI]
HUMBER COLLEGE
8086-ADDRESSING MODES                                                                    PAGE -5
                      MOV AX, [BX]+12+[SI]
                      MOV AX, [BX][SI].NAME_FIELD
       All of these above instructions do the same thing.

       This mode can be used to access a two-dimensional array. For example, the displacement
       could be the start of the array in memory, the base register could point to the beginning of
       the row and the index register hold the offset of the column value we are interested in.


GETTING EFFECTIVE ADDRESSES
There are two ways of having a program load the effective address of an operand into a register -
using the OFFSET operation or the LEA (Load Effective Address) instruction
E.g.

               MOV SI, OFFSET message
               LEA SI, message

The first instruction uses the assembler OFFSET operation which will result in the offset of the
label being made into immediate data. The LEA instruction has the advantage being able to get
the effective address of operands specified using all of the addressing modes. E.g.

               LEA SI, [BX+6]


CREATING A PROGRAM

 1.    Define the problem and write the algorithm in pseudo-code
       This may have to be repeated a number of times with each version of the pseudo-code
       having more detail.
       You could use a flow chart to represent the program.

 2.    Define data structure
          Number, type and size of data items - unsigned, signed, BCD or a more complicated
          structure such as an array, etc.

           Will they be stored in memory or in registers? How will you use the registers?

 3.    Initialization
           Initialize registers e.g. DS
           Initialize variables

 4.    Choose the instructions and code the program.

 5.    Test and repeat steps 1 to 5 as needed.
HUMBER COLLEGE
8086-ADDRESSING MODES                                                               PAGE -6
EXAMPLE PROGRAMS

    Example 1
    Add two numbers (bytes).

      Pseudo-code
           Get first number
           Add second number
           Save result

      Data Definition
           The two numbers are bytes in memory and the result will      be a byte in memory.

      Initialization
             The DS will have to point to the segment the data is in. This is done in the
             template (TPEXE or TPCOM) files. The number variables should be initialized
             with test data.

      Coding
           NUM1   DB           5                 ;test data
           NUM2   DB           4                 ;   "
           RESULT DB           ?

           START:
                       MOV     AL, NUM1          ;Get first number
                       ADD     AL, NUM2          ;Add second number
                       MOV     RESULT, AL        ;Save result
           DONE:

    Comments: This program will add two bytes and save the result in a byte location. The
    result will be correct as long as the result is not larger than one byte.

    Example 2
    Add two bytes together allowing the result to be greater than a byte.

      Pseudo-code
           Get first number
           Add second number keeping carry generated
           Save result

      Data Definition
           The two numbers are bytes in memory and the result will be a word in memory.
           The AX register will be used to do the calculation.

      Initialization
             Do it the same as in example 1.
HUMBER COLLEGE
8086-ADDRESSING MODES                                                                     PAGE -7
   Coding
       NUM1   DB          5                  ;test data
       NUM2   DB          4                  ;   "
       RESULT DW          ?

       START:
                  MOV     AX, 0              ;Clear AX
                  MOV     AL, NUM1           ;Get first number
                  ADD     AL, NUM2           ;Add second number
                  ADC     AH, 0              ;Add carry to upper byte of answer
                  MOV     RESULT, AX         ;Save result
       DONE:

    Example 3
    Add a list of numbers. The list is a series of bytes and the first item in the list will be the
    count of numbers to be added followed by the numbers themselves.

      Pseudo-code
           Point to start of list
           Get count
           Sum=0
           For i=count to 0 by -1
                   sum=sum+1
           result = sum

      Data Definition
           The list consists of bytes. The result could be larger than a byte so we will use a
           word. AX will be used to keep a running total. CX will be used as the counter
           and SI as a pointer to the list.

            Coding
       LIST      DB           5,1,2,3,4,5             ;5 items and test data
       RESULT DW              ?

       START:
                    MOV       SI, OFFSET LIST ;point to list
                    MOV       CL, [SI]        ;Get count
                    INC       SI
                    XOR       AX, AX          ;Clear sum
       NEXT:
                ADD AL, [SI]                    ;Sum=sum+1
                ADC AH, 0
                INC SI
                DEC CL
                JNZ NEXT                        ;Repeat if count not zero
                MOV RESULT, AX                  ;result = sum
    Comments: This program does not check if the result becomes larger than a word.