TYPE: 获取类型大小, 对数组是获取每个元素的大小

; Test10_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?
    v1 db ?
    v2 dw ?
    v3 dd ?
    v4 df ?
    v5 dq ?
    v6 dt ?
    v7 db 1024 dup(?)
.code
main proc
    PrintDec (type v1)  ;1
    PrintDec (type v2)  ;2
    PrintDec (type v3)  ;4
    PrintDec (type v4)  ;6
    PrintDec (type v5)  ;8
    PrintDec (type v6)  ;10
    PrintDec (type v7)  ;1
    ret
main endp
end main
--------------------------------------------------------------------------------


$: 当前程序语句的地址 
--------------------------------------------------------------------------------

; Test10_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
    V1 db 11,22,33,44
    V1Size = ($ - V1)  ;这是变量 V1 的大小
    
    V2 dw 11,22,33,44
    V2Size = $ - V2    ;这是变量 V2 的大小
    
    V3 dd 10 dup(0)
    V3Size = $ - V3      ;这是变量 V3 的大小
    V3Len  = ($-V3) / 4  ;这是变量 V3(dword 类型数组)的元素数目
    
    V4 db 'abcdefghijklmnopqrstuvwxyz', 0
    V4Size = $ - V4      ;这是变量 V4 的大小
.code
main proc
    PrintHex $        ;00401001, 这是当前语句的地址
    PrintDec V1Size   ;4
    PrintDec V2Size   ;8
    PrintDec V3Size   ;40
    PrintDec V3Len    ;10
    PrintDec V4Size   ;27
    PrintHex $        ;0040135A, 这是当前语句的地址
    ret
main endp
end main

LENGTHOF: 数组元素个数; SIZEOF: 总字节数

; Test10_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
    v1 db 'abc', 0
    v2 dd 11, 22, 33, 44, 55
    v3 dd 10 dup(0)
.code
main proc
    PrintDec (lengthof v1)  ;4
    PrintDec (lengthof v2)  ;5
    PrintDec (lengthof v3)  ;10
    
    PrintDec (sizeof v1)  ;4
    PrintDec (sizeof v2)  ;20
    PrintDec (sizeof v3)  ;40
    ret
main endp
end main

TYPEDEF: 创建自定义类型

; Test10_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

;自定义一个等同于 DWORD 的类型: Cardinal
Cardinal typedef ptr dword

.data
    val Cardinal 100  ;使用 Cardinal 定义变量
.code
main proc
    PrintDec val  ;100
    ret
main endp
end main

;TYPEDEF 还用于定义函数原型, 如:
;MyType typedef proto :DWORD, :DWORD, :DWORD

打赏