diff --git a/04/main.s b/04/main.s index 60af137..f2cbea7 100644 --- a/04/main.s +++ b/04/main.s @@ -18,7 +18,7 @@ add eax, read_buff mov [file_lim], eax mov byte [eax], 10 ; add a newline at the end -; 1. get to :, checking for file_lim +; 1. get number, save in id, checking for file_lim ; 2. repeat WINNING_COUNT times ; get the next number ; record in winning list @@ -27,19 +27,24 @@ mov byte [eax], 10 ; add a newline at the end ; get the next number ; check against the winning list ; if match then -; shl points, 1 -; cmovz points, 1 ??? +; increment matches ; end ; end -; 4. add to final value -; 5. goto 1 +; 4. add copies + 1 (original) to final value +; 5. repeat while copies > 0 +; tmp := id + 1 +; repeat matches times +; increment copies_list@tmp +; increment tmp +; end +; end +; 6. goto 1 mov esi, read_buff -; 1. get to :, checking for file_lim +; 1. get number, save in id, checking for file_lim find_colon: -lodsb -cmp al, ':' -je start_round +call dec_parse +jnc start_round cmp esi, [file_lim] jge game_over jmp find_colon @@ -49,6 +54,7 @@ jmp find_colon ; record in winning list ; end start_round: +push eax ; save game id mov ecx, WINNING_COUNT .get_next_number: call dec_parse @@ -61,12 +67,11 @@ loop .get_next_number ; get the next number ; check against the winning list ; if match then -; shl points, 1 -; cmovz points, 1 ??? +; increment matches ; end ; end check_wins: -xor ebx, ebx ; points +xor ebx, ebx ; matches .get_next_number: call dec_parse jc .get_next_number @@ -77,8 +82,7 @@ mov edx, 1 cmp [(winning_list-4)+ecx*4], eax jne .continue ; matched! -shl ebx, 1 -cmovz ebx, edx +inc ebx .continue: loop .check_winning ; check for \n @@ -86,8 +90,31 @@ dec esi lodsb cmp al, 10 ; \n jne .get_next_number -; round done -add dword [final_value], ebx ; round points + +; 4. add copies + 1 (original) to final value +pop edx ; game id +mov eax, [copies_list+edx*4] +inc eax ; copies+1 +add [final_value], eax + +; if there's no matches, skip 5 +test ebx, ebx +jz find_colon ; restart + +; 5. tmp := id + 1 +; repeat matches times +; copies_list@tmp += instances +; increment tmp +; end +add_copies: +inc edx ; tmp +mov ecx, ebx ; matches +.loop: +add [copies_list+edx*4], eax ; instances +inc edx +loop .loop + +; 6. goto 1 jmp find_colon ; restart ; print everything out @@ -103,4 +130,5 @@ filename: db "input",0 [section .bss] winning_list: resd WINNING_COUNT +copies_list: resd 256 read_buff: resb BUFF_LIM diff --git a/04/main_part1.s b/04/main_part1.s new file mode 100644 index 0000000..60af137 --- /dev/null +++ b/04/main_part1.s @@ -0,0 +1,106 @@ +%define BUFF_LIM 32768 + +%define WINNING_COUNT 10 + +global _start +[bits 32] +[section .text] + +%include "utils.s" + +_start: +mov ebx, filename +call open_file +mov ecx, read_buff +mov edx, BUFF_LIM +call read_file +add eax, read_buff +mov [file_lim], eax +mov byte [eax], 10 ; add a newline at the end + +; 1. get to :, checking for file_lim +; 2. repeat WINNING_COUNT times +; get the next number +; record in winning list +; end +; 3. repeat until \n +; get the next number +; check against the winning list +; if match then +; shl points, 1 +; cmovz points, 1 ??? +; end +; end +; 4. add to final value +; 5. goto 1 + +mov esi, read_buff +; 1. get to :, checking for file_lim +find_colon: +lodsb +cmp al, ':' +je start_round +cmp esi, [file_lim] +jge game_over +jmp find_colon + +; 2. repeat WINNING_COUNT times +; get the next number +; record in winning list +; end +start_round: +mov ecx, WINNING_COUNT +.get_next_number: +call dec_parse +jc .get_next_number +; record in list (backward since loop) +mov [(winning_list-4)+ecx*4], eax +loop .get_next_number + +; 3. repeat until \n +; get the next number +; check against the winning list +; if match then +; shl points, 1 +; cmovz points, 1 ??? +; end +; end +check_wins: +xor ebx, ebx ; points +.get_next_number: +call dec_parse +jc .get_next_number +; check against winning list +mov ecx, WINNING_COUNT +mov edx, 1 +.check_winning: +cmp [(winning_list-4)+ecx*4], eax +jne .continue +; matched! +shl ebx, 1 +cmovz ebx, edx +.continue: +loop .check_winning +; check for \n +dec esi +lodsb +cmp al, 10 ; \n +jne .get_next_number +; round done +add dword [final_value], ebx ; round points +jmp find_colon ; restart + +; print everything out +game_over: +mov eax, [final_value] +call print_dec +jmp exit + +[section .data] +file_lim: dd 0 +final_value: dd 0 +filename: db "input",0 + +[section .bss] +winning_list: resd WINNING_COUNT +read_buff: resb BUFF_LIM