preload preload preload preload

Sunday, January 24, 2010

Assembly Language Part 7

Shift Instructions in Assembly

The shift and rotate instructions shift the bits of the operand by one or more bit positions. The shifting can be either left or right. The basic shift instructions are -

SHL (shift left)

The SHL instruction shifts the bit of the destination to the left. The format for calling the SHL is-

SHL destination,steps


Applying this the MSB is shifted to the carry flag and LSB is filled with 0 from right. The bits of the middle is shifted to 1 bit left when steps are 1


              CF  <-  [<-][<-][<-][<-][<-][<-][<-][<-]   <- 0
                           7     6    5    4    3    2    1    0

Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHL DH, CL



                           [1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ]  
                           7     6    5    4    3   2   1   0

after first shift

    CF [ 1 ]   <-   [0 ][0 ][0 ][1 ][0 ][1 ][0 ][ 0 ]  <- 0  
                           7     6    5    4    3   2   1   0

2nd shift

    CF [ 0 ]   <-   [0 ][ 0 ][ 1 ][ 0 ][ 1 ][ 0 ][ 0 ][0 ]  <- 0  
                           7    6     5    4    3     2    1    0

After 3rd and the final shift
    CF [ 0 ]   <-   [0 ][ 1 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 0 ]  <- 0  
                           7    6    5     4    3     2    1    0



SHR (shift right)

The SHR works like the reverse function of SHL. It shifts the bits of the destination operand to the right. The format is

SHR destination, steps

If we apply SHR the MSB is filled with 0 and the LSB is moved to CF and the bits between are mvoved to the same direction.

               0  ->  [->][->][->][->][->][->][->][->]   -> CF
                           7    6    5    4    3    2    1    0


Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHR DH, CLworks as follow -


                           [1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ]  
                           7     6    5    4    3   2   1   0

after first shift

               0  ->  [ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 1 ]  ->   [0] CF  
                           7     6    5    4    3    2     1    0

then the 2nd
               0  ->  [ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ]  ->   [1]CF  
                           7     6    5    4    3    2     1    0


Finally the 3rd

                0  ->  [ 0 ][0 ][0 ][1 ][0 ][0 ][0 ][ 1 ]  ->      [0]CF  
                           7    6   5    4   3   2    1   0


SAR (shift arithmetic right)

The only difference between SHR and SAR is that apllying SAR the MSB retains the original value.

SAR destination, steps


If we apply SAR the MSB is unchaged and it is also copied to the next bit to the right of MSB,  the LSB is moved to CF and the bits between are mvoved to the
same direction.


                         [^>][->][->][->][->][->][->][->]   -> CF
                           7      6    5    4    3    2    1    0


Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHR DH, CLworks as follow -



                           [1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ]  
                           7     6    5    4    3   2   1   0

after first shift

                         [ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 1 ]  ->   [0] CF  
                           7     6    5    4    3    2     1    0

then the 2nd
                        [ 1 ][ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ]  ->   [1]CF  
                           7     6    5    4    3    2     1    0



Finally the 3rd

                0  ->  [ 1 ][ 1 ][ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ]  ->  [0]CF  
                           7     6    5     4    3     2     1   0

These shifting instructions are handy for multiplication and division. As you may notice that the difference of subsequent binary digits change as if they are multuplied by 2
            16     8    4     2     1  . . . .
So if we shift the bits to the right it is simply multiplied by 2 !

                    0 1 1 1 b    =  7d

after 1 SHL

                    1 1 1 0 b    =  14d !


Similarly shifting the bits right divides them by 2



                    0 1 0 0 b    =  4d

after 1 SHR


                    0 0 1 0 b    =  2d  !


so if we want to divide the binary number by 4 all we need to do is to shift it right 2 times.


ROR and ROL (rotate right and rotate left)

The ROR and ROL not only shift the bits to certain direction but also move the LSB to MSB or MSB to LSB



ROR

ROL

As you can see that there is a loop. For the case of ROR the LSB is also copied to CF. and for the case of ROL the MSB is copied to CF


RCR and RCL (rotate right /left through carry )

In the case of rotate throug carry the loop is also gone through CF. i.e for the case of RCL the MSB is moved to CF, the CF is moved to LSB and LSB is moved to LSB +1 , and so on

 RCL



RCR


Further reading :
The Intel Microprocessor Architecture, Programming and Interfacing
                                                                     Barry B. Brey

No comments:

Post a Comment