adventofcode2023/15/main.s

161 lines
2.3 KiB
ArmAsm
Raw Normal View History

2023-12-15 01:58:12 -06:00
global _start
[bits 32]
[section .text]
%include "utils.s"
_start:
; HASH
mov esi, file
xor ebx, ebx ; current value
xor edi, edi ; current label high
xor ecx, ecx ; current label low
mov ebp, 17 ; mul value
hash:
movzx eax, byte [esi]
inc esi
cmp al, 10
je next_seq
cmp al, '-'
je minus
cmp al, '='
je equals
; save label
cmp ecx, 0x00ffffff
ja .needs_high
shl ecx, 8
mov cl, al
jmp .do_hash
.needs_high:
xchg eax, edi
shl eax, 8
mov al, cl
xchg eax, edi
mov cl, al
.do_hash:
; do HASH
; current += new
add eax, ebx
; current *= 17
mul ebp
; current %= 256
movzx ebx, al
jmp hash
; i'm not using a linked list whatever
minus:
; box *box = boxes+current*1024
shl ebx, 10
lea ebp, [ebx+boxes]
lea ebx, [ebx+boxes+4]
; box->max
mov eax, [ebp]
.find_eq:
test eax, eax
jz .done
dec eax
cmp [ebx], ecx
jne .cont
cmp [ebx+4], edi
jne .cont
xchg esi, edx
; remove match, move others down
mov edi, ebx
lea esi, [ebx+12]
; len = eax*8 + eax*4
mov ecx, eax
shl ecx, 3
shl eax, 2
add ecx, eax
rep movsd
xchg esi, edx
; decrease max
dec dword [ebp]
jmp .done
.cont:
add ebx, 12
jmp .find_eq
.done:
inc esi
jmp next_seq
equals:
; box *box = boxes+current*1024
shl ebx, 10
lea ebp, [ebx+boxes]
lea ebx, [ebx+boxes+4]
; box->max
mov eax, [ebp]
.find_eq:
test eax, eax
jz .no_match
dec eax
cmp [ebx], ecx
jne .cont
cmp [ebx+4], edi
jne .cont
; update match
mov al, [esi] ; ascii num
sub al, '0' ; to binary
mov [ebx+8], al
jmp .done
.cont:
add ebx, 12
jmp .find_eq
.no_match:
; new entry
mov [ebx], ecx
mov [ebx+4], edi
mov al, [esi]
sub al, '0'
mov [ebx+8], al
; inc max
inc dword [ebp]
.done:
add esi, 2
next_seq:
xor ebx, ebx ; current value
xor edi, edi ; current label high
xor ecx, ecx ; current label low
mov ebp, 17 ; mul value
cmp esi, file.over
jb hash
mov esi, boxes
calc_shit:
cmp dword [esi], 0
je .cont
mov edi, esi
sub edi, boxes
shr edi, 10 ; box idx
inc edi ; box num
mov ecx, 1 ; slot num
lea ebx, [esi+4] ; lenses
.inner:
mov eax, edi ; box num
mul ecx ; slot num
mul dword [ebx+8] ; focal length
add [final_value], eax
add ebx, 12
inc ecx
cmp ecx, [esi]
jbe .inner
.cont:
add esi, 1024
cmp esi, boxes.end
jb calc_shit
game_over:
mov eax, [final_value]
call print_dec
call newline
jmp exit
[section .data]
final_value: dd 0
file: incbin "input"
.over:
[section .bss]
boxes: resb 256*1024
.end: