adventofcode2023/09/main.s

99 lines
1.6 KiB
ArmAsm
Raw Normal View History

2023-12-10 01:13:51 -06:00
global _start
[bits 32]
[section .text]
%include "utils.s"
_start:
; read in sequence
read_seq:
mov byte [seq_len], 0
mov esi, [char_ptr]
mov edi, sequence_orig
.l:
call sign_dec_parse
stosd
inc byte [seq_len]
cmp byte [esi-1], 10 ; \n
je seq_over
jmp .l
seq_over:
mov [char_ptr], esi
; reverse sequence
movzx ecx, byte [seq_len]
mov eax, ecx
dec eax
lea esi, [sequence_orig+eax*4]
mov edi, sequence
reverse:
std ; set direction flag
lodsd
cld ; clear direction flag
stosd
loop reverse
cld
; calc differences
mov byte [depth], 1
calc_diff:
lea esi, [sequence+4] ; i=1
mov ebx, [sequence] ; last, i=0
mov edi, sequence
xor edx, edx ; all zero flag
movzx ecx, byte [seq_len]
dec ecx
push dword [sequence+ecx*4]
.l:
lodsd ; curr
mov ebp, eax ; save curr
sub eax, ebx ; curr - last
setnz dh ; dl = 1 if not zero
add dl, dh
stosd ; save in new seq
mov ebx, ebp ; last = curr
loop .l
test edx, edx
jz .all_zeros
; we need another layer
inc byte [depth]
dec byte [seq_len]
jmp calc_diff
.all_zeros:
xor ebx, ebx ; start diff is 0
movzx ecx, byte [depth]
.predict_l:
pop eax ; curr
add eax, ebx ; curr += last diff
mov ebx, eax ; last diff = curr
loop .predict_l
call print_sign_dec
call space
call space
add [final_value], eax
mov eax, [final_value]
call print_sign_dec
call newline
cmp dword [char_ptr], file.over-1
jb read_seq
game_over:
mov eax, [final_value]
call print_sign_dec
jmp exit
[section .data]
final_value: dd 0
file_lim: dd file.over - file
dd 0
file: incbin "input"
.over:
char_ptr: dd file
seq_len: db 0
depth: db 0
[section .bss]
sequence: resd 32
sequence_orig: resd 32