rony_t
rony_t

Reputation: 433

Can NASM generate a file with machine code hexdump + asm source?

If I have the following Assembly code:

mov eax, 123
mov ebx, 321

Can NASM generate a file that shows the corresponding machine code for the Assembly code I showed, for example:

F2 FF A3    mov eax, 123
D7 D1 A1    mov ebx, 321

Upvotes: 4

Views: 2501

Answers (1)

Cody Gray
Cody Gray

Reputation: 244732

Yes, NASM can absolutely do this. There are two basic approaches:

  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.

Upvotes: 10

Related Questions