From 2c259cd933d502f6db5c178ad6f2a3d83f000302 Mon Sep 17 00:00:00 2001 From: Lucia Ceionia Date: Wed, 6 Dec 2023 00:10:38 -0600 Subject: [PATCH] i just.. god.. --- 06/Makefile | 3 + 06/input | 2 + 06/main.s | 60 ++++++++++++++++++++ 06/main_part1.s | 53 ++++++++++++++++++ 06/utils.s | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 264 insertions(+) create mode 100644 06/Makefile create mode 100644 06/input create mode 100644 06/main.s create mode 100644 06/main_part1.s create mode 100644 06/utils.s diff --git a/06/Makefile b/06/Makefile new file mode 100644 index 0000000..813998e --- /dev/null +++ b/06/Makefile @@ -0,0 +1,3 @@ +all: + nasm -g -felf64 main.s && ld -g main.o + diff --git a/06/input b/06/input new file mode 100644 index 0000000..d7d60a8 --- /dev/null +++ b/06/input @@ -0,0 +1,2 @@ +Time: 53 83 72 88 +Distance: 333 1635 1289 1532 diff --git a/06/main.s b/06/main.s new file mode 100644 index 0000000..c0c5bde --- /dev/null +++ b/06/main.s @@ -0,0 +1,60 @@ +%define BUFF_LIM 32768 + +global _start +[bits 64] +[section .text] + +;%include "utils.s" + +_start: + +xor rcx, rcx ; race +; check race +check_race: +push rcx +mov rsi, 53837288 +mov rdi, 0x12F02ACD6888C +;mov rsi, 71530 +;mov rdi, 940200 +;mov rbx, [(dists+4)+rcx*4] +xor rbp, rbp ; poss + +mov rcx, 1 ; test ms +check_strat: +mov rax, rsi +sub rax, rcx ; remaining time +mul rcx +;cmp rdx, rbx ; high part +;jb .cont ; high part is less +cmp rax, rdi ; low part +jbe .cont +inc rbp +.cont: +inc rcx +cmp rcx, rsi +jl check_strat + +mov rax, [final_value] +mul rbp +mov [final_value], rax + +are_we_done: +pop rcx +inc rcx +cmp rcx, [race_count] +jl check_race + +game_over: +mov rbx, [final_value] +jmp $ + +[section .data] +final_value: dq 1 +race_count: dq 1 +timelist: dd 53837288 +dists: dq 333163512891532 ; 12F02ACD6888C +;timelist: dd 71530 +;dists: dq 940200 + + +[section .bss] diff --git a/06/main_part1.s b/06/main_part1.s new file mode 100644 index 0000000..1542d45 --- /dev/null +++ b/06/main_part1.s @@ -0,0 +1,53 @@ +%define BUFF_LIM 32768 + +global _start +[bits 32] +[section .text] + +%include "utils.s" + +_start: + +xor ecx, ecx ; race +; check race +check_race: +push ecx +mov esi, [timelist+ecx*4] +mov edi, [dists+ecx*4] +xor ebp, ebp ; poss + +mov ecx, 1 ; test ms +check_strat: +mov eax, esi +sub eax, ecx ; remaining time +mul ecx +cmp eax, edi +jle .cont +inc ebp +.cont: +inc ecx +cmp ecx, esi +jl check_strat + +mov eax, [final_value] +mul ebp +mov [final_value], eax + +are_we_done: +pop ecx +inc ecx +cmp ecx, [race_count] +jl check_race + +game_over: +mov eax, [final_value] +call print_dec +jmp exit + +[section .data] +final_value: dd 1 +race_count: dd 4 +timelist: dd 53, 83, 72, 88 +dists: dd 333, 1635, 1289, 1532 + +[section .bss] diff --git a/06/utils.s b/06/utils.s new file mode 100644 index 0000000..a2dbe05 --- /dev/null +++ b/06/utils.s @@ -0,0 +1,146 @@ +; call # val val2 +; int $0x80 eax eax edx - +; +; arg1 arg2 arg3 arg4 arg5 arg6 arg7 +; ebx ecx edx esi edi ebp - + +exit: +mov eax, 1 ; exit +int 0x80 + +; filename in EBX +; return handle in EBX +; read only +open_file: +push eax +push ecx +mov eax, 5 ; open +xor ecx, ecx ; read only +int 0x80 +mov ebx, eax +pop ecx +pop eax +ret + +; file handle in EBX +; buffer in ECX +; count of bytes to read in EDX +; return bytes actually read in EAX +; exits on error +read_file: +mov eax, 3 ; read +int 0x80 +test eax, eax +js .err +ret +.err: +mov eax, 4 ; write +mov ebx, 1 ; stdout +mov ecx, .err_str +mov edx, 21 +int 0x80 +jmp exit +.err_str: db `Could not read file.\n` + +; string input in ESI +; value in EAX +; CF set if none, clear if some +; ESI set past checked area +dec_parse: +push ebx +push edx +push edi +xor eax, eax +xor edi, edi +mov ebx, 10 ; base +lodsb +sub al, '0' +js .no_input +cmp al, 9 +jle .got_char +.no_input: +stc ; set CF +jmp .done +.loop: +xor eax,eax +lodsb +sub al, '0' +js .dec_done +cmp al, 9 +jg .dec_done +.got_char: +xchg edi,eax +mul ebx +add edi,eax +jmp .loop +.dec_done: +clc ; clear CF +.done: +mov eax,edi +pop edi +pop edx +pop ebx +ret + +; modifies no regs +newline: +pushad +push 10 +mov eax, 4 ; write +mov ebx, 1 ; stdout +mov ecx, esp ; string +mov edx, 1 ; length +int 0x80 +add esp, 4 +popad +ret +; modifies no regs +space: +pushad +push 9 +mov eax, 4 ; write +mov ebx, 1 ; stdout +mov ecx, esp ; string +mov edx, 1 ; length +int 0x80 +add esp, 4 +popad +ret + +; input in EAX, all regs unmodified +print_dec: +pushad ; save regs +; max 4294967296 is 10 chars +; round to nearest 32-bit boundary +sub esp, 12 +; string in ECX, length in EDX +lea ecx, [esp+11] ; last possible byte +; check for 0 +test eax, eax +jz .zero +mov ebx, 10 ; base 10 +xor esi, esi ; counter +.div_shit: +xor edx, edx +; divide +div ebx +dec ecx ; next char +inc esi +; store +add dl, '0' +mov byte [ecx], dl +; check if done +test eax, eax +jnz .div_shit ; continue +mov edx, esi ; counter in edx +jmp .write +.zero: +mov byte [ecx], '0' +mov edx, 1 ; length +.write: +mov eax, 4 ; write +mov ebx, 1 ; stdout +int 0x80 +add esp, 12 ; restore stack +popad ; restore regs +ret