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

查看:102
本文介绍了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位模式运行的事实意味着它们可用,就像386功能一样,例如 .model flat 中的 movzx .但是您可以在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),因此无需选择内存模型.仅在兼容模式下(例如32位或16位保护模式的用户空间侧),才可以在64位内核下进行非固定内存分段. 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天全站免登陆