These two gems show how to shift bit arrays. The first one is made for the 80386 processor (or above). The second way can run on any processor starting with 8086.
What if you would like to shift a bit array to the right by, for example 3 bits so that 0100100....0101010
becomes 0000100100...0101010
. Terje Mathisen suggets the following piece of code which uses SHRD
:;
; Shift a bit array 3 steps to the right
;
; input:
; cl = shift amount (here 3)
; ebx = array length
; esi = array
;
; output:
; shifted array, esi=last index+4
;
; destorys:
; eax, edx
;
mov cl,3 ; Shift amount
xor edx,edx ; Shift in zero bits at the top
more:
mov eax,[esi]
push eax ; Save current dword
shrd eax,edx,cl ; Shift in previous bottom bits
mov [esi]
add esi,4
pop edx ; Pop back the saved original value
dec ebx ; Check count
jnz more
If you just want to shift the bits 1 left Jim Neil suggests the following piece of code which is smaller:;
; Shift a bit array 1 step left
;
; input:
; cx = array length
; bx = offset to array start point
;
; output:
; shifted array, bx = start point
;
; destroys:
; nothing
;
add bx,cx ; point to last byte + 1.
clc ; clear carry
more:
dec bx ; point to next byte
rcl [bx],1 ; shift (cf contains extra bit)
loop more ; loop til done...