On linux, I use a shell script I call asm-link
, which has a -d
option to assemble + link and then disassemble with objdump -drwC -Mintel
. It's part of my answer on another question.
The script could be ported to Windows using nasm -f win64
or OS X using -f macho64
, perhaps even to DOS if you wanted that.
NASM defaults to the .text
section at the top of the file, and GNU Binutils ld
defaults to putting the ELF entry point at the start of the .text
section if it can't find a _start
symbol, so even linking works with absolutely minimal .asm
source files, and you can even run them in GDB and single-step. cat > foo.asm
... type some and hit CtrlD is one way to create a file, or of course your favourite light-weight editor.
Example use:
$ cat > foo.asm
mov eax, ecx
lea rdi, [rel $]
$ asm-link -dn foo.asm
+ nasm -felf64 -Worphan-labels foo.asm
+ ld -o foo foo.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000
foo: file format elf64-x86-64
Disassembly of section .text:
0000000000401000 <__bss_start-0x1000>:
401000: 89 c8 mov eax,ecx
401002: 48 8d 3d f9 ff ff ff lea rdi,[rip+0xfffffffffffffff9] # 401002 <__bss_start-0xffe>
What I actually type on the second command line is asm<tab> -dn <alt-.>
.
Alt. recalls the last token from previous commands so I don't have to retype the filename. And tab-completion for the command works since my system doesn't have any other commands that start with asm
.
nasm -felf64
(passed by the script, or -f elf32
if you use asm-link -m32
) implies BITS 64
(or BITS 32
) for assembling.
GNU Binutils objdump
's output format is fairly nice for showing the machine code, with addresses that make it easy to see length, and spaces that make it easy to see byte boundaries. (Unlike nasm -l /dev/stdout -f elf64 foo.asm
listings which cram hex digits together.)
The + nasm ...
and + ld
output lines are from bash set -x
. The -n
option to the script uses NASM instead of YASM; back when I originally wrote it, I usually used YASM. I should probably update the default to be NASM.