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:
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.
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
AddOne: inc Ax ; increment number of ones
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