Skip to content

yamnikov-oleg/nasmfmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nasmfmt

Automatically format your assembly sources with a simple command.

Inspired by gofmt.

What it does?

In one example. This source:

global _start


section .text

   ;Starting point
_start:
mov rax,1 ;write(fd, buf, len)
mov rdi,1  ; fd
mov rsi, msg   ; buf
mov rdx,  msglen; len
  syscall

mov rax,60 ;exit(status)
mov rdi, 0
  syscall

section .data
msg    db "Hello world!",10
msglen equ $-msg

Would be formatted into:

        global _start

        section .text

; Starting point
_start:
        mov rax, 1                     ; write(fd, buf, len)
        mov rdi, 1                     ; fd
        mov rsi, msg                   ; buf
        mov rdx, msglen                ; len
        syscall

        mov rax, 60                    ; exit(status)
        mov rdi, 0
        syscall

        section .data
msg:
        db "Hello world!", 10
        msglen equ $-msg

Features

  • Labels and instructions indentation.
label:
add rax, rbx

Becomes:

label:
        add rax, rbx
  • Comments indentation.
   ; Start of the cycle
cycle:
        inc rcx ; Make it bigger
        jmp cycle ; Because why not?
                  ; Here's no cycle

Becomes:

; Start of the cycle
cycle:
        inc rcx                        ; Make it bigger
        jmp cycle                      ; Because why not?
; Here's no cycle
  • Consistent labels style.
one_style: test rax, rbx
another_style:
        test rax, rbx

one_style:
        db "Message"
another_style db "Message"

Becomes:

one_style:
        test rax, rbx
another_style:
        test rax, rbx

one_style:
        db "Message"
another_style:
        db "Message"
  • Consistent commas style.
        add rax,rbx
        add rax, rbx
        add rax,  rbx

Becomes:

        add rax, rbx
        add rax, rbx
        add rax, rbx
  • No extra spaces and empty lines.
add_func:
        mov    rax, rdi
        add    rax, rsi
        ret



sub_func:
        mov    rax, rdi
        sub    rax, rsi
        ret

Becomes:

add_func:
        mov rax, rdi
        add rax, rsi
        ret

sub_func:
        mov rax, rdi
        sub rax, rsi
        ret

Issues

There might be some issues since I have not tested it on all nasm functionality. Please, report, if you found some.

Would it work with other assemblers? May be, since most assembly syntaxes are similar.

Usage

Usage: nasmfmt [params] [file1 [file2 ...]]
Parameters:
  -ci int
        Indentation for comments in spaces (default 40)
  -ii int
        Indentation for instructions in spaces (default 8)

Examples:

  • nasmfmt main.asm funcs1.asm funcs2.asm
  • nasmfmt -ii 4 main.asm
  • nasmfmt -ii 4 -ci 36 main.asm

Installing

Visit Releases to download a binary for your platform.

Building

Building requires latest version of golang from golang.org.

If you installed one, run go install github.com/yamnikov-oleg/nasmfmt@latest. Built binary will be located in your $GOPATH/bin.