Skip to content

shiaoquxxx/nasmfmt

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 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 get -u github.com/yamnikov-oleg/nasmfmt. Built binary will be located in your $GOPATH/bin.

About

Formatter for NASM source files

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 90.6%
  • Makefile 9.4%