学 Win32 汇编[25] - 符号扩展指令: CBW、CWDE、CDQ、CWD

2021/4/30 12:25:10

本文主要是介绍学 Win32 汇编[25] - 符号扩展指令: CBW、CWDE、CDQ、CWD,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

学 Win32 汇编[25] - 符号扩展指令: CBW、CWDE、CDQ、CWD


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

posted on 2010-04-15 15:13  万一  阅读(11609)  评论(0)  编辑  收藏

这篇关于学 Win32 汇编[25] - 符号扩展指令: CBW、CWDE、CDQ、CWD的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程