adventofcode2023/11/main_part1.s
2023-12-11 03:28:34 -06:00

268 lines
3.4 KiB
ArmAsm

global _start
[bits 32]
[section .text]
%include "utils.s"
_start:
; find line length
mov edi, file
mov ecx, file.over - file
mov al, 10 ; \n
repne scasb
mov eax, edi
sub eax, file
mov [line_len], eax
call print_dec
call newline
mov al, 0xff
mov ecx, 512
mov edi, empty_rows
rep stosb
mov eax, -1
mov ecx, 512
mov edi, stars
rep stosd
; find empty rows
xor ebx, ebx
mov ebp, empty_rows
check_rows:
mov eax, [line_len]
mov ecx, eax
mul ebx
lea edi, [eax+file]
mov esi, edi
add esi, ecx
mov al, '.'
repe scasb
mov eax, esi
cmp edi, esi
jne .not_empty
.empty:
mov [ebp], bl
inc ebp
.not_empty:
.cont:
inc ebx
cmp eax, file.over
jb check_rows
; find empty cols
xor ebx, ebx
mov ebp, empty_cols
check_cols:
lea edi, [file+ebx]
.l:
cmp edi, file.over
jae .empty
cmp byte [edi], '.'
jne .not_empty
add edi, [line_len]
jmp .l
.empty:
mov [ebp], bl
inc ebp
.not_empty:
inc ebx
cmp ebx, [line_len]
jb check_cols
print_empty_rows:
mov esi, empty_str
mov ecx, 6
call print_string
mov esi, empty_rows
xor eax, eax
.l:
lodsb
cmp al, -1
je .done
call space
call print_dec
jmp .l
.done:
call newline
print_empty_cols:
mov esi, empty_str
mov ecx, 6
call print_string
mov esi, empty_cols
xor eax, eax
.l:
lodsb
cmp al, -1
je .done
call space
call print_dec
jmp .l
.done:
call newline
; find #s
xor ebx, ebx ; row
mov ebp, stars
find_stars:
mov eax, ebx
mul dword [line_len]
lea edi, [file+eax] ; row ptr
cmp edi, file.over
jae .done
xor ecx, ecx ; col
.for_cols:
cmp byte [edi+ecx], '#'
jne .for_cols_cont
mov eax, ebx
call print_dec
call space
mov [ebp], bx ; row
mov eax, ecx
call print_dec
call newline
mov [ebp+2], cx ; col
add ebp, 4
.for_cols_cont:
inc ecx
cmp ecx, [line_len]
jb .for_cols
inc ebx
jmp find_stars
.done:
call newline
; calc adjustments
calc_adj:
xor ecx, ecx
.fill:
mov [adj_rows+ecx], cl
mov [adj_cols+ecx], cl
inc ecx
cmp ecx, 256
jb .fill
; do rows
mov esi, empty_rows
.rows:
lodsb
cmp al, -1
je .rows_done
movzx eax, al
inc eax
.add_to_rows:
inc byte [adj_rows+eax]
inc eax
cmp eax, 256
jb .add_to_rows
jmp .rows
.rows_done:
; do cols
mov esi, empty_cols
.cols:
lodsb
cmp al, -1
je .cols_done
movzx eax, al
inc eax
.add_to_cols:
inc byte [adj_cols+eax]
inc eax
cmp eax, 256
jb .add_to_cols
jmp .cols
.cols_done:
; apply adjustments
apply_adj:
xor eax, eax
mov esi, stars
mov edi, stars
.l:
lodsw ; row
cmp ax, -1
je .done
mov al, [adj_rows+eax]
stosw
lodsw ; col
mov al, [adj_cols+eax]
stosw
jmp .l
.done:
; print stars
print_stars:
mov esi, stars
xor eax, eax
.l:
lodsw
cmp ax, -1
je .done
call print_dec
call space
lodsw
call print_dec
call newline
jmp .l
.done:
call newline
; calc distances
mov ebp, stars
xor edi, edi ; running total
calc_dist:
lea esi, [ebp+4]
movzx ecx, word [ebp] ; row
movzx edx, word [ebp+2] ; col
xor eax, eax
.to_next:
lodsw ; row
cmp ax, -1
je .all_added
sub eax, ecx
; abs (m := n>>31, n := (n+m)^m)
mov ebx, eax
sar ebx, 31
add eax, ebx
xor eax, ebx
; add to total
add edi, eax
lodsw ; col
sub eax, edx
mov ebx, eax
sar ebx, 31
add eax, ebx
xor eax, ebx
; add to total
add edi, eax
jmp .to_next
.all_added:
calc_dist_cont:
add ebp, 4
cmp dword [ebp], -1
jne calc_dist
calc_dist_end:
mov [final_value], edi
game_over:
mov eax, [final_value]
call print_dec
call newline
jmp exit
[section .data]
line_len: dd 0
final_value: dd 0
file: incbin "input"
.over:
empty_str: db "Empty:"
not_empty_str: db "Not Empty:"
[section .bss]
empty_rows: resb 256
empty_cols: resb 256
adj_rows: resb 256
adj_cols: resb 256
stars: resd 512