一个懒人专用文章归档神器,如果你不能成为压路机的一部分,那么你就只能成为道路的一部分 Bala~bala~

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

汇编 懒人 1351℃ 0评论

声明数组实例:


; 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

转载请注明:懒人档案室 » Win32 汇编[6]: 伪指令 DUP 与数组

喜欢 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址