#!/bin/sh # -*- mode: org -*- # I was thinking folding-mode might provide a neat way to make a # non-subroutine-factored assembly program readable in Emacs, but # C-c @ C-q just is not a a usable keybinding. Org-mode is more # reasonable but its asterisks are not valid as assembly comments. # But packaging this as a shell script that assembles the rest of # the file, minus the org-mode headers, does work: sed 's/^\*.*//' <<'EOF' | as -o folded.o && ld folded.o -o folded && ls -l folded * Preliminary setup for gas and whatnot # These persuade gas to produce error messages pointing to the correct line number, # a real time saver. Note that the line number must be edited if you add any code # above this line, and without .file the .line directive fails to work: .file "folded.s" .line 20 * System calls ** System call numbers .equiv __NR_exit, 1 ** System call macros *** sys1 .macro sys1 call_no, a be \a, %ebx sys0 \call_no .endm *** sys0 .macro sys0 call_no be \call_no, %eax ## There's a new, faster instruction for system calls, but I ## don't know how to use it yet. int $0x80 .endm ** `be`: Peephole-optimization of setting a register ### Set dest = src. Usually just `mov src, dest`, but sometimes ### there's a shorter way. .macro be src, dest .ifnc \src,\dest .ifc \src,$0 xor \dest,\dest .else .ifc \src,$1 xor \dest,\dest inc \dest .else .ifc \src,$2 xor \dest,\dest inc \dest inc \dest .else mov \src, \dest .endif .endif .endif .endif .endm * Main program .globl _start _start: sys1 $__NR_exit, $42 # exit code 42