NASM可以生成带有机器码十六进制转储和汇编源代码的文件吗?

3
如果我有以下汇编代码:
mov eax, 123
mov ebx, 321

NASM是否能生成一个文件,该文件显示所示汇编代码的相应机器代码,例如:
F2 FF A3    mov eax, 123
D7 D1 A1    mov ebx, 321

nasm <source> 是最简单的命令行。如果需要,您可以指定输出文件名。ndisasm -b <size> <binary> 将以与您列出的方式相同输出反汇编。 - Margaret Bloom
2
你要求NASM的解决方案,Cody和Margaret已经提供了。但是也有一个在线解决方案,你可以直接粘贴汇编代码以查看代码字节。对于简单的代码片段非常有用。 - David Wohlferd
好建议,@David!我一直在使用那个工具。但是SO上关于16位汇编的大量问题让我希望它也支持这种模式。或者SO上有更少的16位汇编问题...嗯 - Cody Gray
@CodyGray:呃,16位。在回答这些问题时学习了足够多的16位垃圾后,我最终决定回答它们并不有趣,大多数时候我甚至都不看它们。 - Peter Cordes
1个回答

9

是的,NASM绝对可以做到这一点。有两种基本方法:

  1. Have NASM generate a "listing" file as it assembles your code.

    To do this, pass the -l option on the command line when invoking NASM. If you like, you can specify an optional file name (it is conventional to use the .lst extension, but not required):

    nasm -f <format> SourceFile.asm -l ListingFile.lst
    

    A "listing" file displays the addresses and code bytes on the left, with the assembly mnemonics on the right. It also contains expansions of multi-line macros (except for those that have been defined with the .nolist qualifier).

    This does not inhibit assembly (the normal object file output is still generated), so you can just turn this option on in your Makefile and leave it.

    Here's an example of a listing file for a very simple source file:

    1 00000000 B87B000000              mov eax, 123
    2 00000005 BB41010000              mov ebx, 321
    3 0000000A CD80                    int 0x80
    4 0000000C C3                      ret
    

    The first column is the line number from the source code, the second column is the address/offset, and the third column is the binary value (for instructions, these are the machine code bytes; for data, this will be the raw binary data). The fourth, right-most column are the actual instruction mnemonics, as appear in your source code.

    Notice that the MOV instructions do not map to the machine code that is shown in the question… I don't know where you got those values. Maybe you just made them up?

  2. Disassemble the object file or binary generated by NASM.

    Basically, you run the assembler to generate the output file, and then you run that back through a disassembler. NASM comes with a dissembler, called NDISASM. The syntax is:

    ndisasm -b {16|32|64} filename
    

    where the -b option specifies the bitness of the file, which affects how the bytes are decoded into mnemonics. NDISASM defaults to 16-bit, but you will probably want 32-bit or 64-bit.

    There are some other options that you can read about in the above-linked documentation. These often come in handy, like specifying an origin for a COM file (-o), specifying a sync point to ignore data (-s), and skipping a header of a certain size (-e).

    Here's an example of output from NDISASM:

    00000000  B87B000000        mov eax,0x7b
    00000005  BB41010000        mov ebx,0x141
    0000000A  CD80              int 0x80
    0000000C  C3                ret
    

    (No line numbers here, because the source code isn't used. It's just disassembling a binary, the same as you could do for any binary on your machine, whether or not you had the original source code.)

    Notice that NDISASM will print its output to stdout. You will likely want to redirect it to a file. How exactly you do that depends on which operating system you are using; consult your command interpreter's documentation for instructions.


请注意,MOV指令与问题中显示的机器码不匹配...我不知道你从哪里得到这些值。也许你只是编造了它们?是的,我编造了它们 :) - rony_t

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接