lec12 logical instructions

Upload: parth-shah

Post on 06-Apr-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Lec12 Logical Instructions

    1/29

  • 8/2/2019 Lec12 Logical Instructions

    2/29

    AND

    AND Destination, Source Logical ANDs each bit in the source with the

    corresponding bit in the destination (logical

    CF and OF both become zero

    PF, SF and ZF affected

    AF undefined

    AND uses any mode except memory to memory andsegment register addressing

    3-2

  • 8/2/2019 Lec12 Logical Instructions

    3/29

    AND AX,BX

    array0110SI 0020DS:[0130]

    AND array[SI],AL

    AND AX,[DI]

    3-3

  • 8/2/2019 Lec12 Logical Instructions

    4/29

    AND clears bits of a binary number(masking)

    g:xxxx xxxx (Unknown Number)

    0000 1111(Mask)

    0000 xxxx (Result)

    3-4

  • 8/2/2019 Lec12 Logical Instructions

    5/29

    An ASCII number can be converted to BCD byusing AND to mask off the leftmost four binarybit positions

    Eg:

    MOV BX,3236

    AND BX,0F0F

    3-5

  • 8/2/2019 Lec12 Logical Instructions

    6/29

    TEST TEST operand1,operand2

    Only Flags are changed

    Similar to CMP instruction

    (tests single bit unlike CMP)TEST BL,1b

    TEST BL,2b

    3-6

  • 8/2/2019 Lec12 Logical Instructions

    7/29

    OR

    OR Destination, Source ORs each bit in the source with the corresponding bit in the

    destination (Logical Addition)

    CF and OF both become zero

    PF, SF and ZF affected AF undefined

    AND uses any mode except memory to memory and segmentregister addressing

    3-7

  • 8/2/2019 Lec12 Logical Instructions

    8/29

    MASKING

    Eg:

    xxxx xxxx (Unknown Number)

    0000 1111(Mask)XXXX 1111(Result)

    3-8

  • 8/2/2019 Lec12 Logical Instructions

    9/29

    XOR

    XOR Destination, Source XORs each bit in the source with the corresponding bit inthe destination

    CF and OF both become zero

    PF, SF and ZF affected AF undefined

    AND uses any mode except memory to memory and segmentregister addressing

    3-9

  • 8/2/2019 Lec12 Logical Instructions

    10/29

    MASKINGEg:

    xxxx xxxx (Unknown Number)

    as

    MOV CX,EF45H

    XOR CX,1000H ; invert bit 12

    XOR CX,CX

    3-10

  • 8/2/2019 Lec12 Logical Instructions

    11/29

    NOT

    NOT Destination Complements each bit in the destination and stores theresult back into the destination

    No Flags Affected

    NOT AL

    NOT BYTEPTR[SI]

    3-11

  • 8/2/2019 Lec12 Logical Instructions

    12/29

    LEA (Data Movement Instruction)

    Loads a 16 -bit register with the offset address of thedata specified by the operand.

    .DATA

    .CODE

    LEA AX,LIST

  • 8/2/2019 Lec12 Logical Instructions

    13/29

    Why is LEA instruction when there is OFFSET ?

    LEA BX,LIST

    MOV BX,OFFSET LIST

    The MOV BX,OFFSET LIST instruction is actuallyassembled as a MOVE immediate instruction and is moreefficient.

    Microprocessor takes longer to execute the LEA BX,LISTinstruction than the MOV BX,OFFSET LIST

  • 8/2/2019 Lec12 Logical Instructions

    14/29

    LEA AX, [DI] LEA AX,LIST[DI]

    MOV DI,3412 ; DS:3412 =1234

    LEA BX,[DI]

    3-14

  • 8/2/2019 Lec12 Logical Instructions

    15/29

    Find the sum of elements in an array and store it inmemory location SUM

    .model TINY

    .dataARRAY DB 00H

    BACK:MOV AL,[BX+DI]ADD SUM,ALINC DIDEC CL

    3-15

    , , , , ,

    SUM DB 0.CODE.STARTUPMOV CL,6LEA BX,ARRAYXOR DI,DI

    JNZ BACK.EXITEND

  • 8/2/2019 Lec12 Logical Instructions

    16/29

  • 8/2/2019 Lec12 Logical Instructions

    17/29

    String: A series of data words (or bytes) thatreside in consecutive memory locations

    , ,

    LODS,STOS,MOVS,INS,OUTS

    3-17

  • 8/2/2019 Lec12 Logical Instructions

    18/29

    Direction Flag selects auto-increment or auto-decrement operation for SI and DI during stringinstructions

    CLD Clear the D Flag (Auto-increment mode)

    STD - Sets the D Flag(Auto-decrement mode)

    3-18

  • 8/2/2019 Lec12 Logical Instructions

    19/29

    MOVS/MOVSB/MOVSW Transfers a byte, word or double word from data segment

    addressed by SI to ExtraSegment location addressed by DI

    --pointers incremented as dictated by thedirection flag

    The offset of source byte or word in the datasegment must be in SI Register

    The offset of destination byte or word in theextra segment must be in DI Register

    No Flags Affected3-19

  • 8/2/2019 Lec12 Logical Instructions

    20/29

    MOVSB transfers byte from data segment toextra segment

    extra segment

    3-20

  • 8/2/2019 Lec12 Logical Instructions

    21/29

    a) if DF = 0 (CLD)ES:[DI] DS:[SI]

    DI DI + 1

    CLDMOVSB

    +

    (b) if DF = 1(STD)

    ES:[DI] DS:[SI]

    DI DI - 1

    SI SI - 1

    3-21

    STDMOVSB

  • 8/2/2019 Lec12 Logical Instructions

    22/29

    a) if DF = 0 (CLD)ES:[DI] DS:[SI]

    DI DI + 2

    CLDMOVSW

    +

    (b) if DF = 1(STD)

    ES:[DI] DS:[SI]

    DI DI - 2

    SI SI - 2

    3-22

    STDMOVSB

  • 8/2/2019 Lec12 Logical Instructions

    23/29

    For multiple-byte or multiple-word MOVes, the countto be in CX register

    CLD

    3-23

    REP MOVSBrep causes CX to dec by 1 no flags affectedMOVSB causes SI and DI to inc by 1 no flagsaffected

  • 8/2/2019 Lec12 Logical Instructions

    24/29

    .MODEL TINY

    .DATA

    COUNT DW 04H

    SRC_STR DB BITS

    DST_STR DB 4 DUP(?)

    .CODE

    Copy contents of DST_STR to SRC_STR

    .

    LEA SI,SRC_STR

    LEA DI,DST_STR

    MOV CX,COUNT

    CLD REP MOVSB

    3-24

    X1: MOV BL,[SI]

    MOV [DI],BLINC SIINC DIDEC CXJNZ X1

  • 8/2/2019 Lec12 Logical Instructions

    25/29

    The source operand (SI) located in the datasegment may be overridden to another segment

    3-25

    located in the extra segment

    Only instruction which allows memory to memoryoperation

  • 8/2/2019 Lec12 Logical Instructions

    26/29

    MOVS byte1,byte2

    ,

    3-26

  • 8/2/2019 Lec12 Logical Instructions

    27/29

    LODS

    Loads AL or AX with the data stored at the data segmentOffset address indexed by SI register

    3-27

    g , = =LODS affects no FLAGs

    LODSB,LODSW

  • 8/2/2019 Lec12 Logical Instructions

    28/29

    LODSW ;AX=DS:[SI], SI=SI+2 LODSB ;AL=DS:[SI],SI=SI+1

    MOV SI ,offset MPI LODS MPI ; AL=DS:[SI];

    ;SI=SI+1 ;if MPI is byte 3-28

  • 8/2/2019 Lec12 Logical Instructions

    29/29

    SI = SI 1

    LODSW ; AX = DS:[SI];

    SI = SI 2

    3-29