Advent of Code 2022

GitHub

I'm doing Advent of Code again this year, and like last year, I'm doing it in x86_64 assembly.

The rules for this challenge were:

  1. All of the code for solving the challenge must be written in x86_64 assembly. (I can write other code to check my answers, but I have to get to the same answer in assembly.)
  2. I must parse the input - the path to the input file is given as a command line argument.
  3. I may not hard-code anything about the input; the solution must run for the example input and the actual input without any code changes.

So why am I doing this? It's quite fun to program in assembly - at least for small programs. Also, I'm writing a compiler, and since I'm compiling to x86_64 assembly, it's good to practice writing assembly by hand.

I'm using the nasm assembler to assemble, and the standard Linux ld linker.

Unfortunately, I've been unable to finish this year. Assembly programming with structs was as difficult as I expected, and I did not manage to finish Day 11. I think I'll have to learn how to use more advanced macro-assembler features. I also found that register allocation was difficult. If I attempt this again next year, I'd probably write the assembly code using temporaries as a first pass, before doing register allocation. This might involve use of the pre-processor for register allocation - for example:

#define accumulator rax
#define length rcx
#define start r15
#define temp rdx
#define tempbyte dl

  mov accumulator, 0
  mov temp, 0
.loop:
  mov tempbyte, [start + length]
  add accumulator, temp
  loop .loop