day 18 part 2

This commit is contained in:
Lucia Ceionia 2023-12-18 14:04:47 -06:00
parent 5db47a3fcc
commit 0785e7ccd0
4 changed files with 314 additions and 129 deletions

View File

@ -1,3 +1,6 @@
all: all:
nasm -g -felf32 main.s && ld -melf_i386 -g main.o nasm -g -felf64 main.s && ld -g main.o
part_1:
nasm -g -felf32 main_part1.s && ld -melf_i386 -g main_part1.o

172
18/main.s
View File

@ -1,149 +1,65 @@
%include "utils.s" %include "utils64.s"
global _start global _start
[bits 32] [bits 64]
[section .text] [section .text]
%define FILENAME "input" %define FILENAME "input"
;%define FILENAME "input_test" ;%define FILENAME "input_test"
;%define PRINT
_start: _start:
mov esi, file mov rsi, file
mov edi, array+1024*512+512 ; like, the middle idk mov rbp, 11_000_000_000 ; vert
mov byte [edi], 1 ; filled! mov rdi, 1 ; area
proc_line: proc_line:
cmp esi, file.over cmp rsi, file.over
jae .done jae .done
lodsb ; RDLU mov al, '#'
%ifdef PRINT mov rcx, 32
call print_char xchg rdi, rsi
call space repne scasb
%endif xchg rdi, rsi
mov ebx, eax call hex_parse
inc esi ; space mov rbx, rax
call dec_parse ; count and bl, 3
%ifdef PRINT shr rax, 4
call print_dec inc rsi ; \n
call newline ; process shit
%endif cmp bl, 0
; skip to next line je .r
add esi, 10 cmp bl, 1
; put in array je .d
cmp bl, 'R' cmp bl, 2
mov ecx, 1 je .l
cmove edx, ecx cmp bl, 3
cmp bl, 'L' je .u
mov ecx, -1 .r:
cmove edx, ecx add rdi, rax
cmp bl, 'D' mul rbp
mov ecx, 1024 add rdi, rax
cmove edx, ecx jmp proc_line
cmp bl, 'U' .l:
mov ecx, -1024 mul rbp
cmove edx, ecx sub rdi, rax
mov ecx, eax jmp proc_line
.draw: .d:
add edi, edx add rdi, rax
mov byte [edi], 1 sub rbp, rax
loop .draw jmp proc_line
.u:
add rbp, rax
jmp proc_line jmp proc_line
.done: .done:
%ifdef PRINT hlt:
print_array: hlt ; go get a debugger, answer in rdi
call newline jmp hlt
mov esi, array
xor ecx, ecx
.inner:
mov bl, '.'
lodsb
test al, al
mov dl, '#'
cmovnz bx, dx
mov al, bl
call print_char
inc ecx
cmp ecx, 1024*1024
jae .done
mov ebx, ecx
and ebx, 1023
jnz .inner
call newline
jmp .inner
.done:
call newline
%endif
xor ecx, ecx ; x = 0
xor ebx, ebx ; y = 0
mov ebp, 1024*1024 ; count
call flood_fill
jmp flood_done
flood_fill:
mov eax, ebx
shl eax, 10
cmp byte [array+eax+ecx], 0
je .zero
ret
.zero:
dec ebp
mov byte [array+eax+ecx], 1
sub esp, 8
mov [esp+4], ecx
mov [esp+0], ebx
.t_right:
cmp ecx, 1023
je .t_left
inc ecx
call flood_fill
mov ecx, [esp+4]
.t_left:
cmp ecx, 0
je .t_down
dec ecx
call flood_fill
mov ecx, [esp+4]
.t_down:
cmp ebx, 1023
je .t_up
inc ebx
call flood_fill
mov ebx, [esp+0]
.t_up:
cmp ebx, 0
je .ret
dec ebx
call flood_fill
mov ebx, [esp+0]
.ret:
add esp, 8
ret
flood_done:
mov [final_value], ebp
game_over:
mov eax, [final_value]
call print_dec
call newline
jmp exit
PANIC:
p_string panic_str
jmp exit
[section .data] [section .data]
final_value: dd 0 final_value: dq 0
file: incbin FILENAME file: incbin FILENAME
.over: .over:
[section .bss] [section .bss]
array: resb 1024*1024
[section .rodata] [section .rodata]
panic_str: db 10, "AAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!11111111", 10
.over:

149
18/main_part1.s Normal file
View File

