NASM mode for Emacs
Quick post for those Emacs users out there.
The common assembler used on GNU/Linux nowadays is the GAS assembler, part of the GNU Compiler Collection (GCC). If, like me, you get upset with the AT&T syntax and prefer working with a real assembler (and not a just a compiler back-end), you might want to give NASM try.
If you, however, are also a Emacs user, there’s a problem: Emacs assembly mode only supports the GAS syntax, there’s no nasm-mode. Quite disturbing...
So here’s a link to my NASM mode: nasm-mode.el
;; Major mode for nasm assembly
;; Made by Matthieu Hauglustaine ;; Put the following code in your .emacs to use nasm-mode
;;
;; (autoload 'nasm-mode "~/.emacs.d/nasm-mode.el" "" t)
;; (add-to-list 'auto-mode-alist '("\\.\\(asm\\|s\\)$" . nasm-mode)) ;; To set your own indentation level to LEVEL:
;;
;; (add-hook 'nasm-mode-hook
;; (lambda () (setq-default nasm-basic-offset LEVEL))) (defvar nasm-mode-hook nil)
(defcustom nasm-basic-offset 8 "Indentation level.") ;; Use make-sparse-keymap if keymap have very few entries.
(defvar nasm-mode-map
(let ((kmap (make-keymap)))
(define-key kmap (kbd "C-j") 'newline-and-indent)
kmap)
"Keymap for nasm major mode") (defconst nasm-font-lock-keywords
(list
;; Labels
'("^[ \t]*[a-zA-Z0-9_.?][a-zA-Z0-9_$#@~.?]*:" . font-lock-type-face)
;; Directive operands and registers
'("\\<\\(\\$\\$?\\|%[0-9]\\|\\.\\(?:bss\\|data\\|text\\)\\|a\\(?:16\\|32\\|[hlx]\\)\\|b\\(?:yte\\|[hlpx]\\)\\|c\\(?:r[0234]\\|[hlsx]\\)\\|d\\(?:r[0-367]\\|word\\|[hilsx]\\)\\|e\\(?:ax\\|b[px]\\|cx\\|d[ix]\\|s[ip]?\\)\\|f\\(?:ar\\|lat\\|s\\)\\|gs\\|large\\|mm[0-7]\\|n\\(?:ear\\|osplit\\)\\|o\\(?:16\\|32\\)\\|s\\(?:eq\\|mall\\|t[0-7]\\|[ipst]\\)\\|tr[3-7]\\|w\\(?:ord\\|rt\\)\\|xmm[0-7]\\)\\>" . font-lock-variable-name-face)
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\)\\|us[bw]\\|[bdqw]\\)\\|wapd\\)\\|u\\(?:npck\\(?:h\\(?:bw\\|dq\\|qdq\\|wd\\)\\|l\\(?:bw\\|dq\\|qdq\\|wd\\)\\)\\|sh\\(?:a[dw]\\|f[dw]\\|[adfw]\\)?\\)\\|xor\\)\\|r\\(?:c\\(?:p\\(?:[ps]s\\)\\|[lr]\\)\\|d\\(?:msr\\|pmc\\|shr\\|tsc\\)\\|e\\(?:p\\(?:n[ez]\\|[ez]\\)\\|t[fn]\\|[pt]\\)\\|o[lr]\\|s\\(?:dc\\|ldt\\|m\\|\\(?:qrt[ps]\\|t\\)s\\)\\)\\|s\\(?:a\\(?:hf\\|lc\\|[lr]\\)\\|bb\\|cas[bdw]?\\|et\\(?:ae\\|be\\|ge\\|le\\|n\\(?:[abgl]e\\|[abceglopsz]\\)\\|p[eo]\\|[abceglopsz]\\)\\|fence\\|gdt\\|h\\(?:ld\\|rd\\|ufp[ds]\\|[lr]\\)\\|idt\\|ldt\\|m\\(?:i\\(?:nt\\(?:old\\)?\\)?\\|sw\\)\\|qrt\\(?:p[ds]\\|s[ds]\\)\\|t\\(?:mxcsr\\|os[bdw]?\\|[cdir]\\)\\|ub\\(?:p[ds]\\|s[ds]\\)?\\|v\\(?:dc\\|ldt\\|ts\\)\\|ys\\(?:call\\|e\\(?:nter\\|xit\\)\\|ret\\)\\)\\|test\\|u\\(?:comis[ds]\\|d[012]\\|mov\\|npck\\(?:hp[ds]\\|lp[ds]\\)\\)\\|ver[rw]\\|w\\(?:ait\\|binvd\\|r\\(?:\\(?:ms\\|sh\\)r\\)\\)\\|x\\(?:add\\|bts\\|chg\\|latb?\\|or\\(?:p[ds]\\)?\\)\\)\\>" . font-lock-keyword-face)
;; Directives
'("\\<\\(%\\(?:a\\(?:rg\\|ssign\\)\\|define\\|e\\(?:l\\(?:if\\(?:ctk\\|def\\|id\\(?:ni?\\)?\\|macro\\|n\\(?:ctk\\|def\\|id\\(?:ni?\\)?\\|macro\\|num\\|str\\|um\\)\\|str\\)?\\|se\\)\\|nd\\(?:if\\|macro\\|rep\\)\\|rror\\|xitrep\\)\\|i\\(?:assign\\|define\\|f\\(?:ctk\\|def\\|id\\(?:ni?\\)?\\|macro\\|n\\(?:ctk\\|def\\|id\\(?:ni?\\)?\\|macro\\|num\\|str\\|um\\)\\|str\\)?\\|macro\\|nclude\\)\\|l\\(?:ine\\|ocal\\)\\|macro\\|p\\(?:op\\|ush\\)\\|r\\(?:epl?\\|otate\\)\\|s\\(?:t\\(?:acksize\\|rlen\\)\\|ubstr\\)\\|undef\\|x\\(?:i?define\\)\\)\\|\\.nolist\\|a\\(?:bsolute\\|lignb?\\|t\\)\\|bits\\|c\\(?:ommon\\|pu\\)\\|d[bdqtw]\\|e\\(?:n\\(?:d\\(?:\\(?:pro\\|stru\\)c\\)\\|try\\)\\|qu\\|x\\(?:port\\|tern\\)\\)\\|g\\(?:lobal\\|roup\\)\\|i\\(?:end\\|mport\\|ncbin\\|struc\\)\\|org\\|proc\\|res[bdqtw]\\|s\\(?:ection\\|truct\\)\\|times\\|use\\(?:16\\|32\\|64\\)\\)\\>" . font-lock-preprocessor-face)
)
"Highlight the registers") (defvar nasm-mode-syntax-table
(let ((stable (make-syntax-table)))
(modify-syntax-entry ?_ "w" stable)
(modify-syntax-entry ?. "w" stable)
(modify-syntax-entry ?\; "<" stable) ; Comment starter
(modify-syntax-entry ?\n ">" stable) ; Comment ender
(modify-syntax-entry ?\" "\"" stable) ; String quote
(modify-syntax-entry ?\' "\"" stable) ; String quote
stable)
"Syntax table for nasm-mode") (defun nasm-set-offset offset
(setq nasm-basic-offset offset)) (defun nasm-indent-line ()
"Indent current line as nasm assembly code."
(interactive)
(beginning-of-line)
(if (or (looking-at "^[ \t]*\\(%\\(?:a\\(?:rg\\|ssign\\)\\|define\\|e\\(?:l\\(?:if\\(?:ctk\\|def\\|id\\(?:ni?\\)?\\|macro\\|n\\(?:ctk\\|def\\|id\\(?:ni?\\)?\\|macro\\|num\\|str\\|um\\)\\|str\\)?\\|se\\)\\|nd\\(?:if\\|macro\\|rep\\)\\|rror\\|xitrep\\)\\|i\\(?:assign\\|define\\|f\\(?:ctk\\|def\\|id\\(?:ni?\\)?\\|macro\\|n\\(?:ctk\\|def\\|id\\(?:ni?\\)?\\|macro\\|num\\|str\\|um\\)\\|str\\)?\\|macro\\|nclude\\)\\|l\\(?:ine\\|ocal\\)\\|macro\\|p\\(?:op\\|ush\\)\\|r\\(?:epl?\\|otate\\)\\|s\\(?:t\\(?:acksize\\|rlen\\)\\|ubstr\\)\\|undef\\|x\\(?:i?define\\)\\)\\|\\.nolist\\|a\\(?:bsolute\\|lignb?\\|t\\)\\|bits\\|c\\(?:ommon\\|pu\\)\\|d[bdqtw]\\|e\\(?:n\\(?:d\\(?:\\(?:pro\\|stru\\)c\\)\\|try\\)\\|qu\\|x\\(?:port\\|tern\\)\\)\\|g\\(?:lobal\\|roup\\)\\|i\\(?:end\\|mport\\|ncbin\\|struc\\)\\|org\\|proc\\|res[bdqtw]\\|s\\(?:ection\\|truct\\)\\|times\\|use\\(?:16\\|32\\|64\\)\\)")
(looking-at "^[ \t]*[a-zA-Z0-9_.?][a-zA-Z0-9_$#@~.?]*:")
(looking-at "^[ \t]*;"))
(indent-line-to 0) ; line is a directive, a label or a comment
(indent-line-to nasm-basic-offset))) (defun nasm-mode ()
"Major mode for editing nasm assembler source code."
(interactive)
(kill-all-local-variables) (set-syntax-table nasm-mode-syntax-table)
(use-local-map nasm-mode-map)
(set (make-local-variable 'font-lock-defaults) '(nasm-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'nasm-indent-line) (setq major-mode 'nasm-mode)
(setq mode-name "Nasm")
(setq comment-start ";")
(run-hooks 'nasm-mode-hook)) (provide 'nasm-mode)
Please give it a try and don't hesitate to report any bug or missing feature!
All you have to do is put nasm-mode.el in the your load path (usually in ~/.emacs.d/) and add the following lines in your .emacs file:
(autoload 'nasm-mode "~/.emacs.d/nasm-mode.el" "" t)
(add-to-list 'auto-mode-alist '("\\.\\(asm\\|s\\)$" . nasm-mode))
See yah
Posted by Matthieu Hauglustaine at 5:27 AM