adventofcode2023/01/main_part1.s
Lucia Ceionia 963309eaee day 1
2023-12-02 01:38:16 -06:00

118 lines
1.8 KiB
ArmAsm

%define BUFF_LIM 32768
; call # val val2
; int $0x80 eax eax edx -
;
; arg1 arg2 arg3 arg4 arg5 arg6 arg7
; ebx ecx edx esi edi ebp -
global _start
[bits 32]
[section .text]
_start:
push 0 ; eof
mov eax, 5 ; open
mov ebx, filename
xor ecx, ecx ; read only
int 0x80
mov ebx, eax
mov eax, 3 ; read
mov ecx, read_buff
mov edx, BUFF_LIM
int 0x80
cmp eax, -1 ; err
je exit
cmp eax, BUFF_LIM
je .no_eof
; we hit eof
inc dword [esp]
.no_eof:
; parse section
lea edi, [read_buff+eax]
mov esi, read_buff
.cont_read:
xor ecx, ecx ; set if first is found
; last digit in ebx
xor ebx, ebx
; counting loop
xor eax, eax
push eax ; first digit
jmp .count_loop_cont
.count_loop:
lodsb
cmp al, 10 ; newline
je .next_line
sub al, '0'
js .count_loop_cont
cmp al, 9
jg .count_loop_cont
test ecx, ecx
jnz .have_first
mov [esp], eax ; first digit
inc ecx
.have_first:
mov ebx, eax ; set last to current
.count_loop_cont:
cmp esi, edi
jl .count_loop
.next_line:
test ecx, ecx
jz .cont_read ; no digits!
pop eax ; first digit
mov dl, 10
mul dl
add eax, ebx
; add to total
add [final_value], eax
; check for more data
cmp esi, edi
jl .cont_read
; TODO handle files over BUFF_LIM bytes
; check for EOF
cmp dword [esp], 1
jne .cont_read
; we're at EOF, print result
mov eax, [final_value]
call print_dec
jmp exit
exit:
mov eax, 1
int 0x80 ; exit
; i hate this
print_dec:
sub esp, 12
lea ecx, [esp+11]
test eax, eax
jz .zero
mov esi, 0
mov ebx, 10
.div_shit:
xor edx, edx
div ebx
dec ecx
inc esi
add dl, '0'
mov byte [ecx], dl
test eax, eax
jnz .div_shit
mov edx, esi
jmp .write
.zero:
mov byte [esp+10], 0
lea ecx, [esp+10]
mov edx, 1 ; count
.write:
mov eax, 4 ; write
mov ebx, 1 ; stdout
int 0x80
add esp, 12
ret
[section .data]
final_value: dd 0
filename: db "input",0
[section .bss]
read_buff: resb BUFF_LIM