32 位 MASM 模式与 64 位有何不同? [英] How does 32-bit MASM mode differ from 64-bit?

查看:27
本文介绍了32 位 MASM 模式与 64 位有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要使用 MASM 用 32 位汇编语言编写一个完整的程序,可以这样开始,

To write a complete program in 32-bit assembly language using MASM one may start like this,

.686
.model flat,c
.stack 100h
.data
number sdword 5
.code
main proc
mov eax,number
ret
main endp
end main

而在 64 位模式下,代码编写为

whereas in 64-bit mode the code is written as

.data
number sdword 5
.code
main proc
mov eax,number
ret
main endp
end

设置为默认设置,导致在 64 位模式下组装时出现错误,不是因为平台设置为 Win32,而是默认入口点 mainCRTStartup.要解决此问题,必须在高级链接器选项中将其设置为 main.

The settings are set to default which led to occur an error while assembling in a 64-bit mode not because the platform is set to Win32, but instead the default entry point mainCRTStartup. To fix the problem it must be set to main in Advanced Linker options.

我的问题是,为什么在32位模式下不会遇到这样的问题,mainCRTStartup入口点到底指的是什么?我的第二个问题是,为什么在 64 位模式下我们省略了第一行指令行 .686 .model flat,c .stack 100h 我们不必分配堆栈,以及语言类型?指令 .686 表示,

My question is, why encountering such a problem not happening in 32-bit mode, and what does the mainCRTStartup entry point referring to anyway? My second question is, why assembling in the 64-bit mode we omit the first directive lines .686 .model flat,c .stack 100h don't we have to allocate the stack, as well as the language type? The directive .686 indicates to,

为 Pentium Pro 处理器启用非特权指令的汇编.(仅限 32 位 MASM.)

Enables assembly of nonprivileged instructions for the Pentium Pro processor. (32-bit MASM only.)

为什么在 64 位模式下不是这种情况?

why it's not the case in the 64-bit mode?

推荐答案

32 位 MASM 可以在 16 位和 32 位可执行文件之间进行选择,具体取决于指令.构建 64 位代码时并非如此,因此相关指令较少.

32-bit MASM had a choice between 16-bit and 32-bit executables, depending on the directives. This is not the case when building 64-bit code, so fewer directives are relevant.

x86-64 ISA 保证支持 PPro 指令,因此无需启用它们.(SSE/SSE2 相同).您的代码在 64 位模式下运行的事实意味着它们是可用的,就像 .model flat 中的 movzx 等 386 个功能一样.但是您在 MASM 中选择使用命令行选项而不是指令.

The x86-64 ISA guarantees that PPro instructions are supported so there's no need to enable them. (Same for SSE / SSE2). The fact that your code is running in 64-bit mode means they're available, just like 386 features like movzx in .model flat. But you choose that with command-line options, not directives, in MASM.

MASM 是否没有针对其他后续扩展(例如 .haswell.bmi2)的指令来帮助您避免意外使用较新的指令,例如 AVX2 vpermpd ymm0,ymm1, 0x01 当您只想使用 AVX1 时?其他一些汇编器确实具有类似的功能,例如 YASM 的 CPU 指令,或者 GNU 汇编器可以让您限制功能.(默认情况下,GAS 和 YASM/NASM 接受他们知道的一切.)

Does MASM not have directives for other later extensions like .haswell or .bmi2 to help you avoid using newer instructions by accident, like AVX2 vpermpd ymm0, ymm1, 0x01 when you only meant to use AVX1? Some other assemblers do have features like that, such as YASM's CPU directive, or the GNU assembler can let you restrict features. (By default GAS and YASM/NASM accept everything they knows about.)

x86-64 长模式也强制使用扁平内存模型(CS.base = DS.base = ES.base = SS.base),因此无需选择内存模型.非平面内存分段只能在兼容模式下的 64 位内核下进行(如 32 位或 16 位保护模式的用户空间端).https://en.wikipedia.org/wiki/X86-64#Operating_modes

x86-64 long mode also forces a flat memory model (CS.base = DS.base = ES.base = SS.base) so there's no need to choose a memory model. Non-flat memory segmentation is only possible under a 64-bit kernel in compat mode (like the user-space side of 32-bit or 16-bit protected mode). https://en.wikipedia.org/wiki/X86-64#Operating_modes

此外,MASM .model 也是假设 32 位模式与 16 位模式的代理,这很奇怪.其他汇编程序更正交地将其分开,例如 bits 16/bits 32/bits 64 以设置当前模式.(但在不改变输出文件格式的 NASM 中;这只是让您将 64 位代码放入 32 位目标文件中,除非您正在编写一个切换内核,否则您不想这样做 模式.)

Also, MASM .model was also a proxy for assuming 32-bit mode vs. 16-bit mode, which was weird. Other assemblers separate this more orthogonally, like bits 16 / bits 32 / bits 64 to set the current mode. (But in NASM that doesn't change the output file format; that just lets you put 64-bit code into a 32-bit object file which you don't want to do unless you're writing a kernel that switches modes.)

对于 .stack,主线程堆栈的最大大小由链接器设置(使用 /STACK 命令行选项),而不是汇编器.这取代了让汇编器将元数据放在 .obj 文件中以供链接器查找或实际工作的机制.(AFAIK .stack 无论如何只对 16 位可执行文件很重要,甚至对 .model flat 也不重要.您通常不希望在 32 位代码中使用 256 字节的小堆栈).

For .stack, the max size of the main-thread stack is set by the linker (with the /STACK command-line option), not the assembler. That replaces the mechanism of having the assembler put metadata in the .obj file for the linker to find, or however it actually worked. (AFAIK .stack only ever mattered for 16-bit executables anyway, not even for .model flat. You wouldn't usually want a tiny 256-byte stack in 32-bit code).

这篇关于32 位 MASM 模式与 64 位有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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