Assembly Language – Shift Instructions

If you tried to search sites for advanced assembly language tutorials (Intel architecture). you will find that the number of the useful sites is relatively little compared to the other programming languages . therefore, and due to the lack of advanced assembly language tutorials, i am willing to share my humble knowledge in assembly programming with you all.

if you are not familiar with the term “Assembly Language Instructions”, this post is not for you. this post is dedicated to the people who already know the simple instructions of assembly language and want to know more advanced instructions in assembly language.

In this post i will explain the “Shift Instructions” ien Assembly Language, how to use it and finally the practical uses of those instructions.

i will explain the shift instructions for Unsigned binary numbers here.

Explaining Shift Instructions:

Shift Instructions in assembly language is used to shift the most significant bit (MSB) or the least significant bit (LSB) in binary number (in a register or a variable).

Now you might ask, what is shifting??
for example if we have a binary number (e.g. 1111 0000)
using the shift instruction we can make 2 decisions:
1- if we choose to shift that binary number to the left by one bit the outcome will be (1110 0000)
2- if we choose to shift that binary number to the right by one bit the outcome will be (0111 1000)

Now you might ask another question, where did the (MSB) go? and what happened to the (LSB)?
well, in the first case the (left shifting) (MSB) (which is “one” in this case) went to some kind of temporary location called C-Flag (Carry-Flag), and an additional “zero” have been added to the right of the binary number and has become the new (LSB).

the same goes with the second case (right shifting), we took the (LSB) and put it in the C-Flag and then we added a “zero” to the left hence, the new (MSB) is “zero”.

you can imagine the C-flag as a Box, whenever you remove a bit from a binary number you put that bit in that box before you throw it a way!

Code Explaination for Shift Instructions:

i am sure that all programmers now are saying “WTH does that mean??”.
i am also a programmer and i understand codes better then plain English!!! so, if you didnt understand what i wrote about the shift instruction previously, i am sure that you will understand it in this sections.

lets assume that we have the following binary number in the register DL,

mov DL, 10101111B ; equivalent to 175 in decimal
shl DL, 1 ; means shift the binary number in DL to the left by "one" bit

the outcome of the previous instructions will be as follows:
1- DL will be equal to “0101 1110” (moved the MSB to the C-flag and added “zero” to the right)
0101 1110 is equivalent to 94 in decemal.
2- C-flag will be equal to “one”, because the bit we moved from the binary number was “one”.
if we keep on repeating the shift instruction (shl – SHift to the Left) on the same binary number we will get the following results:

Left Shifting:

Code———–Number before shifting ———Number after shifting
shl DL, 1 ———–10101111 (175 decimal)——–01011110 (94 decimal)
shl DL, 1 ———- 01011110 (94 decimal) ———10111100 (188 decimal)
shl DL, 1 ———–10111100 (188 decimal) ——-01111000 ( 120 decimal)
shl DL, 1 ———–01111000 ( 120 decimal) ——11110000 (240 decimal)

if you keep on going you will naturally get all zeros at the end.
Now you might ask “Why is this guy keep on putting the decimal equivalents???”
if you noticed the pattern you will find that if we shifted a “zero” from the left, the number will be DOUBLE the number (as in the case in the second and fourth instructions) before shifting, but if we shifted a “one” the new number will just be different.

Right Shifting:
Code————Number before shifting ———- -Number after shifting
shr DL, 1 ——-10101111 (175 decimal) ———- 01010111 (87 decimal)
shr DL, 1 ——- 01010111 (87 decimal) ———- 00101011 (43 decimal)
shr DL, 1 ——-00101011 (43 decimal) ———- 00010101 (21 decimal)
shr DL, 1 ——-00010101 ( 21 decimal) ———- 00001010 (10 decimal)

here the pattern is terribly clear!!! as we shift a bit from the right the number will always be halved (i.e. divided by two) no matter what bit you shift (“zero” or “one”) if the number is odd the result after division will be lower rounded (e.g. 175/2 = 87.5) the number will be 87. again obviously if we keep on using right shifting instruction, the number will be halved everytime we use the instruction and naturally we will get all zeros at the end.

Example of using shift instructions:

this example will illustrate the importance of the shift instructions. if any of you have taken a course intitled “Digital Logic” (which most of you probably took it, because its essential prerequisite to assembly programming) you will be familiar with the terms “even parity” and “odd parity”. parity refers to the number of “ones” in a binary number (e.g. 1101 has “odd parity” because the number of “ones” in the number is “odd”), those are used to verify if a “message” sent by a computer has been received correctly in the other side. what happens is, after a message is sent, it transfers through the internet in the form of bits (zeros and ones) so, to verify if the message has been correctly sent and received the number of “ones” and whether the parity is odd or even is send along with the message, and when the receiver gets the message the first thing is to check the parity of the received message and compare it with the parity that has been sent by the sender, if they doesnt match then the message is definitely has been altered or some of the bits have been lost while transferring the message.
so, this program will check if the parity is “odd” or “even” in a particular binary number.

mov Ax, 0
mov Cx, 8
mov DL, 10101111B
Next: shl DL, 1
JC AddOne ; jump if C-flag = 1
loop Next
JMP Done
AddOne: inc Ax ; increment number of ones
loop Next
Done: mov BL, 2
div BL ; divide number of ones by 2
mov AL, AH
mov AH, 0
call putDec ; “0” if the number is even else “1” if the number is odd


  1. Saud Said Al-Zakwani

    Some one is busy studying Assembly during holidays. Nice Article Arkan. “Shift”

  2. I don’t mean to be mean but I learned that during my first year of higher studies.
    And also, that’s not specific to assembler, nearly every programming language has “bitwise operators” and shift is “< <" and ">>” in these languages.

    PS : don’t take this personally, but reading this kind of article makes the author a little less a programmer

  3. Arkan Hadi

    well, thanx speps for your comment … i wanted to emphasize the importance of the BIT manipulation. some programmers, just pass it by without realizing the actual meaning of it … or why do we do it.
    and i didnt say that BIT manipulations is only in assembly!! its mostly used in assembly language though …

    PS: i took your last comment personally.


  4. Asalam-0-alikum!

    well I am not new to programming, but thing is that I haven’t study assembly language during my bachelors’ and now I am studying it after learning most of high level languages so I am facing some problems….Today while searching i found your article ..It was nice and easy to understand…can you suggest me some book that explain assembly language from high level programmer’s point of view…


  5. whoa, that is about the worst algorithm for parity. Try this:

    mov eax,[your number]
    move edx,eax
    shr eax,16
    xor eax,edx
    mov edx,eax
    shr eax,8
    xor eax,edx
    setpo al ; al is the parity of your number

  6. salam to all
    yar i dont know what is mean by shifting
    plz guide me yar i m very upset
    what does it meab shift left shift right

Leave a Reply