我知道MASM中有SIZEOF和TYPE,但是NASM呢?正如我发现的,它没有这些。
例如:
array dw 1d, 2d, 3d, 4d
arrSize db $-array ;4 elements * 2 bytes (size of one element) = 8 bytes这就是我如何使SIZEOF array在NASM中的替代。如果我想要数组元素的数量,我可以将arrSize除以2(如果数组元素是单词)。但是如果数组元素是双字(32位),我需要除以4。
在NASM中是否有一种替代TYPE的方法?如果没有,如何使程序确定元素本身的大小?
发布于 2022-11-20 20:34:43
NASM保持其指令简单,并具有最少的自动功能。
它似乎不是MASM type、sizeof和length操作符的对应指令。
幸运的是,NASM采用了比在程序集中引入重型类型化更好的方法:它开发了一个非常丰富和富有表现力的宏系统。
考虑一下这个简单的宏:
;array <label>, <item directive>, <items>
%macro array 4-*
%1: %2 %{3} ;label: db/dw/dd/... <firstItem>
%%second: %2 %{4:-1} ;Local label (to get item size) and rest of the items
%1_size EQU $ - %1 ;Size of the array in bytes (here - label)
%1_type EQU %%second - %1 ;Size of an item in bytes (second - label)
%1_lengthof EQU %1_size / %1_type ;Number of items (size of array / size of item)
%endmacro您可以像使用array my_array, dd, 1, 2, 3一样使用它。不完全像my_array dd 1, 2, 3,但很接近。
那你就有:
my_array_size.bytes.my_array_type.中数组的大小bytes.my_array_lengthof.中一个数组项的大小数组中的项数.您可以调整宏以适应您的个人风格(例如:生成my_array.size和类似的,或者使arrdb、arrdw、arrdd变体隐式地接受项声明指令)。
示例:
BITS 32
;array <label>, <item directive>, <items>
%macro array 4-*
%1: %2 %{3} ;label: db/dw/dd/... <firstItem>
%%second: %2 %{4:-1} ;Local label, to get item size
%1_size EQU $ - %1 ;Size of the array in bytes (here - label)
%1_type EQU %%second - %1 ;Size of an item in bytes (second - label)
%1_lengthof EQU %1_size / %1_type ;Number of items (size of array / size of item)
%endmacro
;To test, use ndisam -b32
mov eax, my_array_size
mov eax, my_array_type
mov eax, my_array_lengthof
mov eax, DWORD [my_array]
;The array (ignore on output from ndisasm)
array my_array, dd, 1, 2, 3在生成的二进制文件上使用ndisam -b32:
00000000 B80C000000 mov eax,0xc
00000005 B804000000 mov eax,0x4
0000000A B803000000 mov eax,0x3
0000000F A114000000 mov eax,[0x14]
00000014 <REDACTED GARBAGE>https://stackoverflow.com/questions/74511262
复制相似问题