CBW、CWDE、CDQ、CWD: 擴展指令
;CBW(Convert Byte to Word): 將 AL 擴展為 AX
;CWDE(Convert Word to Extended Double): 將 AX 擴展為 EAX
;CDQ(Convert Doubleword to QuadWord): 將 EAX 擴展為 64 位數 EDX:EAX
;CWD(Convert Word to DoubleWord): 將 AX 擴展為 DX:AX
;它們都是用符號位填充多出的空間
;它們對 EFLAGS 無影響, 也無參數
; Test25_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
;CBW
mov al, 7Fh
cbw
PrintHex ax ;007F
mov al, 80h
cbw
PrintHex ax ;FF80
;CWDE
mov ax, 7FFFh
cwde
PrintHex eax ;00007FFF
mov ax, 8000h
cwde
PrintHex eax ;FFFF8000
;CDQ
mov eax, 7FFFFFFFh
cdq
PrintHex edx ;00000000
PrintHex eax ;7FFFFFFF
mov eax, 80000000h
cdq
PrintHex edx ;FFFFFFFF
PrintHex eax ;80000000
;CWD
mov ax, 7FFFh
cwd
PrintHex dx ;0000
PrintHex ax ;7FFF
mov ax, 8000h
cwd
PrintHex dx ;FFFF
PrintHex ax ;8000
ret
main endp
end main
符號擴展指令的本質
;一個正數(無符號)或負數(有符號)在擴展儲存空間時, 使用這些指令可保證原值不變
; Test25_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
mov al, 68
cbw
PrintHex ax ;0044
PrintDec ax ;68
mov al, -68
cbw
PrintHex ax ;FFBC
PrintDec ax ;-68
ret
main endp
end main
CBW、CWDE 和 MOVSX 的關系
; 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
;CBW 等價於 movsx ax, al
mov al, 80h
cbw
PrintHex ax ;FF80
mov al, 80h
movsx ax, al
PrintHex ax ;FF80
;CWDE 等價於 movsx eax, ax
mov ax, 8000h
cwde
PrintHex eax ;FFFF8000
mov ax, 8000h
movsx eax, ax
PrintHex eax ;FFFF8000
ret
main endp
end main