学 Win32 汇编[19]: 查看二进制等相关函数

学 Win32 汇编[19]: 查看二进制等相关函数


在 masm32.inc 中有这样几个函数的声明:
byt2bin_ex  PROTO :BYTE, :DWORD
wrd2bin_ex  PROTO :WORD, :DWORD
dw2bin_ex   PROTO :DWORD, :DWORD
dw2hex_ex   PROTO :DWORD, :DWORD
bin2byte_ex PROTO :DWORD

byt2bin_ex: 从 Byte 数到二进制字符串
; Test19_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

.data
    szBin db 8 dup(0), 0  ;这是接收二进制字符串的缓冲区
    bVal  db 3
.code
main proc
    ;查看数字的二进制信息
    invoke byt2bin_ex, 2, addr szBin
    PrintString szBin ;00000010
    
    ;查看变量 bVal 的二进制信息
    invoke byt2bin_ex, bVal, addr szBin
    PrintString szBin ;00000011
    
    ;查看寄存器 al 的二进制信息
    mov al, 0feh
    invoke byt2bin_ex, al, addr szBin
    PrintString szBin ;11111110
    ret
main endp
end main

wrd2bin_ex: 从 Word 数到二进制字符串
; Test19_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

.data
    szBin db 16 dup(0), 0  ;这是接收二进制字符串的缓冲区
    wVal  dw 3
.code
main proc
    ;查看数字的二进制信息
    invoke wrd2bin_ex, 2, addr szBin
    PrintString szBin ;0000000000000010
    
    ;查看变量 wVal 的二进制信息
    invoke wrd2bin_ex, wVal, addr szBin
    PrintString szBin ;0000000000000011
    
    ;查看寄存器 ax 的二进制信息
    mov ax, 0fffeh
    invoke wrd2bin_ex, ax, addr szBin
    PrintString szBin ;1111111111111110
    ret
main endp
end main

dw2bin_ex: 从 DWord 数到二进制字符串
; Test19_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

.data
    szBin db 32 dup(0), 0  ;这是接收二进制字符串的缓冲区
    dwVal dd 3

.code
main proc
    ;查看数字的二进制信息
    invoke dw2bin_ex, 2, addr szBin
    PrintString szBin ;00000000000000000000000000000010
    
    ;查看变量 dwVal 的二进制信息
    invoke dw2bin_ex, dwVal, addr szBin
    PrintString szBin ;00000000000000000000000000000011
    
    ;查看寄存器 eax 的二进制信息
    mov eax, 0fffffffeh
    invoke dw2bin_ex, eax, addr szBin
    PrintString szBin ;11111111111111111111111111111110
    ret
main endp
end main

dw2hex_ex: 从 DWord 数到十六进制字符串
; Test19_4.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

.data
    szHex db 8 dup(0), 0  ;这是接收十六进制字符串的缓冲区
    dwVal dd 255
.code
main proc
    ;查看数字的十六进制信息
    invoke dw2hex_ex, 16, addr szHex
    PrintString szHex ;00000010
    
    ;查看变量 dwVal 的十六进制信息
    invoke dw2hex_ex, dwVal, addr szHex
    PrintString szHex ;000000FF
    
    ;查看寄存器 eax 的十六进制信息
    mov eax, 2694881440
    invoke dw2hex_ex, eax, addr szHex
    PrintString szHex ;A0A0A0A0
    ret
main endp
end main

bin2byte_ex: 从字符串(必须有 8 个 '0' 或 '1' 组成)到字节
; Test19_5.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

.data
    szTxt1 db '00000010', 0
    szTxt2 db '01111011', 0
    szTxt3 db '11111111', 0
.code
main proc
    invoke bin2byte_ex, addr szTxt1
    PrintDec eax  ;2
    
    invoke bin2byte_ex, addr szTxt2
    PrintDec eax  ;123
    
    invoke bin2byte_ex, addr szTxt3
    PrintDec eax  ;255
    ret
main endp
end main

学习查看二进制是为了看到 EFLAGS 中的二进制位的变化:
; Test19_6.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

.data
    szBin db 8 dup(?), 0
.code
main proc
    lahf  ;LAHF 指令是把 EFLAGS 寄存器的低字节读入 AH
    invoke byt2bin_ex, ah, addr szBin
    PrintString szBin  ;01000110
    ret
main endp
end main

posted on 2010-04-13 12:36  万一  阅读(1856)  评论(0)  编辑  收藏
上一篇:学 Win32 汇编[17]: 关于压栈(PUSH)与出栈(POP) 之一


下一篇:学 Win32 汇编[12]: PTR、OFFSET、ADDR、THIS