A small Linux command-line ROT13 filter. It uses Linux system calls directly and does not link the C standard library.
makeThis runs nasm -f elf64 and links the object with ld.
./rot13 # read stdin, write stdout
printf 'Hello, World!\n' | ./rot13
./rot13 input.txt # read a file, write stdout
./rot13 -o output.txt input.txt # read a file, write a file
./rot13 -o output.txt < input.txt-o takes an output path. A single input path is optional; without one, input comes from stdin. Alphabetic ASCII bytes rotate by 13; all other bytes pass through unchanged. Open/read/write errors produce a message on stderr and a nonzero exit status.
printf 'Hello, World! 123\n' | ./rot13
printf 'Hello, World!\n' > /tmp/rot13-in
./rot13 -o /tmp/rot13-once /tmp/rot13-in
./rot13 -o /tmp/rot13-twice /tmp/rot13-once
cmp /tmp/rot13-in /tmp/rot13-twiceFor the round trip, ROT13 is its own inverse, so applying it twice restores the original bytes. cmp exits successfully when the files match.
At _start, the program reads argc and argv from the initial stack. It keeps the input and output file descriptors in r14 and r15; r14 defaults to fd 0 (stdin), and r15 defaults to fd 1 (stdout). r12/r13 retain argc/argv while the arguments are parsed. Each Linux syscall puts its number in rax, arguments in rdi, rsi, rdx (then r10, r8, r9 if needed), and returns its result in rax. Negative results indicate errors.
The read loop uses sys_read to fill a 4096-byte buffer, calls rot13_buffer, then loops on sys_write until the whole chunk is written. Input and output paths are opened with sys_openat relative to AT_FDCWD; files opened by the program are closed with sys_close. Errors are written to fd 2 and the process ends with sys_exit.