首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘`TYPE`’但在NASM

‘`TYPE`’但在NASM
EN

Stack Overflow用户
提问于 2022-11-20 19:28:42
回答 1查看 38关注 0票数 3

我知道MASM中有SIZEOFTYPE,但是NASM呢?正如我发现的,它没有这些。

例如:

代码语言:javascript
复制
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的方法?如果没有,如何使程序确定元素本身的大小?

EN

回答 1

Stack Overflow用户

发布于 2022-11-20 20:34:43

NASM保持其指令简单,并具有最少的自动功能。

它似乎不是MASM typesizeoflength操作符的对应指令。

幸运的是,NASM采用了比在程序集中引入重型类型化更好的方法:它开发了一个非常丰富和富有表现力的宏系统。

考虑一下这个简单的宏:

代码语言:javascript
复制
;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和类似的,或者使arrdbarrdwarrdd变体隐式地接受项声明指令)。

示例:

代码语言:javascript
复制
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

代码语言:javascript
复制
00000000  B80C000000        mov eax,0xc
00000005  B804000000        mov eax,0x4
0000000A  B803000000        mov eax,0x3
0000000F  A114000000        mov eax,[0x14]
00000014  <REDACTED GARBAGE>
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74511262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档