学 Win32 汇编[6]: 伪指令 DUP 与数组


声明数组实例:
; Test6_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
    ;声明并初始化有三个元素的 DWORD 数组; 该数组每个元素是 4 字节
    val dd 11,22,33
.code
start:
    mov eax, val
    PrintDec eax     ;11
    mov eax, val[4]
    PrintDec eax     ;22
    mov eax, val[8]
    PrintDec eax     ;33
    ret
end start

上面的例子也可以这样写:
; Test6_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
    val dd 11
        dd 22,33
.code
start:
    mov eax, val[4*0]
    PrintDec eax     ;11
    mov eax, val[4*1]
    PrintDec eax     ;22
    mov eax, val[4*2]
    PrintDec eax     ;33
    ret
end start

使用伪指令 DUP:
; Test6_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
    ;声明有三个元素的 DWORD 数组, 并把每个元素初始化为 6
    v1 dd 3 dup(6)
    ;声明有三个元素的 DWORD 数组, 无初始化; 对全局变量, 没有初始化的将用 0 填充
    v2 dd 3 dup(?)
.data?
    ;声明有三个元素的 DWORD 数组
    v3 dd 3 dup(?)

.code
start:
    DumpMem offset v1, 12  ;06 00 00 00 - 06 00 00 00 - 06 00 00 00
    DumpMem offset v2, 12  ;00 00 00 00 - 00 00 00 00 - 00 00 00 00
    DumpMem offset v3, 12  ;00 00 00 00 - 00 00 00 00 - 00 00 00 00
    ret
end start

现在可以区别未初始化的变量在 .data 段和 .data? 的区别:
; Test6_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
    v1 dd 4096 dup(?) ;未初始化变量在 .data 段, 生成的 exe 文件会根据大小分配内存
.code
start:
    PrintText 'Hi'
    ret
end start

; ------------------------------------------------------
; 上面的程序的 exe 是 18944 字节
; 下面的程序的 exe 是 2560 字节
; 它们刚好相差 4096*4 个字节
; 结论: 不需要初始化的变量应该声明在 .data? 段
; ------------------------------------------------------

; Test6_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 dd 4096 dup(?) ;未初始化变量在 .data? 段
.code
start:
    PrintText 'Hi'
    ret
end start

继续 dup 的例子:
; Test6_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
    val dd 2 dup(1,2,3)
.code
start:
    DumpMem offset val, 24 ;01000000-02000000-03000000-01000000-02000000-03000000
    ret
end start

嵌套数组:
; Test6_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
    val dd 2 dup(3 dup(1,2))
.code
start:
    DumpMem offset val, 48 ;1 2 1 2 1 2  1 2 1 2 1 2
    ret
end start

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值