如何知道汇编代码是否具有特定语法(emu8086,NASM,TASM等)? [英] How to know if an assembly code has particular syntax (emu8086, NASM, TASM, ...)?
问题描述
我想知道通过查看示例源代码如何识别所使用的语法是em8086,TASM还是NASM?我是组装的新手..我想了解更多关于emu8086的信息.
I want to know how,by looking through a sample source code, recognise if the syntax used is em8086, TASM or NASM? I am a new to assembly..I would like to know more about emu8086 please.
推荐答案
NASM/YASM很容易与MASM/TASM/emu8086区分开. YASM使用NASM语法,在接受常量和指令方面稍有不同.
NASM/YASM is easy to distinguish from MASM/TASM/emu8086. YASM uses NASM syntax, with a few minor differences in what it accepts for constants and directives.
我不知道如何区分MASM和TASM,或者TASM和emu8086或FASM,所以我将其留给其他答案.
I don't know how to distinguish MASM from TASM, or TASM from emu8086, or FASM, so I'll leave that for another answer to address.
在NASM中,诸如内存操作数之类的显式大小使用dword
或byte
.在TASM/MASM样式中,您必须编写dword ptr
或byte ptr
.
In NASM, explicit sizes on things like memory operands use dword
or byte
. In TASM/MASM style, you have to write dword ptr
or byte ptr
.
在MASM(我认为是TASM/emu8086)中,裸露的符号名称是指内容.您必须使用offset foo
来获取foo的地址.在NASM中,必须使用[foo]
创建内存操作数,而foo
是地址.
In MASM (and I think TASM/emu8086), a bare symbol name referes to the contents. You have to use offset foo
to get the address of foo. In NASM, you have to use [foo]
to create a memory operand, and foo
is the address.
语法上也可能存在其他差异(例如,段覆盖中的差异),但是通过查看某些内容是NASM风格还是MASM风格,这些差异就足够了.
There are probably other differences in syntax, too (e.g. in segment overrides), but these should be enough to tell by looking whether something is NASM-style or MASM-style.
NASM :
global foo
foo: ; a function called foo()
add dword [ecx], 2
add dword [counter], 1 ; Error without "dword", because neither operand implies an operand-size for the instruction. And the [] is required.
mov eax, [static_var]
mov eax, [static_array + ecx*4] ; Everything *must* be inside the []
mov esi, static_var ; mov esi,imm32 with the address of the static_var
ret
section .data
static_var: dd 0xdeadbeef ; NASM can use 0x... constant. MASM only allows 0DEADBEEFh style
section .bss
counter: resd 1 ; reserve space for one dword (initialized to zero)
buf: resb 256 ; reserve 256 bytes
请注意此处的标签名称后的:
,即使是数据也是如此.建议这样做,但不是必需的:假定行首的任何未知标记都是标签,因此counter resd 1
会组合在一起.但是loop resd 1
不会,因为loop
是有效的指令助记符.
Note the :
after label names here, even for data. This is recommended but not required: any unknown token at the start of a line is assumed to be a label so counter resd 1
will assemble. But loop resd 1
won't because loop
is a valid instruction mnemonic.
MASM/TASM (我可能有一些错误,我没有使用MASM或TASM):
MASM/TASM (I may have some of this wrong, I don't use MASM or TASM):
GNU GAS .intel_syntax noprefix
基本相同,但没有标签的魔术操作数大小关联. GAS指令/伪指令完全不同,例如.byte 0x12
与db 12h
.
.CODE
foo PROC ; PROC/ENDP definitely means not NASM
add dword ptr [ecx], 2
add counter, 1 ; operand-size magically implied by the dd after the counter label. [] is optional
mov eax, static_var ; mov eax, [static_var] is the same, and recommended by some for clarity
mov eax, static_array[ecx*4] ; [ static_array + ecx*4 ] is also allowed, but not required.
mov esi, OFFSET static_var ; mov esi,imm32 with the address.
ret
ENDP
.data ; no SECTION directive, just .data directly
static_var dd 0deadbeefH
;;; With a : after the name, it would be just a label, not a "variable" with a size associated.
.bss
; (In most OSes, the BSS is initialized to zero. I assume MASM/TASM allows you to write dd 0 in the BSS, but I'm not sure)
counter: dd 0 ; reserve space for one dword (zeroed)
buf db 256 dup(?) ; reserve 256 bytes (uninitialized).
除非我另有说明,否则这些差异中的任何一个都可以肯定地表明它是NASM/YASM或MASM/TASM/emu8086
Except where I commented otherwise, any of these differences are a guaranteed sign that it's NASM/YASM or MASM/TASM/emu8086
例如如果您看到裸符号作为目标操作数(例如mov foo, eax
),则绝对不是NASM,因为mov imm32, r32
没有任何意义. 除非符号实际上是寄存器的宏定义,例如%define result eax
将允许mov result, 5
. (
e.g. if you ever see a bare symbol as the destination operand (e.g. mov foo, eax
), it's definitely not NASM, because mov imm32, r32
makes no sense. Unless the symbol is actually a macro definition for a register, e.g. %define result eax
would allow mov result, 5
. (Good catch, @MichaelPetch). If the source is full of macros, then look for the defs. %define
means NASM, while MACRO
means MASM/TASM.
MASM/TASM没有resb
/resd
指令.相反,它们具有count DUP(value)
,其中值可以为?
.
MASM/TASM doesn't have resb
/ resd
directives. Instead, they have count DUP(value)
, where value can be ?
.
NASM具有times 30 db 0x10
来重复该字节0x10
30次.您可以在任何内容上使用它,甚至可以在说明中使用它.它还具有%rep
指令来重复一个块.
NASM has times 30 db 0x10
to repeat the byte 0x10
30 times. You can use it on anything, even instructions. It also has %rep
directives to repeat a block.
MASM和NASM具有重要的宏功能,但是它们使用不同的语法.
MASM and NASM have significant macro capabilities, but they use different syntax.
x86 标记维基的问题具有指向汇编程序的链接手册等等.
The x86 tag wiki has links to assembler manuals and much more.
- NASM
- MASM
- TASM version 5 user's guide
- GNU
as
, aka gas
这篇关于如何知道汇编代码是否具有特定语法(emu8086,NASM,TASM等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!