移位和循环移位指令
shl, shr sal, sar, sol, sor, scl, scr, shld, shrd
逻辑移位和算术移位
shl
shl reg/mem, imm8/cl
mov bl, 8fhshl bl, 1 ; bl = 00011110b cf = 1mov al, 10000000bshl al, 2 ; cf = 0, al = 00000000b
当一个数多次进行左移时,进位标志保存的是最后移出最高有效位的数值
位元乘法
shr
shr reg/mem, imm8/cl
```assembly
mov al, 0D0h
shr al, 1 ; al = 01101000 cf = 0
mov al, 00000010bshr al, 2 ; al = 00000000b cf = 1```位元除法当一个数多次进行右移时,进位标志保存的是最后移出最低有效位的数值
sal和sar
sal 与shl一样
mov al, 0f0hsar al, 1 ; cf = 0 al = 11111000b; 有符号数除法mov dl, -128sar dl, 3 ; dl = 11110000b cf = 0; ax符号扩展到eaxmov ax, -128shl eax, 16sar eax, 16
rol
把所有位都向左移, 最高位复制到进位标志位和最低位
mov al, 40hrol al, 1 ; al = 10000000b cf = 0rol al, 1 ; al = 00000001b cf = 1rol al, 1 ; al = 00000010b cf = 9; 循环多次mov al, 26hrol al, 4 ; al = 62hmov ax, 6a4bhrol ax, 4 ; ax = a4b6hrol ax, 4 ; ax = 4b6ahrol ax, 4 ; ax = b6a4hrol ax, r ; ax = 6a4bh
ror
把所有位都向右移, 最低位复制到进位标志位和最高位
mov al, 01hror al, 1 ; al = 10000000b cf = 1ror al, 1 ; al = 01000000b cf = 0; 循环多次mov al, 00000100bror al, 3 ; al = 80h cf = 1
rcl 和 rcr
rcl: 每一位左移,进位标志复制到最低位,最高位复制到进位标志rcr: 每一位右移,进位标志复制到最高位,最低位复制到进位标志
clc ; cf = 0mov bl, 88hrcl bl, 1 ; bl = 00010000b cf = 1rcl b1, 1 ; bl = 00100001b cf = 0; 从进位标志位恢复位.datatestVal byte 01101010b.codeshr testVal, 1 ; testVal = 00110101b cf = 0jc exitrcl testVal, 1 ; testVal = 01101010b cf = 0stc ; cf = 1mov ah, 10hrcr ah, 1 ; ah = 10001000b cf = 0
有符号数溢出
mov al, +127rol al, 1 ; al = 11111110b of = 1mov al, -128shr al, 1 ; al = 01000000b of = 1
shld和shrd指令
shld/shrd dest, source, count
shld/shrd reg16, reg16, cl/imm8
shld/shrd mem16, reg16, cl/imm8
shld/shrd reg32, reg32, cl/imm8
shld/shrd mem32, reg32, cl/imm8
.datawVal word, 9ba6h.codemov ax, 0ac36hshld wVal, ax, 4 ; wVal = ba6ahmov ax, 234bhmov dx, 7654hshrd ax, dx, 4 ; ax = 4243.dataarray dword 648b2165h, 8c943a29h, 6dfa4b86h, 91f76c04h, 8baf9857h.codemov bl, 4mov esi, offset arraymov ecx, (lengthof array) - 1L1:push ecxmov eax, [esi + type dword]mov cl, blshrd [esi], eax, cladd esi, type dwordpop ecxloop L1shr dword ptr [esi], 4