summary refs log tree commit diff homepage
path: root/day01.asm
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-12-02 03:47:42 -0500
committerJune McEnroe <june@causal.agency>2016-12-02 03:47:42 -0500
commit062d8cf0837e1c321e89675e841a0c5849510555 (patch)
tree850c3108b7502371143ffd4e9e5b8dd5e7b1a402 /day01.asm
parentDay 1 (diff)
downloadaoc-062d8cf0837e1c321e89675e841a0c5849510555.tar.gz
aoc-062d8cf0837e1c321e89675e841a0c5849510555.zip
Rewrite day 1 solution
Diffstat (limited to 'day01.asm')
-rw-r--r--day01.asm111
1 files changed, 54 insertions, 57 deletions
diff --git a/day01.asm b/day01.asm
index 725c9b2..8ad4c66 100644
--- a/day01.asm
+++ b/day01.asm
@@ -1,72 +1,69 @@
 %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
+%define rPosX r8
+%define rPosY r9
+%define rDirX r10
+%define rDirY r11
 
-section .text
+global _start
 _start:
-  .loopTurn:
-    syscall SYS_READ, FD_STDIN, input, 1
-    test rax, rax
-    jz .breakTurn
+  sub rsp, 4096
+  syscall SYS_READ, FD_STDIN, rsp, 4096
+  mov rdi, rax ;; Length.
+  xor rcx, rcx ;; Index.
 
-    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 rPosX, rPosX
+  xor rPosY, rPosY
+  xor rDirX, rDirX
+  mov rDirY, -1 ;; North.
 
-    xor r12, r12
-    .loopDigit:
-      syscall SYS_READ, FD_STDIN, input, 1
-      test rax, rax
-      jz .breakDigit
-      movzx rax, byte [input]
+  .loopTurn:
+    cmp byte [rsp + rcx], 'L'
+    jne .right
+    .left: ;; L(dx, dy) = (dy, -dx)
+      neg rDirX
+      jmp .swap
+    .right: ;; R(dx, dy) = (-dy, dx)
+      neg rDirY
+    .swap:
+    xchg rDirX, rDirY
 
-      cmp al, ','
-      je .breakDigit
+    xor rax, rax
+    .loopDigit:
+      inc rcx
+      movzx rdx, byte [rsp + rcx]
+      cmp dl, '0'
+      jb .breakDigit
 
-      shl r12, 1
-      lea r12, [r12 + r12 * 4 - '0']
-      add r12, rax
+      ;; rax = rax * 10 + rdx - '0'
+      shl rax, 1
+      lea rax, [rax + rax * 4 - '0']
+      add rax, rdx
     jmp .loopDigit
     .breakDigit:
+    add rcx, 2 ;; Discard comma and space.
+
+    mov rdx, rax
+    imul rdx, rDirX
+    add rPosX, rdx
 
-    syscall SYS_READ, FD_STDIN, input, 1
+    mov rdx, rax
+    imul rdx, rDirY
+    add rPosY, rdx
 
-    mov eax, r12d
-    imul eax, [direction.x]
-    add [position.x], eax
-    imul r12d, [direction.y]
-    add [position.y], r12d
-  jmp .loopTurn
-  .breakTurn:
+  cmp rcx, rdi
+  jb .loopTurn
 
-  mov eax, [position.x]
-  sar eax, 31
-  mov ecx, eax
-  xor ecx, [position.x]
-  sub ecx, eax
+  ;; abs(x) = (x ^ (x >> 63)) - (x >> 63)
+  mov rax, rPosX
+  sar rax, 63
+  xor rPosX, rax
+  sub rPosX, rax
 
-  mov eax, [position.y]
-  sar eax, 31
-  mov edx, eax
-  xor edx, [position.y]
-  sub edx, eax
+  mov rax, rPosY
+  sar rax, 63
+  xor rPosY, rax
+  sub rPosY, rax
 
-  add ecx, edx
-syscall SYS_EXIT, rcx
+  lea rax, [rPosX + rPosY]
+syscall SYS_EXIT, rax