Thursday, December 24, 2009

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

- - Important Notice - -





Programming google group is up. If you want to join request personally to any of the managers!




Message from DIUCSP




This blog is open for all students of CS, CSE, CIS of Daffodil International University.


Join This blog to stay on touch with other students. If you have a gmail account then just sign in as the g-mail user and become a follower of the blog. You can then submit comments on our posts.

You will find information on different subjects of our course, discuss problems and have solution. Try to maintain the subjectwise discussion for early response. And remember this blog is not only for the genius or more than average CS, CSE, CIS students with great computer skills so dont feel shy in submitting your simple daily life problems (even the silliest ones) because I believe if you feel shy in learning now then you will feel shy all your life for not learning it at all.


Please be conscious about sharing your personal Data, DaffodilCSE blog moderator is not responsible for any risk you take, If you feel safe post comment as your own will, If you do not want to share your mail, comment anonymously. Thanks




They are with us

There was an error in this gadget

BlogCatalog

Earn from a cool looking gadget..! It works with Google adsense and any other advertising programs!
Grab now !