Skip to content

Instantly share code, notes, and snippets.

@waruqi
Forked from jlettvin/assembler.md
Created April 23, 2018 14:18
Show Gist options
  • Save waruqi/08f01e21495b4a82724bd009e672f738 to your computer and use it in GitHub Desktop.
Save waruqi/08f01e21495b4a82724bd009e672f738 to your computer and use it in GitHub Desktop.

Comparing assembler syntax

A variety of assemblers are available for programming on Intel machines. In this gist, commonalities and differences between them are illustrated. This exploration does not expose the ability of most assemblers to lex/parse different code syntax such as Intel or AT&T, and variations for 32 and 64 bit. Examples of some syntax variations are shown to illustrate. Effort has been taken to reduce the syntax differences to only those absolutely necessary. Lines of the same number have the same logical content in all three files.

+ --------- + ------------ + ------- + ------ + -------- +
| Assembler |   Platform   | Version | Status | exe size |
+ --------- + ------------ + ------- + ------ + -------- +
|     clang | Apple LLVM   |     6.0 |  Ready |     8276 |
|      nasm | Apple bld 12 | 0.98.40 |  Ready |     8276 |
|      yasm | Apple        |   1.3.0 |  Ready |     8288 |
+ --------- + ------------ + ------- + ------ + -------- +

A variety of platforms are available for Intel machines. This hw39 repository is in its infancy, and only Mac OS X 10.7.0 is represented. Eventually we will show that the same code may be compiled on these and other platforms too.

+ ---------- + -------- + --------- + ------ +
|   Makefile |    OS    |  Version  | Status |
+ ---------- + -------- + --------- + ------ +
|  Darwin.mk | Mac OS X |    10.7.0 |  Ready |
|   Linux.mk |   ubuntu | 14.04 LTS | Absent |
| Windows.mk |  Windows |  none yet | Absent |
+ ---------- + -------- + --------- + ------ +

Links to full repository and assembler source files discussed.

Organization for hw (hello world) source files.

All three files were designed to have identical outlines for represented assemblers.

  • Header comment showing command-lines to produce executable;
  • _main entrypoint global declaration;
  • Definition of stdout;
  • Macro for outputting a string;
  • .data section containing "hello world!\n";
  • .text (code) section containing _main entrypoint.

All files except LICENSE fit into a 39x39 character block. This is the unfortunate consequence of the author being both a poet and an assembler.

Mac OS X 10.7.0

hw_clang.asm: Using the clang assembler on Mac OS X

// clang -c -arch i386 \
//   -o hw_clang.o hw_clang.asm
// ld -macosx_version_min 10.7.0 \
//   -o hw_clang hw_clang.o \
//   -e _main -no_pie -arch i386
//_____________________________________
.globl _main

.equ    stdout, 1

.macro  write msg, len
    push \len
    push \msg
    push $stdout
    movl $4, %eax
    call _syscall
    addl $12, %esp
.endm

//_____________________________________
.data

msg:    .string "Hello World!\n"
.equ    len, .-msg

.text

//_____________________________________
_main:
    write $msg, $len    // OUTPUT

    push $0
    movl $1, %eax
    call _syscall       // EXIT

_syscall:
    int $0x80
    ret
//_____________________________________

hw_nasm.asm: Using the nasm assembler on Mac OS X

; nasm -f macho \
;   -o hello_nasm.o   hello_nasm.asm
; ld -macosx_version_min 10.7.0 \
;   -o hello_nasm hello_nasm.o \
;   -e _main
;______________________________________
global _main

stdout  equ 1

%macro  write 2
     push    dword %2
     push    dword %1
     push    dword stdout
     mov     eax, 0x4
     call    _syscall
     add     esp, 12
%endmacro

;______________________________________
section .data

msg     db      "Hello, world!",0xa
len     equ     $-msg

section     .text

;______________________________________
_main:
     write msg, len     ; OUTPUT

     push    dword 0
     mov     eax, 0x1
     call    _syscall   ; EXIT

_syscall:           
     int     0x80
     ret
;______________________________________

hw_yasm.asm: Using the yasm assembler on Mac OS X

; yasm -f macho64 -DDARWIN \
;   -o hw_yasm.o hw_yasm.asm
; ld -macosx_version_min 10.7.0 \
;   -o hw_yasm hw_yasm.o \
;   -e _main
;______________________________________
global _main

%define stdout 1

%macro  write 2
    mov     rdx, %2
    mov     rsi, qword %1
    mov     rdi, stdout
    mov     rax, 0x2000000 + 4
    syscall

%endmacro

;______________________________________
section .data

msg:    db  'hello, world!',0xa
len     equ $-msg

section .text

;______________________________________
_main:
    write msg, len  ; OUTPUT

    mov     rax, 0x2000000 + 1
    xor     rdi, rdi
    syscall         ; EXIT

# syscall is builtin


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