aoc_omni/fasm/2015/1/problem.asm

114 lines
2.2 KiB
NASM
Raw Normal View History

2023-12-19 04:20:18 -05:00
format ELF64 executable 3
include "lib/linux_syscall.inc"
include "lib/itoa.inc"
2023-12-21 09:40:11 -05:00
include "lib/print.inc"
2023-12-21 09:47:43 -05:00
include "lib/file.inc"
2023-12-19 04:20:18 -05:00
segment readable executable
entry main
main:
2023-12-21 09:47:43 -05:00
mov rdi, input_filename
call load_file
2023-12-19 04:20:18 -05:00
2023-12-21 09:40:11 -05:00
call part_1
call part_2
2023-12-21 09:47:43 -05:00
call unload_file
2023-12-21 09:40:11 -05:00
;; Done!
exit 0
part_1:
2023-12-19 04:20:18 -05:00
;; Setup the loop variables for counting
;; rbx starts at the first byte of input, and is incremented to read it all
mov rbx, [input]
;; rcx tracks the current floor
mov rcx, 0
;; rdx counts down from the file's size to 0
mov rdx, [file_stat.st_size]
2023-12-21 09:40:11 -05:00
.lp:
2023-12-19 04:20:18 -05:00
;; Is the current character the "go up" character?
cmp byte [rbx], '('
;; If so, jump to "up"
2023-12-21 09:40:11 -05:00
je .up
;; Is the current character the "go down" character?
cmp byte [rbx], ')'
;; If so, it's the "go down" character, so decrement the current floor
2023-12-21 09:40:11 -05:00
je .down
;; Otherwise it's the EOF probably, skip it
2023-12-21 09:40:11 -05:00
jmp .other
.up:
2023-12-19 04:20:18 -05:00
;; We should "go up", so increment the current floor
inc rcx
2023-12-21 09:40:11 -05:00
jmp .other
.down:
;; We should "go down", so decrement the current floor
dec rcx
2023-12-21 09:40:11 -05:00
.other:
2023-12-19 04:20:18 -05:00
;; Check the next character in the file
inc rbx
;; Count down the file's length
dec rdx
;; If we haven't reached the end of the file, loop
2023-12-21 09:40:11 -05:00
jnz .lp
2023-12-19 04:20:18 -05:00
;; Put part 1's answer in rax
mov rax, rcx
2023-12-21 09:40:11 -05:00
;; Convert rax to a c string in rbx
mov rbx, part_1_answer
2023-12-19 04:20:18 -05:00
call itoa
2023-12-21 09:40:11 -05:00
;; Print part 1's verbiage
mov rdi, part_1_verbiage
call print_c_string
call print_newline
2023-12-19 04:20:18 -05:00
2023-12-21 09:40:11 -05:00
ret
part_2:
2023-12-19 04:20:18 -05:00
;; Setup the loop variables for counting
;; rbx starts at the first byte of input, and is incremented to read it all
mov rbx, [input]
;; rcx tracks the current floor
mov rcx, 0
;; rdx tracks how many steps we've made so far
mov rdx, 0
2023-12-21 09:40:11 -05:00
.lp:
2023-12-19 04:20:18 -05:00
cmp byte [rbx], '('
2023-12-21 09:40:11 -05:00
je .up
2023-12-19 04:20:18 -05:00
dec rcx
2023-12-21 09:40:11 -05:00
jmp .moved
.up:
2023-12-19 04:20:18 -05:00
inc rcx
2023-12-21 09:40:11 -05:00
.moved:
2023-12-19 04:20:18 -05:00
inc rdx
cmp rcx, 0
2023-12-21 09:40:11 -05:00
jl .done
2023-12-19 04:20:18 -05:00
inc rbx
2023-12-21 09:40:11 -05:00
jnz .lp
.done:
2023-12-19 04:20:18 -05:00
;; Put part 2's answer in rax
mov rax, rdx
2023-12-21 09:40:11 -05:00
;; Convert rax to a c string in rbx
mov rbx, part_2_answer
call itoa
2023-12-19 04:20:18 -05:00
2023-12-21 09:40:11 -05:00
;; Print part 2's verbiage
mov rdi, part_2_verbiage
call print_c_string
call print_newline
ret
2023-12-19 04:20:18 -05:00
segment readable writable
input_filename db '../data/2015/1/input.txt', 0
2023-12-21 09:40:11 -05:00
part_1_verbiage db 'Part 1: '
part_1_answer db ' ', 0
part_2_verbiage db 'Part 2: '
part_2_answer db ' ', 0