Integer Arithmetic

Shift and Rotate Instructions

  • Logical shifts fill the created bit position with zero.
  • Arithmetic shifts fill the newly created bit position with a copy of the number’s sign bit.

SHL Instruction

  • The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with 0.
  • Operand types for SHL:
    • SHL reg,imm8
    • SHL mem,imm8
    • SHL reg,CL
    • SHL mem,CL
  • Same operand types apply to all shift and rotate instructions.
  • Shifting left 1 bit multiplies a number by 2.
    • Example:
      assembly mov dl,5 shl dl,1 ; DL = 10
  • Shifting left n bits multiplies the operand by 2^n. For example, 5 * 2^2 = 20.
    • Example:
      assembly mov dl,5 shl dl,2 ; DL = 20

SHR Instruction

  • The SHR (shift right) instruction performs a logical right shift on the destination operand. The highest bit position is filled with a zero.
    • Example:
      assembly mov dl,80 shr dl,1 ; DL = 40 shr dl,2 ; DL = 10
  • Shifting right n bits divides the operand by 2^n.

SAL and SAR Instructions

  • SAL (shift arithmetic left) is identical to SHL.
  • SAR (shift arithmetic right) performs a right arithmetic shift on the destination operand. An arithmetic shift preserves the number's sign.
    • Example:
      assembly mov dl,-80 sar dl,1 ; DL = -40 sar dl,2 ; DL = -10

ROL Instruction

  • ROL (rotate left) shifts each bit to the left.
  • The highest bit is copied into both the Carry flag and into the lowest bit.
  • No bits are lost.
    • Example:
      assembly mov al,11110000b rol al,1 ; AL = 11100001b mov dl,3Fh rol dl,4 ; DL = F3h

ROR Instruction

  • ROR (rotate right) shifts each bit to the right.
  • The lowest bit is copied into both the Carry flag and into the highest bit.
  • No bits are lost.
    • Example:
      assembly mov al,11110000b ror al,1 ; AL = 01111000b mov dl,3Fh ror dl,4 ; DL = F3h

RCL Instruction

  • RCL (rotate carry left) shifts each bit to the left.
  • Copies the Carry flag to the least significant bit.
  • Copies the most significant bit to the Carry flag.
    • Example:
      assembly clc ; CF = 0 mov bl,88h ; CF,BL = 0 10001000b rcl bl,1 ; CF,BL = 1 00010000b rcl bl,1 ; CF,BL = 0 00100001b

RCR Instruction

  • RCR (rotate carry right) shifts each bit to the right.
  • Copies the Carry flag to the most significant bit.
  • Copies the least significant bit to the Carry flag.
    • Example:
      assembly stc ; CF = 1 mov ah,10h ; CF,AH = 1 00010000b rcr ah,1 ; CF,AH = 0 10001000b

SHLD Instruction

  • Shifts a destination operand a given number of bits to the left.
  • The bit positions opened up by the shift are filled by the most significant bits of the source operand.
  • The source operand is not affected.
  • Syntax: SHLD destination, source, count
  • Operand types:
    • SHLD reg16/32, reg16/32, imm8/CL
    • SHLD mem16/32, reg16/32, imm8/CL
    • Example:
      assembly mov al,11100000b mov bl,10011101b shld al,bl,1

SHRD Instruction

  • Shifts a destination operand a given number of bits to the right.
  • The bit positions opened up by the shift are filled by the least significant bits of the source operand.
  • The source operand is not affected.
  • Syntax: SHRD destination, source, count
  • Operand types:
    • SHRD reg16/32, reg16/32, imm8/CL
    • SHRD mem16/32, reg16/32, imm8/CL
    • Example:
      assembly mov al,11000001b mov bl,00011101b shrd al,bl,1

Shift and Rotate Applications

  • Shifting Multiple Doublewords

    • Programs sometimes need to shift all bits within an array, as one might when moving a bitmapped graphic image from one screen location to another.
    • The following shifts an array of 3 doublewords 1 bit to the right
      assembly .data ArraySize = 3 array DWORD ArraySize DUP(99999999h) ; 1001 1001... .code mov esi,0 shr array[esi + 8],1 ; high dword rcr array[esi + 4],1 ; middle dword, include Carry rcr array[esi],1 ; low dword, include Carry
  • Binary Multiplication

    • SHL performs unsigned multiplication efficiently when the multiplier is a power of 2.
    • Factor any binary number into powers of 2.
    • For example, to multiply EAX * 36, factor 36 into 32 + 4 and use the distributive property of multiplication to carry out the operation:
      EAX * 36 = EAX * (32 + 4) = (EAX * 32)+(EAX * 4)
      assembly mov eax,123 mov ebx,eax shl eax,5 ; mult by 2^5 shl ebx,2 ; mult by 2^2 add eax,ebx
  • Displaying Binary Bits

    • Algorithm: Shift MSB into the Carry flag; If CF = 1, append a