@ -0,0 +1,149 @@
%include "utils.s"
global _start
[bits 32]
[section .text]
%define FILENAME "input"
;%define FILENAME "input_test"
;%define PRINT
_start:
mov esi, file
mov edi, array+1024*512+512 ; like, the middle idk
mov byte [edi], 1 ; filled!
proc_line:
cmp esi, file.over
jae .done
lodsb ; RDLU
%ifdef PRINT
call print_char
call space
%endif
mov ebx, eax
inc esi ; space
call dec_parse ; count
%ifdef PRINT
call print_dec
call newline
%endif
; skip to next line
add esi, 10
; put in array
cmp bl, 'R'
mov ecx, 1
cmove edx, ecx
cmp bl, 'L'
mov ecx, -1
cmove edx, ecx
cmp bl, 'D'
mov ecx, 1024
cmove edx, ecx
cmp bl, 'U'
mov ecx, -1024
cmove edx, ecx
mov ecx, eax
.draw:
add edi, edx
mov byte [edi], 1
loop .draw
jmp proc_line
.done:
%ifdef PRINT
print_array:
call newline
mov esi, array
xor ecx, ecx
.inner:
mov bl, '.'
lodsb
test al, al
mov dl, '#'
cmovnz bx, dx
mov al, bl
call print_char
inc ecx
cmp ecx, 1024*1024
jae .done
mov ebx, ecx
and ebx, 1023
jnz .inner
call newline
jmp .inner
.done:
call newline
%endif
xor ecx, ecx ; x = 0
xor ebx, ebx ; y = 0
mov ebp, 1024*1024 ; count
call flood_fill
jmp flood_done
flood_fill:
mov eax, ebx
shl eax, 10
cmp byte [array+eax+ecx], 0
je .zero
ret
.zero:
dec ebp
mov byte [array+eax+ecx], 1
sub esp, 8
mov [esp+4], ecx
mov [esp+0], ebx
.t_right:
cmp ecx, 1023
je .t_left
inc ecx
call flood_fill
mov ecx, [esp+4]
.t_left:
cmp ecx, 0
je .t_down
dec ecx
call flood_fill
mov ecx, [esp+4]
.t_down:
cmp ebx, 1023
je .t_up
inc ebx
call flood_fill
mov ebx, [esp+0]
.t_up:
cmp ebx, 0
je .ret
dec ebx
call flood_fill
mov ebx, [esp+0]
.ret:
add esp, 8
ret
flood_done:
mov [final_value], ebp
game_over:
mov eax, [final_value]
call print_dec
call newline
jmp exit
PANIC:
p_string panic_str
jmp exit
[section .data]
final_value: dd 0
file: incbin FILENAME
.over:
[section .bss]
array: resb 1024*1024
[section .rodata]
panic_str: db 10, "AAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!11111111", 10
.over:

117
18/utils64.s Normal file
View File

@ -0,0 +1,117 @@
[bits 64]
[section .text]
; string input in RSI
; value in RAX
; CF set if none, clear if some
; RSI set past checked area
dec_parse:
sub rsp, 24
mov [rsp+16], rbx
mov [rsp+8], rdx
mov [rsp+0], rdi
xor rax, rax
xor rdi, rdi
mov rbx, 10 ; base
lodsb
sub al, '0'
js .no_input
cmp al, 9
jle .got_char
.no_input:
stc ; set CF
jmp .done
.loop:
xor rax,rax
lodsb
sub al, '0'
js .dec_done
cmp al, 9
jg .dec_done
.got_char:
xchg rdi,rax
mul rbx
add rdi,rax
jmp .loop
.dec_done:
clc ; clear CF
.done:
mov rax, rdi
mov rdi, [rsp+0]
mov rdx, [rsp+8]
mov rbx, [rsp+16]
add rsp, 24
ret
; string input in RSI
; value in RAX
; CF set if none, cear if some
; RSI set past checked area
hex_parse:
sub rsp, 24
mov [rsp+16], rbx
mov [rsp+8], rdx
mov [rsp+0], rdi
xor rax, rax
xor rdi, rdi
lodsb
cmp al, '0'
jb .no_input
cmp al, '9'
jbe .got_dec_char
cmp al, 'A'
jb .no_input
cmp al, 'F'
jbe .got_ualpha_char
cmp al, 'a'
jb .no_input
cmp al, 'f'
jbe .got_ualpha_char
.no_input:
stc ; set CF
jmp .done
.loop:
xor rax,rax
lodsb
cmp al, '0'
jb .hex_done
cmp al, '9'
jbe .got_dec_char
cmp al, 'A'
jb .hex_done
cmp al, 'F'
jbe .got_ualpha_char
cmp al, 'a'
jb .hex_done
cmp al, 'f'
jbe .got_ualpha_char
jmp .hex_done
.got_dec_char:
sub al, '0'
xchg rdi,rax
shl rax, 4
add rdi,rax
jmp .loop
.got_alpha_char:
sub al, 'A'-10
xchg rdi,rax
shl rax, 4
add rdi,rax
jmp .loop
.got_ualpha_char:
sub al, 'a'-10
xchg rdi,rax
shl rax, 4
add rdi,rax
jmp .loop
.hex_done:
clc ; clear CF
.done:
mov rax, rdi
mov rdi, [rsp+0]
mov rdx, [rsp+8]
mov rbx, [rsp+16]
add rsp, 24
ret
; --- MACROS ---
%define len(x) x %+ .over - x