Shift/Rotate Instructions

The next lot of instructions to cover are the shift and rotate instructions. To the person unfamiliar with assembly language, instructions such as these look pretty useless. However, nothing could be further from the truth. A shift instruction simply shifts the bits in a register left or right by a certain amount. Bits that "fall off the end" are lost. Whilst this may sound like a useless operation, it is actually quite useful if you know what to do with it. For one thing, shifting all the bits in a number to the left one place is equivalent to multiplying the number by 2. This is because of the base 2, or binary, nature of bits. If we had been in ordinary base 10, shifting to the left one place would simply mean multiplying by 10. Similarly, shifting to the right one bit divides by 2 (the remainder is lost).

Another use for shift instructions is to manipulate information contained in fields within a word. Perhaps the top five bits mean something special, and we want to extract them. Shifting the word to the right by 11 bits will bring these top five bits to the bottom of the word.

A rotate is similar to a shift, except the bits that fall off one end are recycled and put back in the other end. Rotate instructions are useful if you want to select a certain range of bits by moving them to the bottom of the word, but you don't want to lose all the other information in the word.

C provides us with a shift operation. To shift right one uses >>, whilst to shift left, one uses &<<. The value that is to have its bits shifted is put to the left of the operator, and the number of bits to shift by is put on the right hand side of the operator. Nothing could be easier.

Rotating bits is slightly more complicated. To accomplish this we make use of a 32 bit integer. Essentially, the extra 16 bits of the 32 bit integer are used to store the extra bits that we now do not want to lose after shifting. Basically, we do the shift as normal, but store the result in the 32 bit integer. This tells C we want all 32 bits of the result of the shift. The final part of the procedure is to put the bits that now overhang the original 16 bits of space, back in the other end of the 16 bit word, as though they had been rotated out one end of the 16 bit word and back in the other end.

This calculation is done using modulo arithmetic, which we have described previously. By taking the quotient and remainder of a 32 bit integer after division by 65536 = 2^16, we can get access to the upper and lower 16 bits of the 32 bit integer. So basically, we perform a shift (taking into account the fact that a rotate by a multiple of 16 bits does nothing), split the resulting 32 bit integer into two 16 bit words, and add them together. It should be easy to tell from the code itself how all this works.

We make note of one final trick. Instead of shifting right when performing a rotate right, we shift left instead. This is because we do not want to lose the information we are interested in out the bottom end of our 32 bit integer. So, essentially, we shift left by 16-n bits, where n is the number of bits we wanted to shift right by. This has the same effect, when we add the two 16 bit words together, as if we had shifted right into an additional 16 bits of space and added.

top

Flag Manipulation

Sometimes we wish to set or clear the various flags. The instructions for doing this, and their implementations are very straightforward and should be self explanatory.