adventofcode2023/04/main_part1.s

107 lines
1.9 KiB
ArmAsm
Raw Normal View History

2023-12-04 00:46:30 -06:00
%define BUFF_LIM 32768
%define WINNING_COUNT 10
global _start
[bits 32]
[section .text]
%include "utils.s"
_start:
mov ebx, filename
call open_file
mov ecx, read_buff
mov edx, BUFF_LIM
call read_file
add eax, read_buff
mov [file_lim], eax
mov byte [eax], 10 ; add a newline at the end
; 1. get to :, checking for file_lim
; 2. repeat WINNING_COUNT times
; get the next number
; record in winning list
; end
; 3. repeat until \n
; get the next number
; check against the winning list
; if match then
; shl points, 1
; cmovz points, 1 ???
; end
; end
; 4. add to final value
; 5. goto 1
mov esi, read_buff
; 1. get to :, checking for file_lim
find_colon:
lodsb
cmp al, ':'
je start_round
cmp esi, [file_lim]
jge game_over
jmp find_colon
; 2. repeat WINNING_COUNT times
; get the next number
; record in winning list
; end
start_round:
mov ecx, WINNING_COUNT
.get_next_number:
call dec_parse
jc .get_next_number
; record in list (backward since loop)
mov [(winning_list-4)+ecx*4], eax
loop .get_next_number
; 3. repeat until \n
; get the next number
; check against the winning list
; if match then
; shl points, 1
; cmovz points, 1 ???
; end
; end
check_wins:
xor ebx, ebx ; points
.get_next_number:
call dec_parse
jc .get_next_number
; check against winning list
mov ecx, WINNING_COUNT
mov edx, 1
.check_winning:
cmp [(winning_list-4)+ecx*4], eax
jne .continue
; matched!
shl ebx, 1
cmovz ebx, edx
.continue:
loop .check_winning
; check for \n
dec esi
lodsb
cmp al, 10 ; \n
jne .get_next_number
; round done
add dword [final_value], ebx ; round points
jmp find_colon ; restart
; print everything out
game_over:
mov eax, [final_value]
call print_dec
jmp exit
[section .data]
file_lim: dd 0
final_value: dd 0
filename: db "input",0
[section .bss]
winning_list: resd WINNING_COUNT
read_buff: resb BUFF_LIM