Skip to content

Instantly share code, notes, and snippets.

@wsgac
Last active March 3, 2026 04:25
Show Gist options
  • Select an option

  • Save wsgac/f5ca3a7fcc4c284456e1c0ef6815acfc to your computer and use it in GitHub Desktop.

Select an option

Save wsgac/f5ca3a7fcc4c284456e1c0ef6815acfc to your computer and use it in GitHub Desktop.
Insert a file-local propline giving sensible assembly and linker commands to `M-x compile` on Mac OS X
(defconst *nasm-formats*
'(bin
ith
srec
aout
aoutb
coff
elf32
elf64
elfx32
as86
obj
win32
win64
rdf
ieee
macho32
macho64
dbg
elf
macho
win)
"List of output formats available in NASM.")
(defun nasm-guess-format ()
"Try to obtain the correct NASM format by looking at
SYSTEM-TYPE. Signal error when system not recognized."
(cond ((string-equal system-type "darwin") "macho64")
((string-equal system-type "gnu/linux") "elf64")
((string-equal system-type "berkeley-unix") "elf64")
((string-equal system-type "windows-nt") "win64")
(t (error "Operating system not recognized: %s. Select NASM format manually." system-type))))
(defun nasm-insert-propline (&optional arg)
"Insert a propline defining two file-local variables: MODE
equal to NASM (so that NASM-MODE is associated to the file) and
COMPILE-COMMAND, which specifies a sensible behavior for the M-x
compile command. When run with a non-NIL prefix argument, ask to
choose a format from available NASM formats, otherwise attempt to
guess the most suitable one."
(interactive "P")
(let* ((get-nasm-format (and (consp arg) (zerop (mod (car arg) 16))))
(get-linker (and (consp arg) (zerop (mod (car arg) 4))))
(full-filename (file-name-nondirectory (buffer-file-name)))
(filename (file-name-base full-filename))
(linker (if get-linker
(completing-read "Linker program: " '(ld gcc clang) nil t nil)
"ld"))
(nasm-format (if get-nasm-format
(completing-read "NASM format: " *nasm-formats* nil t (nasm-guess-format))
(nasm-guess-format)))
(linker-libraries (cond ((string-equal system-type "darwin")
" -lSystem")
(t ""))))
(delete-file-local-variable-prop-line 'mode)
(delete-file-local-variable-prop-line 'compile-command)
(add-file-local-variable-prop-line 'mode 'nasm)
(add-file-local-variable-prop-line
'compile-command
(concat
(format "nasm -f%s %s.asm" nasm-format filename )
" && "
(format "%s%s -o %s %s.o" linker linker-libraries filename filename)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment