summaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2022-12-02 19:39:24 +0100
committerBond_009 <bond.009@outlook.com>2022-12-02 19:39:24 +0100
commite744ce1f1703309cd95e617fea816a07a082902f (patch)
tree6927c69e80874522929c01043bbae2591a03bdfe /2022
parentd499fb9e569e4eb1c7bafeba46e6d1e5dd33398f (diff)
Add day 2 2022HEADmaster
Diffstat (limited to '2022')
-rw-r--r--2022/02/part1.asm75
-rw-r--r--2022/02/part2.asm94
2 files changed, 169 insertions, 0 deletions
diff --git a/2022/02/part1.asm b/2022/02/part1.asm
new file mode 100644
index 0000000..1b0952b
--- /dev/null
+++ b/2022/02/part1.asm
@@ -0,0 +1,75 @@
+%include "unistd.asm"
+%include "utils.asm"
+
+stdout equ 1
+
+global _start
+
+section .text
+_start:
+ mov rcx, [rsp] ; Get number of arguments
+ mov rdi, [rsp + rcx * 8] ; Get last argument
+
+ mov rax, SYS_access ; Check if file exists and is readable
+ mov esi, R_OK
+ syscall
+ test rax, rax
+ jnz .exit_err
+
+ mov rax, SYS_open
+ xor esi, esi
+ xor edx, edx
+ syscall
+
+ mov r8d, eax ; Move fd into r8
+ mov rax, SYS_mmap
+ xor edi, edi ; Let the kernel choose the address
+ mov rsi, 12288
+ mov rdx, PROT_READ
+ mov r10d, MAP_PRIVATE
+ xor r9d, r9d
+ syscall
+
+ cmp rax, -1 ; Exit on error
+ je .exit_err
+
+ xor r8d, r8d ; score
+.loop:
+ cmp byte [rax], 0
+ je .exit
+ mov ecx, [rax]
+ add rax, 4
+ mov edx, ecx
+ and ecx, 0xff
+ shr edx, 2 * 8
+ and edx, 0xff
+ sub ecx, 'A' - 1
+ sub edx, 'X' - 1
+ add r8d, edx ; Add score selected shape
+ cmp ecx, edx
+ je .draw
+ inc edx
+ mov edi, edx
+ shr edi, 2
+ or edx, edi
+ and edx, 0b11
+ cmp ecx, edx
+ je .loop
+ add r8d, 3 ; win
+.draw:
+ add r8d, 3
+ jmp .loop
+
+.exit:
+ mov edi, stdout
+ mov esi, r8d
+ call write_ulong
+
+ mov rax, SYS_exit
+ xor edi, edi
+ syscall
+
+.exit_err:
+ mov rax, SYS_exit
+ mov edi, 1
+ syscall
diff --git a/2022/02/part2.asm b/2022/02/part2.asm
new file mode 100644
index 0000000..fa92ea6
--- /dev/null
+++ b/2022/02/part2.asm
@@ -0,0 +1,94 @@
+%include "unistd.asm"
+%include "utils.asm"
+
+stdout equ 1
+
+global _start
+
+section .rodata
+ JMP_TABLE: dq _start.lose, _start.draw, _start.win
+
+section .text
+_start:
+ mov rcx, [rsp] ; Get number of arguments
+ mov rdi, [rsp + rcx * 8] ; Get last argument
+
+ mov rax, SYS_access ; Check if file exists and is readable
+ mov esi, R_OK
+ syscall
+ test rax, rax
+ jnz .exit_err
+
+ mov rax, SYS_open
+ xor esi, esi
+ xor edx, edx
+ syscall
+
+ mov r8d, eax ; Move fd into r8
+ mov rax, SYS_mmap
+ xor edi, edi ; Let the kernel choose the address
+ mov rsi, 12288
+ mov rdx, PROT_READ
+ mov r10d, MAP_PRIVATE
+ xor r9d, r9d
+ syscall
+
+ cmp rax, -1 ; Exit on error
+ je .exit_err
+
+ xor r8d, r8d ; score
+.loop:
+ cmp byte [rax], 0
+ je .exit
+ mov ecx, [rax]
+ add rax, 4
+ mov edx, ecx
+ and ecx, 0xff
+ shr edx, 2 * 8
+ and edx, 0xff
+ sub ecx, 'A' - 1
+ lea rdi, [JMP_TABLE]
+ jmp [rdi + rdx * 8 - 'X' * 8]
+
+.lose:
+%rep 2
+ inc ecx
+ mov edi, ecx
+ shr edi, 2
+ or ecx, edi
+ and ecx, 0b11
+%endrep
+ add r8d, ecx
+
+ jmp .loop
+
+.win:
+ inc ecx
+ mov edi, ecx
+ shr edi, 2
+ or ecx, edi
+ and ecx, 0b11
+
+ add r8d, 6
+ add r8d, ecx
+
+ jmp .loop
+
+.draw:
+ add r8d, ecx
+ add r8d, 3
+ jmp .loop
+
+.exit:
+ mov edi, stdout
+ mov esi, r8d
+ call write_ulong
+
+ mov rax, SYS_exit
+ xor edi, edi
+ syscall
+
+.exit_err:
+ mov rax, SYS_exit
+ mov edi, 1
+ syscall