COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Bit-Shifting in ASM

• Email
• Follow

```Hello,

I was wondering if there is a command in assembly that can shift the
bits of a given binary string to the left.  For example, the string
11100000 would become 11000001.

1110 -> 1101
0001 -> 0010
0000 -> 0000

and so on...

Can this be done in assembly? Thank you,
Dustin

```
 0

See related articles to this posting

```jex wrote:

> Hello,
>
> I was wondering if there is a command in assembly that can shift the
> bits of a given binary string to the left.  For example, the string
> 11100000 would become 11000001.
>
> 1110 -> 1101
> 0001 -> 0010
> 0000 -> 0000
>
> and so on...
>
> Can this be done in assembly? Thank you,
> Dustin

If you would like to shift left/right a string you'd use SHL and SHR (SHift
Left and SHift Right). For the word 11100000 to become 11000001 you'd
better use the ROL and ROR operators (ROtate Left and ROtate Right).
The use it's pretty simple:
SHL AX, CL ; CL contains the number of bit to shift, AX the word to shift
ROL AX, CL ; same as SHL
Note that with the SHL/SHR the bit that leave the word is copied onto to
Carry.

Hope this help,
Luca

```
 0

```What you want is to rotate the bits...

Here is an example:

mov eax,11100000b ;after operation: EAX = 11100000b
rol eax,1                   ;after operation: EAX = 11000001b

ROL rotates bits to the left, ROR rotates them to the right.

With shifting you would end up like this:

mov eax,11100000b ;after operation: EAX = 11100000b
shl eax,1                   ;after operation: EAX = 11000000b and Carry flag
= 1

SHL shifts bit to the left, SHR shifts them to the right.
(If bits are shifted "out" of the register they will end up int the carry
flag).

//Spike

"jex" <sales@jexnet.com> skrev i meddelandet
> Hello,
>
> I was wondering if there is a command in assembly that can shift the
> bits of a given binary string to the left.  For example, the string
> 11100000 would become 11000001.
>
> 1110 -> 1101
> 0001 -> 0010
> 0000 -> 0000
>
> and so on...
>
> Can this be done in assembly? Thank you,
> Dustin
>

```
 0

```On Wed, 18 Feb 2004 21:57:53 +0000 (UTC), "Spike"
<im_a_user@hotmail.com> wrote:

<snip>
>
>SHL shifts bit to the left, SHR shifts them to the right.
>(If bits are shifted "out" of the register they will end up int the carry
>flag).
Only the last one shifted out. :-)

--
Arargh402 at [drop the 'http://www.' from ->] http://www.arargh.com
BCET Basic Compiler Page: http://www.arargh.com/basic/index.html

```
 0

```sales@jexnet.com (jex) wrote in message news:<369c3027.0402181037.23e10403@posting.google.com>...
> Hello,
>
> I was wondering if there is a command in assembly that can shift the
> bits of a given binary string to the left.  For example, the string
> 11100000 would become 11000001.
>
> 1110 -> 1101
> 0001 -> 0010
> 0000 -> 0000
>
> and so on...
>
> Can this be done in assembly? Thank you,
> Dustin

You say "binary string" but you say nothing about the length of the
string.  If the string is within the register size, 8, 16, or 32 bits,
you can do what the previous posters suggested. If the string is
longer than 32 bits then you will need the CARRY flag as an
intermediary between 32 bit DWORDs.

```
 0

```Thanks very much for all of your help!   The ROL operation worked
great.

> You say "binary string" but you say nothing about the length of the
> string.  If the string is within the register size, 8, 16, or 32 bits,
> you can do what the previous posters suggested. If the string is
> longer than 32 bits then you will need the CARRY flag as an
> intermediary between 32 bit DWORDs

Thanks.  I was concered with <32 bit binary strings, but that's

Dustin

```
 0