summary refs log tree commit diff homepage
path: root/day01.asm
blob: 725c9b2b5c8fcc0837ceb6579657a85c66f43f88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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