TYPEDEF PTR-大小无关紧要? [英] TYPEDEF PTR - size doesn't matter?

查看:102
本文介绍了TYPEDEF PTR-大小无关紧要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到使用TYPEDEF定义具有各种数据类型的PTR似乎没有什么区别.例如,当用于存储和mov 32位地址时,这三种类型的行为似乎完全相同:

I noticed using TYPEDEF to define PTR's with various data types doesn't seem to make any difference. For example, these three types seem to behave exactly the same when used to store and mov 32-bit addresses:

PTYPE TYPEDEF PTR 
PBYTE TYPEDEF PTR BYTE
PWORD TYPEDEF PTR WORD

.data 

arrayByte BYTE 10h,20h,30h

ptr_1 PTYPE arrayByte
ptr_2 PBYTE arrayByte
ptr_3 PWORD arrayByte

.code
main PROC

mov eax, ptr_1 
mov eax, ptr_2 
mov eax, ptr_3 

exit    
main ENDP

除了文件尺寸更易于记录外,是否有实际理由指定尺寸?

Is there any practical reason to specify a size other than it's more self-documenting?

推荐答案

在MASM中几乎没有声明和使用指针类型. MASM中的类型基本上只是大小,仅用于确定对象和操作数的大小以及它们在大小方面的兼容性.如果在汇编示例代码时生成列表文件(在添加.MODEL FLATEND指令以便进行汇编之后),您将看到ptr1ptr2ptr3的类型均为DWORD:

Declaring and using pointer types has little use in MASM. Types in MASM basically are just sizes and are only to used determine sizes of objects and operands and their compatibility size-wise. If you generate a listing file while assembling your example code (after adding .MODEL FLAT and END directives so it assembles), you'll see that the type of ptr1, ptr2 and ptr3 are all DWORD:

Types:

                N a m e                  Size     Attr

PBYTE  . . . . . . . . . . . . .         00000004     Near32 PTR Byte
PTYPE  . . . . . . . . . . . . .         00000004     Near32 PTR
PWORD  . . . . . . . . . . . . .         00000004     Near32 PTR Word

...

Symbols:

                N a m e                 Type     Value    Attr

...
ptr_1  . . . . . . . . . . . . .        DWord    00000003 _DATA
ptr_2  . . . . . . . . . . . . .        DWord    00000007 _DATA
ptr_3  . . . . . . . . . . . . .        DWord    0000000B _DATA

我唯一能看到的关于指针类型可能使它们有用的事实是,它们将根据有效的内存模型自动改变大小.因此,如果您用.MODEL SMALL而不是.MODEL FLAT汇编示例,则ptr1ptr2ptr3的类型将变为WORD而不是DWORD.同样,如果删除模型指令并将其与x64版本的MASM组装在一起,则这些符号的类型将变为QWORD.但是,做这两种事情都表明它没有听起来那么有用,因为示例代码中的MOV EAX, ...指令由于操作数大小不匹配而将产生错误.实际上,仍然需要重写许多其他代码以适应指针大小的变化.

The only thing I can see the about pointer types that might make them useful is the fact that they'll automatically change in size according to the memory model in force. So you if you assemble your example with .MODEL SMALL instead of .MODEL FLAT the types of ptr1, ptr2 and ptr3 become WORD instead of DWORD. Similarly if you delete the model directive and assemble it with the x64 version of MASM the types of those symbols becomes QWORD. However doing either of these things reveals that it's not as useful as it sounds, because the MOV EAX, ... instructions in your example code will then generate errors because of the operand size mismatch. In practice a lot of other code would still need to be rewritten to adjust to the change in pointer size.

另一种可能性是,指针类型将以某种方式在宏中使用以执行有用的操作,但是我真的看不到会是什么.即使作为文档,使用指针类型也是可疑的,因为其他读者在不搜索代码定义的情况下也不会知道PBYTEPTYPE的含义.我不建议使用它们.

Another possibility is that the pointer types would somehow be used in macros to do something useful, but I can't really see what that would be. Even as documentation, using the pointer types is dubious, as other readers aren't going to know what PBYTE or PTYPE mean without searching through the code for their definition. I wouldn't recommend using them.

这篇关于TYPEDEF PTR-大小无关紧要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