Skip to content

Instantly share code, notes, and snippets.

@roktas
Last active January 30, 2022 21:05
Show Gist options
  • Save roktas/9a2df3130434538574458a1a4f371bf4 to your computer and use it in GitHub Desktop.
Save roktas/9a2df3130434538574458a1a4f371bf4 to your computer and use it in GitHub Desktop.

Shebang for Assembler

All the credits go to: iximeow (aka pipeline staller)

See the thread for details.

  • Compile nasm-stub.rs

    rustc nasm-stub.rs
  • Install it as /usr/local/bin/nasm-stub

    cp nasm-stub /usr/local/bin/
  • Install binfmt handler

    cp nasm.conf /etc/binfmt.d/

    Note that we install the compiled stub as the handler for ;x08#!

    head -c 4 helloworld.asm | hd
    00000000  3b 08 23 21                                       |;.#!|
    00000004
  • Activate handler

    systemctl restart systemd-binfmt.service
  • Play

    ./helloworld
;#!nasm
BITS 64
section .text
global _start
_start:
mov rax, 1 ; sys_write
mov rdi, 1
lea rsi, [rel msg]
mov rdx, 15 ; "Hello, World!" + newline (0x0a) + null termination
syscall
mov rax, 60 ; sys_exit
mov rdi, 0
syscall
msg:
db "Hello, World!", 0x0a
; vim: ft=nasm
This file has been truncated, but you can view the full file.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment