adventofcode2023/02/main.s

171 lines
2.6 KiB
ArmAsm
Raw Normal View History

2023-12-02 03:17:55 -06:00
%define BUFF_LIM 32768
%define red_lim 12
%define green_lim 13
%define blue_lim 14
%define limits ((red_lim << 16) | (green_lim << 8) | blue_lim)
; 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
test eax,eax
js exit
add eax, read_buff
mov [file_lim], eax
mov esi, read_buff
; just try until number lol
.get_id:
call dec_parse
test eax, eax
jz .get_id
2023-12-02 20:41:34 -06:00
; we don't need to save the id
2023-12-02 03:17:55 -06:00
; get another number lol
2023-12-02 20:41:34 -06:00
xor ebx, ebx ; B
xor edi, edi ; G
xor ebp, ebp ; R
2023-12-02 03:17:55 -06:00
.get_num:
call dec_parse
test eax, eax
jz .get_num
mov ecx, eax
; r g b
lodsb
cmp al, 'r'
je .r_chk
cmp al, 'g'
je .g_chk
.b_chk:
2023-12-02 20:41:34 -06:00
cmp ecx, ebx
cmovg ebx, ecx
2023-12-02 03:17:55 -06:00
jmp .next
.g_chk:
2023-12-02 20:41:34 -06:00
cmp ecx, edi
cmovg edi, ecx
2023-12-02 03:17:55 -06:00
jmp .next
.r_chk:
2023-12-02 20:41:34 -06:00
cmp ecx, ebp
cmovg ebp, ecx
2023-12-02 03:17:55 -06:00
.next:
xor edx, edx ; game over flag
; find one of , ; \n
lodsb
cmp al, ','
je .get_num ; next number
cmp al, ';'
2023-12-02 20:41:34 -06:00
je .get_num ; we don't care about rounds
2023-12-02 03:17:55 -06:00
cmp al, 10 ; \n
je .new_game
cmp esi, dword [file_lim] ; end of file
jge .new_game
jmp .next ; other char
.new_game:
2023-12-02 20:41:34 -06:00
mov eax, ebx ; B
mul edi ; G
mul ebp ; R
2023-12-02 03:17:55 -06:00
.game_done:
2023-12-02 20:41:34 -06:00
; add power to final value
2023-12-02 03:17:55 -06:00
add dword [final_value], eax
; checked earlier but... meh
cmp esi, dword [file_lim] ; end of file
jl .get_id ; new game
; we can print final value now
mov eax, [final_value]
call print_dec
jmp exit
exit:
mov eax, 1
int 0x80 ; exit
; string in esi
; modifies EAX ESI
dec_parse:
push ebx
push ecx
push edx
push edi
xor eax, eax
xor ecx, ecx
xor edi, edi
mov ebx, 10
.loop:
lodsb
sub al, '0'
js .dec_done
cmp al, 9
jg .dec_done
xchg edi,eax
mul ebx
add edi,eax
jmp .loop
.dec_done:
mov eax,edi
pop edi
pop edx
pop ecx
pop ebx
ret
2023-12-02 21:35:17 -06:00
; input in EAX, all regs unmodified
2023-12-02 03:17:55 -06:00
print_dec:
2023-12-02 21:35:17 -06:00
pushad ; save regs
; max 4294967296 is 10 chars
; round to nearest 32-bit boundary
2023-12-02 03:17:55 -06:00
sub esp, 12
2023-12-02 21:35:17 -06:00
; string in ECX, length in EDX
lea ecx, [esp+11] ; last possible byte
; check for 0
2023-12-02 03:17:55 -06:00
test eax, eax
jz .zero
2023-12-02 21:35:17 -06:00
mov ebx, 10 ; base 10
xor esi, esi ; counter
2023-12-02 03:17:55 -06:00
.div_shit:
xor edx, edx
2023-12-02 21:35:17 -06:00
; divide
2023-12-02 03:17:55 -06:00
div ebx
2023-12-02 21:35:17 -06:00
dec ecx ; next char
2023-12-02 03:17:55 -06:00
inc esi
2023-12-02 21:35:17 -06:00
; store
2023-12-02 03:17:55 -06:00
add dl, '0'
mov byte [ecx], dl
2023-12-02 21:35:17 -06:00
; check if done
2023-12-02 03:17:55 -06:00
test eax, eax
2023-12-02 21:35:17 -06:00
jnz .div_shit ; continue
mov edx, esi ; counter in edx
2023-12-02 03:17:55 -06:00
jmp .write
.zero:
2023-12-02 21:35:17 -06:00
mov byte [ecx], '0'
mov edx, 1 ; length
2023-12-02 03:17:55 -06:00
.write:
mov eax, 4 ; write
mov ebx, 1 ; stdout
int 0x80
2023-12-02 21:35:17 -06:00
add esp, 12 ; restore stack
popad ; restore regs
2023-12-02 03:17:55 -06:00
ret
[section .data]
file_lim: dd 0
final_value: dd 0
filename: db "input",0
[section .bss]
read_buff: resb BUFF_LIM