学 Win32 汇编[4]: 使用 Debug 输出测试结果

学 Win32 汇编[4]: 使用 Debug 输出测试结果


由于 Debug 输出是使用的标准的 Win32 窗口, 所以 include 和 includelib 的文件较多.

这样建立工程时就不需要是控制台程序了, 工程建立方法:
文件 -> 新建工程 -> 工程类型: Win32 App (no res) ...

PrintString: 输出字符串变量
; Test4_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
    szText db 'Hello World!', 0

.code
start:
    PrintString szText  ;Hello World!
    ret
end start

PrintText: 直接输出字符串
; Test4_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
start:
    PrintText 'Hello World!'  ;Hello World!
    ret
end start

PrintStringByAddr: 根据字符串地址输出字符串
; Test4_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
    szText db 'Hello World!', 0
    
.code
start:
    PrintStringByAddr offset szText  ;Hello World!
    
    mov eax, offset szText
    PrintStringByAddr eax            ;Hello World!
    ret
end start

PrintLine: 输出一行线
; Test4_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
    szText db 'Hello World!', 0
    
.code
start:
    PrintLine           ;----------------------------------------
    PrintString szText  ;Hello World!
    PrintLine           ;----------------------------------------
    ret
end start

PrintDec: 输出 8-32 位的十进制整数
; Test4_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
    v1 db 127
    v2 db -128
    v3 dw 32767
    v4 dw -32768
    v5 dd 2147483647
    v6 dd -2147483648
    
.code
start:
    PrintDec v1  ;127
    PrintDec v2  ;-128
    PrintDec v3  ;32767
    PrintDec v4  ;-32768
    PrintDec v5  ;2147483647
    PrintDec v6  ;-2147483648
    
    ;它还可以有第二个文本参数
    PrintDec v1, 'Other Text'  ;127, Other Text
    ret
end start

PrintHex: 输出 8-32 位的十进制整数
; Test4_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
    v1 db 127
    v2 db -128
    v3 dw 32767
    v4 dw -32768
    v5 dd 2147483647
    v6 dd -2147483648
    
.code
start:
    PrintHex v1  ;7F
    PrintHex v2  ;80
    PrintHex v3  ;7FFF
    PrintHex v4  ;8000
    PrintHex v5  ;7FFFFFFF
    PrintHex v6  ;80000000
    
    ;它还可以有第二个文本参数
    PrintHex v1, 'Other Text'  ;7F, Other Text
    ret
end start

PrintDouble: 输出双精度浮点数
; Test4_7.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
    v1 real8 3.14
    
.code
start:
    PrintDouble v1                ;3.14
    
    ;它还可以有第二个文本参数
    PrintDouble v1, 'Other Text'  ;3.14, Other Text
    ret
end start

Spy..StopSpy: 跟踪变量(变量必须是 8-32 位整数)
; Test4_8.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
    val dd 0
    
.code
start:
    Spy val
    mov val, 2  ;2
    add val, 3  ;5
    sub val, 1  ;4
    shl val, 1  ;8
    StopSpy
    
    ret
end start

DumpMem: 查看内存
; Test4_9.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
    szText db 'ABCDEFG', 0
    num    dd 13579ACEh

.code
start:
    DumpMem offset szText, 4  ;41 42 43 44
    DumpMem offset szText, 7  ;41 42 43 44 45 46 47
    
    DumpMem offset num, 4     ;CE 9A 57 13
    
    ;它也可以有个文本参数
    DumpMem offset num, 4, 'txt'
    ret
end start

DbgDump: 类似 DumpMem, 更适合查看文本
; Test4_10.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
    szText db 'ABCDEFG', 0
    num    dd 13579ACEh

.code
start:
    DbgDump offset szText, 4     ;41 42 43 44 - ABCD
    DbgDump offset szText, 7     ;41 42 43 44 45 46 47 - ABCDEFG
    
    DbgDump offset num, 4        ;CE 9A 57 13

    ;它也可以有个文本参数
    DbgDump offset num, 4, 'txt'
    ret
end start

需要注意的是 debug.inc 提供的这些宏指令都是区分大小写的.

还有更多指令可参见帮助: \RadASM\masm32\help\VKDebug.chm(帮助内容不是很全)
或直接查看 \RadASM\masm32\include\debug.inc

它还包括一个非常有用的宏 CTEXT, 这在用 invoke 调用有字符串参数的函数时非常方便.
使用 CTEXT 可以把前面那个 "最简单的 Win32 程序" 修改如下:
; Test4_11.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    debug.inc
includelib kernel32.lib
includelib debug.lib

include    user32.inc
includelib user32.lib

.code
start:
    invoke MessageBox, NULL, CTEXT("Hello World!"), CTEXT("Hi"), MB_OK
    invoke ExitProcess, NULL
end start

posted on 2010-04-04 13:55  万一  阅读(4725)  评论(5)  编辑  收藏
上一篇:学 Win32 汇编[9]: 子过程中的变量声明


下一篇:学 Win32 汇编[11]: 数据对齐相关的伪指令(ALIGN、EVEN、ORG)