汇编masm的移位指令
自:网络
SHL、SHR、SAL、SAR: 移位指令
;SHL(Shift Left): 逻辑左移
;SHR(Shift Right): 逻辑右移
;SAL(Shift Arithmetic Left): 算术左移
;SAR(Shift Arithmetic Right): 算术右移
;其中的 SHL 和 SAL 相同, 但 SHR 和 SAR 不同.
;SHL、SAL: 每位左移, 低位补 0, 高位进 CF
;SHR : 每位右移, 低位进 CF, 高位补 0
;SAR : 每位右移, 低位进 CF, 高位不变
;它们的结果影响 OF、SF、ZF、PF、CF
;它们的指令格式相同:
SHL r/m, i8
SHL r/m, CL
; Test24_1.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
.code
main proc
;SHL、AL
mov al, 11100111b
shl al, 1
PrintHex al ;CE - 11001110b
sal al, 2
PrintHex al ;38 - 00111000b
mov al, 11100111b
mov cl, 2
shl al, cl
PrintHex al ;9C - 10011100b
;SHR
mov al, 11100111b
shr al, 2
PrintHex al ;39 - 00111001b
;SAR
mov al, 11100111b
sar al, 2
PrintHex al ;F9 - 11111001b
ret
main endp
end main
ROL、ROR、RCL、RCR: 循环移位指令
;ROL(Rotate Left): 循环左移
;ROR(Rotate Right): 循环右移
;RCL(Rotate through Carry Left): 带进位循环左移
;RCR(Rotate through Carry Right): 带进位循环右移
;ROL: 循环左移, 高位到低位并送 CF
;ROR: 循环右移, 低位到高位并送 CF
;RCL: 循环左移, 进位值(原CF)到低位, 高位进 CF
;RCR: 循环右移, 进位值(原CF)到高位, 低位进 CF
;它们的结果影响 OF、CF
;它们的指令格式相同:
SHL r/m, i8
SHL r/m, CL
; Test24_2.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
.code
main proc
;ROL
mov al, 11000011b
rol al, 1
PrintHex al ;87 - 010000111b
;ROR
mov al, 11000011b
ror al, 1
PrintHex al ;E1 - 11100001b
;RCL
clc
mov al, 10101111b
rcl al, 1
PrintHex al ;5E - 01011110b
stc
mov al, 10101111b
rcl al, 1
PrintHex al ;5F - 01011111b
clc
mov al, 10101111b
rcl al, 2
PrintHex al ;BD - 10111101b
;RCR
clc
mov al, 11101011b
rcr al, 2
PrintHex al ;BA - 10111010b
ret
main endp
end main
SHLD、SHRD: 双精度移位
;SHLD(Double-Precision Shift Left): 双精度左移
;SHRD(Double-Precision Shift Right): 双精度右移
;SHLD 和 SHRD 有三个操作数
;操作数三: 是移位数目
;操作数一: 是目的操作数
;SHLD 左边被移出的位由操作数二相同数目的高位填充
;SHRD 右边被移出的位由操作数二相同数目的低位填充
;操作数二不变, 且须是寄存器
;它们的结果影响 OF、SF、ZF、PF、CF
;它们的指令格式相同:
SHLD r16/32/m16/m32, r16/32, i8
SHLD r16/32/m16/m32, r16/32, CL
; Test25_3.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
.code
main proc
;SHLD
mov ax, 1100110011110000b
mov dx, 1111111100000000b
shld ax, dx, 2
PrintHex ax ;33C3 - 0011001111000011b
;SHRD
mov ax, 0000111100110011b
mov dx, 0000000011111111b
shrd ax, dx, 2
PrintHex ax ;C3CC - 1100001111001100b
ret
main endp
end main