声明数组实例:
; 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


posted on 2010-04-04 18:03  万一  阅读(7348)  评论(0编辑  收藏  举报