Files 3 Handouts Lecture 8

Embed Size (px)

Citation preview

  • 8/13/2019 Files 3 Handouts Lecture 8

    1/15

    Lecture on Logical & Shift InstructionsLogical instructions: The 88/86 processors has instructions to perform bit by bit logic

    operation on the specified source and destination operands.

    Mnemonic Meaning Format Operation Flags Affected

    AND

    OR

    XOR

    NOT

    Logical AND

    Logical Inclusive OR

    Logical Exclusive OR

    LOGICAL NOT

    AND D,S

    OR D,S

    XOR D,S

    NOT D

    (S) (D) (D)

    (S)+(D) (D)

    (S) (D) (D) _

    (D)

    (D)

    OF, SF, ZF, PF, CFAF undefined

    OF, SF, ZF, PF, CFAF undefined

    OF, SF, ZF, PF, CFAF undefined

    None

    +

    Destination Source

    Register

    Register MemoryRegister Memory

    Accumulator

    Register

    MemoryRegister

    ImmediateImmediate

    Immediate

    Destination

    RegisterMemory

    Allowed operand

    for NOT Operation Allowed operands for AND,OR, and XOR operations

  • 8/13/2019 Files 3 Handouts Lecture 8

    2/15

    Logical Instructions

    AND Uses any addressing mode except memory-to-memory and

    segment registers

    Especially used to clear certain bits (masking)xxxx xxxx AND 0000 1111 = 0000 xxxx

    (clear the first four bits)

    Examples: AND BL, 0FHAND AL, [345H]

    OR Used to set certain bits

    xxxx xxxx OR 0000 1111 = xxxx 1111(Set the upper four bits)

  • 8/13/2019 Files 3 Handouts Lecture 8

    3/15

    XOR Used to invert certain bitsxxxx xxxx XOR 0000 1111 = xxxxxxxx

    -Example: Clear bits 0 and 1, set bits 6 and 7, invert bit 5 ofregister CL:

    AND CL, 1111 1100B ; clear bits 0 and 1OR CL, 1100 0000B; set bits 6 and 7XOR CL, 0010 0000B; invert bit 5.

  • 8/13/2019 Files 3 Handouts Lecture 8

    4/15

    Shift InstructionsMnemonic Meaning Format Operation Flags Affected

    SAL/SHL

    SHR

    SAR

    Shift arithmeticLeft/shiftLogical left

    Shift logicalright

    Shift arithmeticright

    SAL/SHL D, Count

    SHR D, Count

    SAR D, Count

    Shift the (D) left by the numberof bit positions equal to countand fill the vacated bits

    positions on the right with zeros

    Shift the (D) right by thenumber of bit positions equal tocount and fill the vacated bits

    positions on the left with zeros

    Shift the (D) right by thenumber of bit positions equal tocount and fill the vacated bits

    positions on the left with theoriginal most significant bit

    CF,PF,SF,ZFAF undefinedOF undefined if count 1

    CF,PF,SF,ZFAF undefinedOF undefined if count 1

    CF,PF,SF,ZFAF undefinedOF undefined if count 1

    Destination Count

    Register

    Register MemoryMemory

    1

    CL1CL

  • 8/13/2019 Files 3 Handouts Lecture 8

    5/15

    SHL

    SAL

    SHR

    CF

    0

    SAR

    0

    CF

    CF

    Sign bit

    0

    CF

    Target register or memory

  • 8/13/2019 Files 3 Handouts Lecture 8

    6/15

    Note that the amount of shift specified in the source operand can be definedExplicitly if it is one bit or should be stored in CL if more than 1.

    EX: SHL AX, 1causes the 16-bit register to be shifted 1-bit position to the leftWhere the vacated LSB is filled with zero and the bit shifted out of the MSBis saved in CF

    0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0xCF

    Register AX before instruction SHL AX, 1

    0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 00 0

    Shift arithmetic left SAL is exactly the same as SHL

  • 8/13/2019 Files 3 Handouts Lecture 8

    7/15

    EX: MOV CL, 2HSHR AX, CL

    The two MSBs are filled with zeros and the LSB is thrown away while theSecond LSB is saved in CF.

    0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 xCF

    0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 00 0

  • 8/13/2019 Files 3 Handouts Lecture 8

    8/15

    In Arithmetic shift to the right SAR the vacated bits at the left are filled withThe value of the original MSB of the operand. Thus the original sign of theNumber is maintained.

    Ex: Assume (CL)= 2 and (AX)= 091AH. Determine the new contents of AX And CF after the instruction SAR AX, CL is executed.

    0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0

    This operation is equivalent to division by powers of 2 as long as the bitsshifted out of the LSB are zeros.

    0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 1CF

  • 8/13/2019 Files 3 Handouts Lecture 8

    9/15

    Ex. Multiply AX by 10 using shift instructionsSHL AX, 1MOV BX, AXMOV CL,2

    SHL AX,CLADD AX, BX

    Ex. What is the result of SAR CL, 1if CL initially contains B6H?

    DBH

    Ex.What is the result of SHL AL, CL

    if AL contains 75H and CL contains 3?A8H

  • 8/13/2019 Files 3 Handouts Lecture 8

    10/15

    Rotate Instructions

    Mnemonic Meaning Format Operation Flags Affected

    ROL Rotate left ROL D, Count Rotate the (D) left by thenumber of bit positionsequal to Count. Each bitshifted out from the leftmost bit goes back into therightmost bit position.

    CFOF undefined if count 1

    ROR Rotate right ROR D, Count Rotate the (D) right by the

    number of bit positionsequal to Count. Each bitshifted out from therightmost bit goes back intothe leftmost bit position.

    CF

    OF undefined if count 1

    RCL Rotate leftthroughcarry

    RCL D, Count Same as ROL except carryis attached to (D) forrotation.

    CFOF undefined if count 1

    RCR Rotate right

    throughcarry

    RCR D, Count Same as ROR except carry

    is attached to (D) forrotation.

    CF

    OF undefined if count 1

  • 8/13/2019 Files 3 Handouts Lecture 8

    11/15

    Destination Count

    RegisterRegister Memory

    Memory

    1CL1

    CL Allowed Operands for rotate instruction

  • 8/13/2019 Files 3 Handouts Lecture 8

    12/15

    CF

    RCL

    Ex. What is the result of ROL BTRE PTR [SI], 1

    if SI is pointing to a memory location that contains 41H? (82H)

    CF

    ROL

    CFRCR

    RORCF

  • 8/13/2019 Files 3 Handouts Lecture 8

    13/15

    EX: Assume (AX) = 1234HWhat is the result of executing the instruction ROL AX, 1

    0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0

    0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0

    0

    0

    The original value of bit 15 which is 0 is rotated into CF and bit 0 of AX. All other bits have been rotated 1 bit position to the left.

    Rotate right ROR instruction operates the same way as ROL except thatData is rotated to the right instead of left.

  • 8/13/2019 Files 3 Handouts Lecture 8

    14/15

    In rotate through carry left RCL and rotate through carry right RCR the bitsrotate through the carry flag.

    Rotation caused by RCL instruction

    Rotation caused by RCR instruction

  • 8/13/2019 Files 3 Handouts Lecture 8

    15/15

    Example

    Write a program that counts the number of 1s in a byte and writes it into BL

    DATA1 DB 97 ; 61hSUB BL, BL ; clear BL to keep the number of 1sMOV DL, 8 ; Counter MOV AL, DATA1

    AGAIN: ROL AL,1 ; rotate left onceJNC NEXT ; check for 1

    INC BL ; if CF=1 then add one to count NEXT: DEC DL ; go through this 8 times

    JNZ AGAIN ; if not finished go back NOP