i just.. god..

This commit is contained in:
Lucia Ceionia 2023-12-06 00:10:38 -06:00
parent 232bea8dec
commit 2c259cd933
5 changed files with 264 additions and 0 deletions

3
06/Makefile Normal file
View File

@ -0,0 +1,3 @@
all:
nasm -g -felf64 main.s && ld -g main.o

2
06/input Normal file
View File

@ -0,0 +1,2 @@
Time: 53 83 72 88
Distance: 333 1635 1289 1532

60
06/main.s Normal file
View File

@ -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]

53
06/main_part1.s Normal file
View File

@ -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]

146
06/utils.s Normal file
View File

@ -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