summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile14
-rw-r--r--day01.asm72
-rw-r--r--input/day01.txt1
-rw-r--r--sys.asm50
5 files changed, 138 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d553987
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+day??
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..df4adfd
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+BINS = day01
+
+all: $(BINS)
+
+%: %.o
+	ld -o $@ $<
+
+%.o: %.asm sys.asm
+	nasm -f elf64 -o $@ $<
+
+clean:
+	rm -f $(BINS)
+
+.PHONY: clean
diff --git a/day01.asm b/day01.asm
new file mode 100644
index 0000000..725c9b2
--- /dev/null
+++ b/day01.asm
@@ -0,0 +1,72 @@
+%include "sys.asm"
+global _start
+
+section .bss
+input: resb 1
+
+section .data
+position:
+  .x: dd 0
+  .y: dd 0
+direction:
+  .x: dd 0
+  .y: dd -1
+
+section .text
+_start:
+  .loopTurn:
+    syscall SYS_READ, FD_STDIN, input, 1
+    test rax, rax
+    jz .breakTurn
+
+    cmp byte [input], 'R'
+    jne .elseLeft
+    .thenRight:
+      rol qword [direction], 32
+      neg dword [direction.x]
+      jmp .endLeft
+    .elseLeft:
+      neg dword [direction.x]
+      rol qword [direction], 32
+    .endLeft:
+
+    xor r12, r12
+    .loopDigit:
+      syscall SYS_READ, FD_STDIN, input, 1
+      test rax, rax
+      jz .breakDigit
+      movzx rax, byte [input]
+
+      cmp al, ','
+      je .breakDigit
+
+      shl r12, 1
+      lea r12, [r12 + r12 * 4 - '0']
+      add r12, rax
+    jmp .loopDigit
+    .breakDigit:
+
+    syscall SYS_READ, FD_STDIN, input, 1
+
+    mov eax, r12d
+    imul eax, [direction.x]
+    add [position.x], eax
+    imul r12d, [direction.y]
+    add [position.y], r12d
+  jmp .loopTurn
+  .breakTurn:
+
+  mov eax, [position.x]
+  sar eax, 31
+  mov ecx, eax
+  xor ecx, [position.x]
+  sub ecx, eax
+
+  mov eax, [position.y]
+  sar eax, 31
+  mov edx, eax
+  xor edx, [position.y]
+  sub edx, eax
+
+  add ecx, edx
+syscall SYS_EXIT, rcx
diff --git a/input/day01.txt b/input/day01.txt
new file mode 100644
index 0000000..c08616b
--- /dev/null
+++ b/input/day01.txt
@@ -0,0 +1 @@
+R2, L5, L4, L5, R4, R1, L4, R5, R3, R1, L1, L1, R4, L4, L1, R4, L4, R4, L3, R5, R4, R1, R3, L1, L1, R1, L2, R5, L4, L3, R1, L2, L2, R192, L3, R5, R48, R5, L2, R76, R4, R2, R1, L1, L5, L1, R185, L5, L1, R5, L4, R1, R3, L4, L3, R1, L5, R4, L4, R4, R5, L3, L1, L2, L4, L3, L4, R2, R2, L3, L5, R2, R5, L1, R1, L3, L5, L3, R4, L4, R3, L1, R5, L3, R2, R4, R2, L1, R3, L1, L3, L5, R4, R5, R2, R2, L5, L3, L1, L1, L5, L2, L3, R3, R3, L3, L4, L5, R2, L1, R1, R3, R4, L2, R1, L1, R3, R3, L4, L2, R5, R5, L1, R4, L5, L5, R1, L5, R4, R2, L1, L4, R1, L1, L1, L5, R3, R4, L2, R1, R2, R1, R1, R3, L5, R1, R4
\ No newline at end of file
diff --git a/sys.asm b/sys.asm
new file mode 100644
index 0000000..a7d5295
--- /dev/null
+++ b/sys.asm
@@ -0,0 +1,50 @@
+%macro syscall 0
+  syscall
+%endmacro
+
+%macro syscall 1
+  mov rax, %1
+  syscall
+%endmacro
+
+%macro syscall 2
+  mov rdi, %2
+  syscall %1
+%endmacro
+
+%macro syscall 3
+  mov rsi, %3
+  syscall %1, %2
+%endmacro
+
+%macro syscall 4
+  mov rdx, %4
+  syscall %1, %2, %3
+%endmacro
+
+%macro syscall 5
+  mov r10, %5
+  syscall %1, %2, %3, %4
+%endmacro
+
+%macro syscall 6
+  mov r8, %6
+  syscall %1, %2, %3, %4, %5
+%endmacro
+
+%macro syscall 7
+  mov r9, %7
+  syscall %1, %2, %3, %4, %5, %6
+%endmacro
+
+SYS_READ   equ 0
+SYS_WRITE  equ 1
+SYS_GETPID equ 39
+SYS_EXIT   equ 60
+SYS_KILL   equ 62
+
+FD_STDIN  equ 0
+FD_STDOUT equ 1
+FD_STDERR equ 2
+
+SIG_ABRT equ 6