Microprocessor Based Systems Spring 2013 Department of Electrical Engineering University of Gujrat
2
AND, OR, and XOR Instructions AND destination, source OR destination, source XOR destination, source Effect on flags: SF, ZF, PF reflect the result AF is undefined CF, OF = 0 3
Rules • The destination must be a register or memory location. • The source may be a constant, register, or memory location. • The memory-to-memory operations are not allowed. 4
Use of AND, OR, and XOR Instructions • Selectively modify the bits in the destination • Construct a source bit pattern, mask • Only desired destination bits are modified
AND • can be used to clear specific destination bits while preserving the others. • A 0 mask bit clears the corresponding destination bit. • A 1 mask bit preserves the corresponding destination bit. 6
OR • can be used to set specific destination bits while preserving the others. • A 1 mask bit sets the corresponding destination bit. • A 0 mask bit preserves the corresponding destination bit. 7
XOR • can be used to complement specific destination bits while preserving the others. • A 1 mask bit complements the corresponding destination bit. • A 0 mask bit preserves the corresponding destination bit. 8
Clear the sign bit of AL while leaving the other bits unchanged. Use AND with 0111 1111b = 7Fh as the mask. Thus, AND AL, 7Fh 9
Set msb and lsb of AL while preserving the other bits. Use OR with 1000 0001b = 81h as the mask. Thus, OR AL, 81h 10
Change the sign bit of DX. Use XOR with a mask of 8000h. Thus, XOR DX, 8000h 11
Converting an ASCII Digit to a Number If the “5” key is pressed, AL gets 35h instead of 5. To get 5 in AL, we could do this: SUB AL, 30h Another method is to use AND to clear the high nibble (high four bits) of AL: AND AL, 0FH Because the codes “0” to “9” are 30h to 39h. 12
Converting a Lowercase Letter to Upper Case The ASCII codes of “a” to “z” range from 61h to 7Ah; the codes “A” to “Z” go from 41h to 5Ah. Thus for example, if DL contains the code of a lowercase letter, we could convert to upper case by executing SUB DL, 20h 13
Converting a Lowercase Letter to Upper Case Character Code Character Code a 0110 0001 A 0100 0001 b 0110 0010 B 0100 0010 . . . . . . . . . . . . z 0111 1010 Z 0101 1010 14
Converting a Lowercase Letter to Upper Case We need only clear bit 5 by using AND with 1101 1111b or 0DFh. So if the lowercase character to be converted is in DL, execute AND DL, 0DFh 15
Clearing a Register To clear AX, we could execute MOV AX, 0 SUB AX, AX Using the fact that 1 XOR 1 = 0 and 0 XOR 0 = 0, a third way is XOR AX, AX 16
Testing a Register for Zero OR CX, CX Because 1 OR 1 = 1 and 0 OR 0 = 0, it leaves the content of CX unchanged; however, it affects ZF and SF, and in particular if CX contains 0 then ZF = 1. So it can be used as an alternative to CMP CX, 0 17
NOT Instruction NOT destination 18
Complement the bits in AX. NOT AX 19
TEST Instruction TEST destination, source Effect on flags: SF, ZF, PF reflect the result AF is undefined CF, OF = 0 20
TEST Instruction • performs an AND operation but does not change the destination content. • The purpose of TEST is to set the status flags. 21
Examining Bits TEST destination, mask Because 1 AND b = b and 0 AND b = 0, the result will have 1’s in the tested bit positions if and only if the destination has 1’s in these positions; it will have 0’s elsewhere. If destination has 0’s in all the tested position, the result will be 0 and so ZF = 1. 22
Jump to label BELOW if AL contains an even number Even numbers have a 0 in bit 0. Thus, the mask is 0000 0001b = 1. TEST AL, 1 ; is AL even? JZ BELOW ; yes, go to BELOW 23
Shift and Rotate Instructions For a single shift or rotate, the form is Opcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contains N. In both cases, destination is an 8- or 16-bit register or memory location. 24
Shift Left SHL shifts the bits in the destination to the left. Effect on flags: SF, ZF, PF reflect the result AF is undefined CF = last bit shifted out OF = 1 if result changes sign on last shift 25
SHL and SAL 26
Multiplication by Left Shift A left shift on a binary number multiplies it by 2. Suppose that AL contains 5 = 00000101b. A left shift gives 00001010b = 10d, thus doubling its value. If AX is FFFFh (–1), then shifting three times will yield AX = FFF8h (–8). 27
Shift Arithmetic Left SAL is often used in instances where numeric multiplication is intended. Both instructions generate the same machine code. 28
Overflow The overflow flags are not reliable indicators for a multiple left shift because it is really a series of single shifts, and CF, OF only reflect the result of the last shift. 29
Overflow If BL contains 80h, CL contains 2and we execute SHL BL, CL, then CF = OF = 0 even though both signed and unsigned overflow occur. 30
Write some code to multiply the value of AX by 8. Assume that overflow will not occur. MOV CL, 3 ; number of shifts to do SAL AX, CL ; multiply by 8 31
Shift Right SHR performs right shifts on the destination operand. A 0 is shifted into the msb position, and the rightmost bit is shifted into CF. 32
Shift Arithmetic Right SAR operates like SHR, with one difference: the msb retains its original value. 33
SHR 34
SAR 35
Division by Right Shift For even numbers, a right shift divides the destination’s value by 2. For odd numbers, a right shift halves the destination’s value and round down to the nearest integer. If BL contains 00000101b = 5, then after a right shift BL will contain 00000010b = 2. 36
Signed and Unsigned Division If an unsigned interpretation is being given, SHR should be used. For a signed interpretation, SAR must be used, because it preserves the sign. 37
Rotate Left ROL shifts bits to the left. The msb is shifted into the rightmost bit. The CF also gets the bit shifted out of the msb. 38
Rotate Right ROR works just like ROL, except that the bits are rotated to the right. 39
ROL 40
ROR 41
Use ROL to count the number of 1 bits in BX, without changing BX. Put the answer in AX. XOR AX, AX ; AX counts bits MOV CX, 16 ; loop counter TOP: ROL BX, 1 ; CF = bit rotated out JNC NEXT ; 0 bit INC AX ; 1 bit, increment total NEXT: LOOP TOP ; loop until done 42
Use ROL to count the number of 1 bits in BX, without changing BX. Put the answer in AX. In this example, we used JNC (jump if no carry), which causes a jump if CF = 0. 43
Rotate Carry Left RCL shifts bits of the destination to the left. The msb is shifted into the CF, and the previous value of CF is shifted into the rightmost bit. 44
Rotate Carry Right RCR works just like RCL, except that the bits are rotated to the right. 45
RCL 46
RCR 47
An application: Reversing a Bit Pattern MOV CX, 8 ; number of operations to do REVERSE: SHL AL, 1 ; get a bit into CF RCR BL, 1 ; rotate it to BL LOOP REVERSE ; loop until done MOV AL, BL ; AL gets reversed pattern 48

Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift, and Rotate instructions)

  • 1.
    Microprocessor Based Systems Spring2013 Department of Electrical Engineering University of Gujrat
  • 2.
  • 3.
    AND, OR, andXOR Instructions AND destination, source OR destination, source XOR destination, source Effect on flags: SF, ZF, PF reflect the result AF is undefined CF, OF = 0 3
  • 4.
    Rules • The destinationmust be a register or memory location. • The source may be a constant, register, or memory location. • The memory-to-memory operations are not allowed. 4
  • 5.
    Use of AND,OR, and XOR Instructions • Selectively modify the bits in the destination • Construct a source bit pattern, mask • Only desired destination bits are modified
  • 6.
    AND • can beused to clear specific destination bits while preserving the others. • A 0 mask bit clears the corresponding destination bit. • A 1 mask bit preserves the corresponding destination bit. 6
  • 7.
    OR • can beused to set specific destination bits while preserving the others. • A 1 mask bit sets the corresponding destination bit. • A 0 mask bit preserves the corresponding destination bit. 7
  • 8.
    XOR • can beused to complement specific destination bits while preserving the others. • A 1 mask bit complements the corresponding destination bit. • A 0 mask bit preserves the corresponding destination bit. 8
  • 9.
    Clear the signbit of AL while leaving the other bits unchanged. Use AND with 0111 1111b = 7Fh as the mask. Thus, AND AL, 7Fh 9
  • 10.
    Set msb andlsb of AL while preserving the other bits. Use OR with 1000 0001b = 81h as the mask. Thus, OR AL, 81h 10
  • 11.
    Change the signbit of DX. Use XOR with a mask of 8000h. Thus, XOR DX, 8000h 11
  • 12.
    Converting an ASCIIDigit to a Number If the “5” key is pressed, AL gets 35h instead of 5. To get 5 in AL, we could do this: SUB AL, 30h Another method is to use AND to clear the high nibble (high four bits) of AL: AND AL, 0FH Because the codes “0” to “9” are 30h to 39h. 12
  • 13.
    Converting a Lowercase Letterto Upper Case The ASCII codes of “a” to “z” range from 61h to 7Ah; the codes “A” to “Z” go from 41h to 5Ah. Thus for example, if DL contains the code of a lowercase letter, we could convert to upper case by executing SUB DL, 20h 13
  • 14.
    Converting a Lowercase Letterto Upper Case Character Code Character Code a 0110 0001 A 0100 0001 b 0110 0010 B 0100 0010 . . . . . . . . . . . . z 0111 1010 Z 0101 1010 14
  • 15.
    Converting a Lowercase Letterto Upper Case We need only clear bit 5 by using AND with 1101 1111b or 0DFh. So if the lowercase character to be converted is in DL, execute AND DL, 0DFh 15
  • 16.
    Clearing a Register Toclear AX, we could execute MOV AX, 0 SUB AX, AX Using the fact that 1 XOR 1 = 0 and 0 XOR 0 = 0, a third way is XOR AX, AX 16
  • 17.
    Testing a Registerfor Zero OR CX, CX Because 1 OR 1 = 1 and 0 OR 0 = 0, it leaves the content of CX unchanged; however, it affects ZF and SF, and in particular if CX contains 0 then ZF = 1. So it can be used as an alternative to CMP CX, 0 17
  • 18.
  • 19.
    Complement the bitsin AX. NOT AX 19
  • 20.
    TEST Instruction TEST destination,source Effect on flags: SF, ZF, PF reflect the result AF is undefined CF, OF = 0 20
  • 21.
    TEST Instruction • performsan AND operation but does not change the destination content. • The purpose of TEST is to set the status flags. 21
  • 22.
    Examining Bits TEST destination,mask Because 1 AND b = b and 0 AND b = 0, the result will have 1’s in the tested bit positions if and only if the destination has 1’s in these positions; it will have 0’s elsewhere. If destination has 0’s in all the tested position, the result will be 0 and so ZF = 1. 22
  • 23.
    Jump to labelBELOW if AL contains an even number Even numbers have a 0 in bit 0. Thus, the mask is 0000 0001b = 1. TEST AL, 1 ; is AL even? JZ BELOW ; yes, go to BELOW 23
  • 24.
    Shift and RotateInstructions For a single shift or rotate, the form is Opcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contains N. In both cases, destination is an 8- or 16-bit register or memory location. 24
  • 25.
    Shift Left SHL shiftsthe bits in the destination to the left. Effect on flags: SF, ZF, PF reflect the result AF is undefined CF = last bit shifted out OF = 1 if result changes sign on last shift 25
  • 26.
  • 27.
    Multiplication by LeftShift A left shift on a binary number multiplies it by 2. Suppose that AL contains 5 = 00000101b. A left shift gives 00001010b = 10d, thus doubling its value. If AX is FFFFh (–1), then shifting three times will yield AX = FFF8h (–8). 27
  • 28.
    Shift Arithmetic Left SALis often used in instances where numeric multiplication is intended. Both instructions generate the same machine code. 28
  • 29.
    Overflow The overflow flagsare not reliable indicators for a multiple left shift because it is really a series of single shifts, and CF, OF only reflect the result of the last shift. 29
  • 30.
    Overflow If BL contains80h, CL contains 2and we execute SHL BL, CL, then CF = OF = 0 even though both signed and unsigned overflow occur. 30
  • 31.
    Write some codeto multiply the value of AX by 8. Assume that overflow will not occur. MOV CL, 3 ; number of shifts to do SAL AX, CL ; multiply by 8 31
  • 32.
    Shift Right SHR performsright shifts on the destination operand. A 0 is shifted into the msb position, and the rightmost bit is shifted into CF. 32
  • 33.
    Shift Arithmetic Right SARoperates like SHR, with one difference: the msb retains its original value. 33
  • 34.
  • 35.
  • 36.
    Division by RightShift For even numbers, a right shift divides the destination’s value by 2. For odd numbers, a right shift halves the destination’s value and round down to the nearest integer. If BL contains 00000101b = 5, then after a right shift BL will contain 00000010b = 2. 36
  • 37.
    Signed and UnsignedDivision If an unsigned interpretation is being given, SHR should be used. For a signed interpretation, SAR must be used, because it preserves the sign. 37
  • 38.
    Rotate Left ROL shiftsbits to the left. The msb is shifted into the rightmost bit. The CF also gets the bit shifted out of the msb. 38
  • 39.
    Rotate Right ROR worksjust like ROL, except that the bits are rotated to the right. 39
  • 40.
  • 41.
  • 42.
    Use ROL tocount the number of 1 bits in BX, without changing BX. Put the answer in AX. XOR AX, AX ; AX counts bits MOV CX, 16 ; loop counter TOP: ROL BX, 1 ; CF = bit rotated out JNC NEXT ; 0 bit INC AX ; 1 bit, increment total NEXT: LOOP TOP ; loop until done 42
  • 43.
    Use ROL tocount the number of 1 bits in BX, without changing BX. Put the answer in AX. In this example, we used JNC (jump if no carry), which causes a jump if CF = 0. 43
  • 44.
    Rotate Carry Left RCLshifts bits of the destination to the left. The msb is shifted into the CF, and the previous value of CF is shifted into the rightmost bit. 44
  • 45.
    Rotate Carry Right RCRworks just like RCL, except that the bits are rotated to the right. 45
  • 46.
  • 47.
  • 48.
    An application: Reversinga Bit Pattern MOV CX, 8 ; number of operations to do REVERSE: SHL AL, 1 ; get a bit into CF RCR BL, 1 ; rotate it to BL LOOP REVERSE ; loop until done MOV AL, BL ; AL gets reversed pattern 48