urlencode

When I first started making the sept page I noticed that the default markdown handler, markdown.awk, wasn’t able to handle the special characters (mostly the spaces) in the URL, so I needed a program to replace them with the character code (e.g. %20 for a space). The urlencode.awk that came with werc changed spaces to + signs, which wasn’t correct for my use-case, so I decided to write my own.

The first thing I tried was a simple C program, using syscalls from unistd.h:

I noticed that the binary was a whole 18KB! Way too much for such a simple program, surely? So I decided to do away with libc, I was already using the syscalls, after all. Just had to write a simple entry point in assembly:

This brought the executable size down to just 1.2KB, very cool! But it could be better, why use C at all? Why not just write the whole thing in assembly? I spent a few hours learning the basics of x86_64 assembly programming (some of the resources I used are listed here), so this most certainly isn’t the most optimal, nor the prettiest, but it’s small and it gets the job done:

This is the tiniest (native executable) at only 263 bytes! This is what currently drives the sept page. It’s optimized to the extent of my assembly knowledge (addition in mov instructions, and xor instead of mov register 0), and it gets the job done. This is the first actual (non-“Hello, world!”) program I ever made in assembly, so I’m honestly just proud it works at all. I do believe that there are many things that could be improved, but that’s an exercise for future me.

Honestly, I think one of the biggest improvements in file size came from the assembly version’s lack of a section header, but I hope my small optimizations helped a bit.

Since I was having so much fun working on this, I decided to also write a uxn version. This one weighs in at only 64 bytes, nice! It does require the overhead of the uxn emulator (39KB on my system), sadly.

Making small binaries is really fun, and I learned a decent bit about x86_64 assembly through this small project. 1010, would get sidetracked again.