8086 instruction

Embed Size (px)

Citation preview

  • 7/29/2019 8086 instruction

    1/44

    for the example slides do not clickthe mouse to see the full

    Microprocessor 1

    Dr.Raed Al-qadi

    2009for the example slides do not click

    the mouse to see the full

  • 7/29/2019 8086 instruction

    2/44

    for the example slides do not clickthe mouse to see the full

    Instruction set for 8088/8086

    microprocessors

    1- Data transfer instructions.

    2-ALU instructions.

    3-Shift and rotate instructions.4-Program control and subroutines

    instructions.

    5-Miscallenous instructions.

    for the example slides do not clickthe mouse to see the full

  • 7/29/2019 8086 instruction

    3/44

    for the example slides do not clickthe mouse to see the full

    Data transfer instructions.

    In 8088/8086 the data transfer or data movementinstructions are :

    1) MOV

    2) IN/OUT

    3) LEA

    4) MOV OFFSET

    5) XLAT

    6) PUSH and POP

    7) String instructions

    We are going to investigate these instructions in details.

    for the example slides do not clickthe mouse to see the full

  • 7/29/2019 8086 instruction

    4/44

    for the example slides do not clickthe mouse to see the full

    MOV

    MOV instrucion has the following syntax:

    MOV dst,src ; just clone the src into the dst

    The following criteria's should be appliedwhereever MOV is used :

    1- dst and src must be the same size.

    2- dst must NOT be immediate value.

    3- dst and src cant be segment registers at thesame time.

    4- dst and src cant be memory at the same timeexcept with MOVS instruction ( discussed later).

  • 7/29/2019 8086 instruction

    5/44

    for the example slides do not clickthe mouse to see the full

    Example 1 :

    *Knowing that,

    DS=2042H.

    SI=500H,AX=214E,

    MOV [SI],AX ;

    DS=2042H DS=20420H

    SI=500

    20920H

    20921H

    21 4E

    AH AL

    4E

    21

  • 7/29/2019 8086 instruction

    6/44

    for the example slides do not clickthe mouse to see the full

    Dos and Don'ts

    Some special cases :

    Case 1 : MOV CS,anything; DONT ( if you transferto CS problems occur since CS contains the

    program code)Case 2 :MOV Segment, anything ; DO if anything

    does not equal segment register

    Case 3 : MOV segment,segemnt; DO NOT

    Case 4: MOV mem,mem; DON'T, memory tomemory is not allowed except with MOVS

  • 7/29/2019 8086 instruction

    7/44

    for the example slides do not clickthe mouse to see the full

    IN/OUT

    These two instructions are used to read from

    and to a device.

    IN instruction syntax:

    IN AL,port address; (port

  • 7/29/2019 8086 instruction

    8/44

    for the example slides do not clickthe mouse to see the full

    IN is used to read from input device in separateI/O mode.

    Example 2:

    IN AL,KEYBOARD; true

    But,

    ADD AL,KEYBOARD; false

    IN AL,60H; true, means read from

    Device at address 60H I/O addresses64Kbyte

    0000

    FFFF

    KEYBOARD

  • 7/29/2019 8086 instruction

    9/44

    for the example slides do not clickthe mouse to see the full

    Example 3 :

    The serial port is at 03F8H(>255), read a

    byte from the serial port.

    Since 03F8H>255

    MOV DX,03F8H

    IN AL,DX

    I/O addresses

    64Kbyte

    0000

    FFFF

    data03F8H

    DX=03F8H

    AL=data

  • 7/29/2019 8086 instruction

    10/44

    for the example slides do not clickthe mouse to see the full

    Example 4 :

    Define the DISPLAY device at 2430H and

    read a byte from it.

    DISPLAY EQU 2430H

    MOV DX,DISPLAY

    IN AL,DX I/O addresses64Kbyte

    0000

    FFFF

    DISPLAY2430

    DX=03F8H

    AL=data

  • 7/29/2019 8086 instruction

    11/44

    for the example slides do not clickthe mouse to see the full

    OUT instruction

    Syntax:

    OUT port address, AL; can be AX or AL

    OUT DX,AL ; Just holds for DX

    Example 5:

    Send char A to the printer at 03A0H.

    MOV DX,03A0H

    MOV AL,A

    OUT DX,AL

  • 7/29/2019 8086 instruction

    12/44

    for the example slides do not clickthe mouse to see the full

    LEA

    LEA stands for load effective address, it

    loads a 16 bit register with the offset

    address of the data specified by the

    operand.

    Syntax :

    LEA dst,src; dst stores the address of src

  • 7/29/2019 8086 instruction

    13/44

    for the example slides do not clickthe mouse to see the full

    Example 6:

    Fill the array AR1 with *

    .org 300

    .data AR1 DB 20 DUP(?)

    LEA SI,AR1

    MOV CX,20;

    LP : MOV [SI],* INC SI

    LOOP LP

    SI=add(AR1)

    AR1

    Cx=201918

    17161514131211

    109876543

    21*

    ***

    ******

    ****

    ***

    ***

  • 7/29/2019 8086 instruction

    14/44

    for the example slides do not clickthe mouse to see the full

    MOV OFFSET

    The same as LEA ,

    Syntax :

    MOV dst,OFFSET src; store the addressof src into dst.

  • 7/29/2019 8086 instruction

    15/44

    for the example slides do not clickthe mouse to see the full

    XLAT

    XLAT called translate instruction, the

    operation ofXLAT is changing the value of

    AL with the memory pointed to by (AL+BX)

    Mem[AL+BX]AL

    AL=data

    AL=dddd

    BX=mmmm

    datadddd+mmmm

  • 7/29/2019 8086 instruction

    16/44

    for the example slides do not clickthe mouse to see the full

    More clearly the XLAT instruction converts

    the content of register AL, using a table. Its

    beginning address is contained in registerBX. The content of register AL is interpreted

    as a relative address in the table.

    XLAT is the only instruction that adds 8 bitnumber to 16 bit number!

    XLAT never Affects the flags..

  • 7/29/2019 8086 instruction

    17/44

    for the example slides do not clickthe mouse to see the full

    Example 7:

    Suppose that a seven segment LED display

    lookup table is stored in memory at

    address TABLE, now XLAT can use this

    table to translate the BCD number in ALto a seven segment code also in AL.

    Let TABLE =1000H,DS=1000H,

  • 7/29/2019 8086 instruction

    18/44

    for the example slides do not clickthe mouse to see the full

    First , obtain the look up table.

    gfedcbax

    0

    x

    1

    x

    2

    x

    3

    01111110000

    00001101000

    10110110100

    10011111100

    11011100010

    11011011010

    11111010110

    00001111110

    11111110001

    11011111001

    3FH0

    06H1

    5BH2

    4FH3

    66H4

    6DH5

    7DH6

    27H7

    7FH8

    6FH9

    Next see the code

  • 7/29/2019 8086 instruction

    19/44

    for the example slides do not clickthe mouse to see the full

    The code for the operation of converting is verysimple using XLAT

    TABLE DB3FH,06H,5BH,4FH,66H,6DH,7DH,27H,7FH,6FH

    MOV AL,SOME_BCD_VALUE

    MOV BX,OFFSET TABLE

    XLAT

    For example take 06H(6 BCD) for the initialvalue of AL,after translation AL contains 7DH,see the figures in the next slide.

  • 7/29/2019 8086 instruction

    20/44

    for the example slides do not clickthe mouse to see the full

    AL=4FH

    TABLE

    DB 3FH,06H,5BH

    DB4FH,66H,6DH DB 7DH,27H,7FH,6FH

    MOV AL,06H

    MOV BX,OFFSET TABLE

    XLAT

    3FH

    06H

    5BH

    4FH

    66H

    6DH

    7DH

    27H

    7FH

    6FH

    DS TABLE

    BX=address (TABLE)=1000H

    1000H

    AL=06H

    Temp=AL+BX=1006HAL=memory [temp]

    AL=4FH, which is the

    seven segment

    code of 06H

  • 7/29/2019 8086 instruction

    21/44

    for the example slides do not clickthe mouse to see the full

    Example 8:

    Write code to read the input key from port(60H) and output the value to a 7-segemntdisplay at 80H.

    KEYS EQU 60H; DISPLAY EQU 80H;

    TABLE DB3FH,06H,5BH,4FH,66H,6DH,7DH,27H,7FH,6FH

    LEA BX,TABLE;

    IN AL,KEYS; XLAT

    OUT DISPLAY,AL

  • 7/29/2019 8086 instruction

    22/44

    for the example slides do not clickthe mouse to see the full

    Stack instructions

    PUSH and POP

    Both are important to store and retrieve datafrom the LIFO structure (stack),

    PUSH instruction :

    In 8088 and 8086 PUSH always transfer 2bytes of data to the stack.

    It has the following syntax :

    PUSH src;src could be any 2 byte register or memory

    location or immediate value

  • 7/29/2019 8086 instruction

    23/44

    for the example slides do not clickthe mouse to see the full

    How does PUSH executes?

    Simply when we push 2 bytes of data to thestack, the most significant byte is stored in

    stack segment pointed to by SP-1, and the least

    significant to location pointed by SP-2,see thefollowing example,

    Example 9 :let AX=31F2H,SP=100H,SS=2000H;

    CX=FFFFH ,BX=413AH

  • 7/29/2019 8086 instruction

    24/44

    for the example slides do not clickthe mouse to see the full

    SP=FE

    PUSH AX

    SS=2000H

    SP=100

    31 F2

    AX31

    F2

  • 7/29/2019 8086 instruction

    25/44

    for the example slides do not clickthe mouse to see the full

    Example 10:

    Assume SP=100H,SS=2000H

    AX=31F2H,BX=413AH,CX=FFFFH.

    We will see the changes in the stack after

    executing the following sentences :

    PUSH AX

    PUSH BX

    POP CX

    POP AX

  • 7/29/2019 8086 instruction

    26/44

    for the example slides do not clickthe mouse to see the full

    SP=FCHSP=FEH

    SS=2000H

    SP=100H

    PUSH AX

    PUSH BX

    POP CX

    31 F2

    AH AL

    41 3A

    BH BL

    31F2413A

    CH CL

    3A41FF FF

  • 7/29/2019 8086 instruction

    27/44

    for the example slides do not clickthe mouse to see the full

    Example 11:

    Write code to reverse string using stackoperations.

    STR DB HELLO,00H ;

    REVSTR DB 256 DUP(?)

    MOV CX,0; count number of bytes

    LEA SI,STR;

    LEA DI,REVSTR

  • 7/29/2019 8086 instruction

    28/44

    for the example slides do not clickthe mouse to see the full

    NEXT-CHAR :

    MOV AL,[SI]

    CMP AL,00 JE NEXT

    PUSH AX ;only care about AL

    INC CX; JMP NEXT-CHAR

  • 7/29/2019 8086 instruction

    29/44

    for the example slides do not clickthe mouse to see the full

    NEXT:

    CMP CX,00; check if cx =0 or the string is null

    JE DONE-REV

    POP AX MOV [DI],AL

    INC DI

    DEC CX;

    JMP NEXT

    DONE-REV: MOV [DI],00; to terminate with null

  • 7/29/2019 8086 instruction

    30/44

    for the example slides do not clickthe mouse to see the full

    String Instructions

    String instructions

    MOVS LODS STOS

    STOSB STOSWLODSB LODSWMOVSB MOVSW

    B stands for byte, and W stands for word

  • 7/29/2019 8086 instruction

    31/44

    for the example slides do not clickthe mouse to see the full

    MOVS*MOVSB : how does it execute?

    Copy byte at DS:[SI] to ES:[DI]. Update SI and DI.Note that MOVS instruction deals with SI in thedata segment and DI in the extra segment. Theextra segment cant be overridden here).

    DS:[SI] - ES:[DI]

    if DF = 0 then (DF is D flag)SI = SI + 1

    DI = DI + 1

    elseSI = SI - 1

    DI = DI - 1

  • 7/29/2019 8086 instruction

    32/44

    for the example slides do not clickthe mouse to see the full

    Using REP instruction with string

    instructions MOV CX,100

    REP MOVSB

    How does the above code executes?

    IF (Dflag==0)

    {

    Memory[DI]----memory[SI]

    SI++;DI++;

    }

    else(If D flag==1)

    {

    SI--; DI-;CX--

    }

    IF(CX=!=0) repeat the instruction, other wise then next one

    CLD clears the D flag (D=0)

    STD sets the Dflag to 1(D=1)

    D=1 we decrement SI,DI

    D=0 we increment DI,SI

  • 7/29/2019 8086 instruction

    33/44

    for the example slides do not clickthe mouse to see the full

    CLD

    MOV CX,100REP MOVSB ; is same as:

    MOV CX,100;

    LP:MOV AL,[SI]

    MOV ES:[DI],AL

    INC SI

    INC DI

    LOOP LP

  • 7/29/2019 8086 instruction

    34/44

    for the example slides do not clickthe mouse to see the full

    MOVSW *MOVSW : how does it execute?

    Copy word at DS:[SI] to ES:[DI]. Update SI and DI.The same as MOVSB except we move a word notabyte, see what happens to SI and DI.

    DS:[SI] - ES:[DI]

    if DF = 0 thenSI = SI + 2

    DI = DI + 2

    elseSI = SI - 2

    DI = DI - 2

  • 7/29/2019 8086 instruction

    35/44

  • 7/29/2019 8086 instruction

    36/44

    for the example slides do not clickthe mouse to see the full

    Example 13:

    Copy 500 words from physical address

    0800:200 to physical address

    4000:0300,use string instructions.

    0800 is the segment address and the 200 is the offset by contrast

    4000 is the segment address and 0300 is its offset

    So, we begin by assigning the segments

    addresses to DS and ES and the offsets

    to SI and DI

  • 7/29/2019 8086 instruction

    37/44

    for the example slides do not clickthe mouse to see the full

    MOV DS,0800H

    MOV ES,4000H

    MOV SI,0200H

    MOV DI,0300H

    CLD MOV CX,500

    REP MOVSW1000

    bytes

    1000bytes

    DS=0800H

    ES=4000H

    SI=200

    DI=300

    D=0

    D

    A

    T

    A

  • 7/29/2019 8086 instruction

    38/44

    for the example slides do not clickthe mouse to see the full

    STOSB and STOSW

    ForSTOSB Store byte in AL into ES:[DI]. Update DI.

    ForSTOSW Store word in AX into ES:[DI]. Update DI.How STOSB executes?

    ES:[DI] = AL

    if DF = 0 thenDI = DI + 1

    elseDI = DI - 1 And STOSW executes the same as STOSB but DI is

    incremented or decremented by 2

  • 7/29/2019 8086 instruction

    39/44

    for the example slides do not clickthe mouse to see the full

    LODSB and LODSW

    ForLODSB this is how it is done

    AL = DS:[SI]

    if D = 0 thenSI = SI + 1

    else

    SI = SI 1And LODSW is the same except that SI is

    changed by 2

  • 7/29/2019 8086 instruction

    40/44

    for the example slides do not clickthe mouse to see the full

    Example 14:

    Write program to clear DOS-screen.

    MOV ES,E000; E000:800 is not the real addressof video memory but here is just to illustrate the

    idea MOV DI,800

    CLD

    MOV CX,25*80; since the screen is 25*80

    characters and it is done by assembler MOV AX,0020H; 00 for color and 20H for space

    REP STOSW

  • 7/29/2019 8086 instruction

    41/44

    for the example slides do not clickthe mouse to see the full

    The old screen is character screen,and the characters that appear

    on it are stored in the video memory location,for each character

    2 bytes are used one for the ASCCI and the other for the color

    HELLO

    H

    E

    L

    L

    O

    00 20H

    COLOR CHAR

    Black space

  • 7/29/2019 8086 instruction

    42/44

    for the example slides do not click

    the mouse to see the full

    Review of data movement

    instructions

    MOV operand1,operand2Copy operand2 to operand1

    The MOV instruction cannot:

    set the value of the CS and IP registers.

    copy value of one segment register to anothersegment register (should copy to generalregister first).

    copy immediate value to segment register

    (should copy to general register first).

    Transfer instructions never affect the flags

  • 7/29/2019 8086 instruction

    43/44

    for the example slides do not click

    the mouse to see the full

    LEA and MOV OFFSET do the same

    operation, they load the address of one

    memory location to a register.

    XLAT copies the memory location

    addressed by [AL+BX] to AL.

    PUSH and POP are used to store and to

    read from the stack, both cant deal with 8bits register, just use 16 bits.

  • 7/29/2019 8086 instruction

    44/44

    for the example slides do not click

    String instructions are :MOVSB,MOVSW,STOSB,STOSW,LODSB,LODSW.

    The are used for moving or stroing orloading a whole byte or a whole word at atime, they shrink the code to minimal sizeat most problems

    Only MOVS can move data from memoryto memory locations