day 14 part 1&2

This commit is contained in:
Lucia Ceionia 2023-12-14 14:11:15 -06:00
parent 900602889b
commit 7a619a48c3
6 changed files with 808 additions and 0 deletions

3
14/Makefile Normal file
View File

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

101
14/input Normal file
View File

@ -0,0 +1,101 @@
......O........#....O..O.O#O.O.##.O#.O##.O#......O........O....O.#..OO#.OO.#..#.........#.O..#O..#.O
O#..O...O....O.#...##.O....O..O.#...#...O..O#.#.#......#.O##O#....#..O...O..#.#......#.#O..#..#....#
O....##.O#O##.O...#...#.#.......OO.##..###.OO..O.#.#.OO#O..##..O.O.#...#..O.#....O.##.O#OO..........
....O#.O..OO.#..OOOO#.......O.....O.#O......OOO#....O.....O......O.....#..OOO.....O...O.O##OO.....##
OO#..#.O.##..##..#........O..OO.......#..O.OO.OO..#O...OO.##...#.OOO...O.O.O..#.O..#..O.O.##.O...OOO
#.....#OO.O......O..####O...O.....O.......#..#OO#..#...O...OOO.#....O#.....O...........#O#OO...O..O.
#O.O......O...O.O...O##.O#..OO....O..##.OO#O.##O.#.#.......O.O#.....OOOO......#..OO###OO.......O..#.
#.O.OO...#...##.O..OO..#.......O.O.O.O......O#....##.OOO.O.....#O#..##...OO.......#OOO.O..O......##.
..O.OO.O.#O.O........#..##.#.OO.O..##.O...O.#.....#..O.#....O..#O..O.#OO......O........#.#....#O..#.
..O......OO.O............OO#....##....#......O.#..O..O...O.O..#O......O.OO#.#.......O....#....##.O.#
.....#.O#O.#..#.#.##....#O....O.O.O.OO......#..O.O..#..OOOO....#.#..O.#.O.O..O.O#.O...O#..#..O##.O..
O..O#.OO.#.##.#.........OO.#.....#OO...........O....O..OO..O.O###..O.O.#O...O.O...#...O..O#O...OO...
.#.#...OO...#.O...O.........#OO..#.....O....##.#.OO.#.#.#OOO...#OOO.O.O..O#......#.##..........O....
....#..#...#...#.......#..#......##..O........O.OO.#O..#O.#.......#..#..O.O#.#...O#..O...O#.O...O.#O
.#O#O.O..........#O##.O#.#.........##.#............#.#.....#..#OO...#O....#OO.O.OO.#..#...O#........
.#..#.O#O...##O....O.....##...OOO#...#O..#..O##O..#..OO.#...#O.....#.O...O#.O#.OO...###.#..O#O..OO..
...#...O....O.#....#..#.O#OO...O.O#.#....O..#OO.#.O....#O#...O...O..O.##O..#.......O.##....O##....O.
#...O...O...O#O##O.O.#.#.....#.O...#.....##..#.#OO....OO#.......#..O..O.#.#O...#.#.#.....OO.O...OO..
.O...OO.O.....##.O.##.#O.....#O..O.###..#..##.O.##O....OO..#O#....O.....O...#O#O...OO.........O..##O
.O..OO.#O.#...O...O...#...O....#.O.O.O....#O.###O.#..#..##OO.O...O.#.#.#..O...#.....O.#.O.O.#..O....
O...OO.#...O.#..#.O..O#..O#....##.#O.##...#......#O#O..O..#.##.....O...#O#..O.O...OO...OO..#.OO.O.OO
.O.O....O.O.##O#....##..##O......#.#O#..O..#...OO..O...O.#..#O#..#..#.O...#.O.OO....O#..O##.O..O..O.
....#..O..O.OO.#.O...#OOOOO.OO.O#..O....#.#O.##........#OO.#.#.O.#.OO#...#OO..#.OO...O..#.O.....O#.#
.#O...O......#OOO#..#..#....O...O..O.#O....#O#...O..#...OO.#.O.O...O..#..#..O..##.O##.....O.O..OO..#
..#O.##.#.......OOOO.OO.....O..O........##.O.OO....O###..#.O...#O.O.#OO.#....O.O.#..........#.OO....
.##...##.#......O.#.OO#...#..O##..#..##.O.......O#..O.O.#.O..#.....#..O#....O....OO.OO.O..#...O#..O.
..##O##O......O.#O.O.....#..#.#O#.O..#O#O..#OO.#..#.#.....##O.................O.....O#OO...##...O..O
....#O#O..O.#..O....#.....#.....#......O.#O........O#..O.O#..OO...#.#.O..##....O.....#......O#....#.
#.....#...O....OO..O##.OO....O.#..#O.........O..............O....##OO##.....#.##.O...#..O..#O#OO...O
...O.#O#...O..OO#...O.#O#.......#..O...O#.#..#O...#.O.O....O##...##...#.OO...O.O..OO#.#..........##.
.......#OO....#.#O#..........O.##.O.O#.....O.OO.O.OO##...##O...OO.O..O#.OO...O......O#O..O.O.##...OO
..O.O#.#.....OO............O.O..#.O#.O.O.#.##.#O.O...#.O.....O..#.#....O..#O#O....O.O.#..OOO..O..O..
..O.#.O.............#OO#.#O....#O#...OO..O##...#.....O...............#..#..O..O#....O..##...O..O.O#.
...........O.........##O#.#.O.O..O##........#..#....#O..##O..##.OO...O#.##..#..#.#O....O.O.#........
...............OOO.#........O....#.#O..##.O...O.O#.......#.#O.#...#O.......###.......O.OO...O.O..O.#
.#.O#....#......O...OO##..O#O...O.O#.O.##.OO...OOOO....OO....#.#....##.###.O.O..O..##....#..........
..OO.....OO#O.........O.#..O....#......#.#.#.#...##O.OO#.O......O..O.#O....O#...OOO....O..O.O..#..#O
#O...O.O.......##..#..O..O...O...O..O.#..#.#.O.OO............OO....#OOOO#...OO.#.O#OO....O##.O....#.
.O#..OOO.O.#.......O....#......O.OO.#......#..#.O.......###.#....#O..O....O#.O.....#O#....O#.#.....#
....OOO#.#..#.O..O....#...O.#...OO....#..OO.O#O..O....O.O...O...O#O..O..O......O.#O..O#O..........O#
..O..#.OO###....#..O....#.O..#.....O.O.#..OOO.O...##...O#..##..O#.O.......O.#.###...O.....O......O..
..O##..OO..O.O...O.#....OO.OOO.O........O......#.O..O..........O...O#OOO..O....O.#..O##O.......#....
O.O..OO##.#...#O#..#...O..#.......#O..###..OOO..O..#.O#.O..#....O.OO#.##..##O.......OO#OO..#.O....#O
...#..O.#........OO.O....OOO#.O..O#O.......O....O........#O#...#O#.#..OO..O....#O.O....O...O###.#...
O..#.......#..#.....O..OOO.#..#OO...#O.O...O.....##O.O.#O....#.....#...#.........OO.O....#...O#O#...
.O..O.#O....O#O.###....#O.O.....................###......O..#...O.OO.....#.......O..#...OO.......##.
............#O#..#.#.......#.##....#O.#....O.#...O...OOO.O.....#O..####O...O..#..#.O##....#OO##.#.#.
....O.#..#.....#...#OO#.##...O..OO....#....O#..O......OO.OO.#OO....#.O..#.....#.##..#.O...O.O#.##O..
...#OO#.O.O......#...O.O.....#......#.......................#.#.....O..O.#OO.#..OOO.OO.O.......#..#.
O#..#OO.OO.O#....##.O.#....OO..#.#.....O...O...#.O...O...O..#..O.....O..OO..O...O...O#....#...O.O...
.......OO.#O..#O#OO.#.............#....O.O#..#..#.....#..O.O#..#..##O.O.O...O.O.#O..OO..#O...OO...#.
#..#O.O.O.....O..O#.#............##O#....O.O#...#...............#OO..O.....#...OOO#.......OO...#....
..#...O...OO..#...O#O.O.O.O....O..#O#O......#.#....#..OO....#O...OO.O..O#.#O#OO.#...O.O.O..#....##.O
..#..O#..##.O...O..O....O.O.#.....#.O.OO..#.#.#...OO#....#..#O........OO.OO#.#..#..##O...O.OO.O#.#..
...#O..OO.O#OO..O#O#O....#O....#.O#.OO.##.......O.O.OO##.......#.O.O.#O.##.......O..O....##.#O..#..#
#....##...O.......#..#OO.........#O.O.O.O#...O......O.......OO##....O....#.#O.#.#.....O.OO..#...O.O.
.O..O#.....O.#..O..#..#O..O.O...O..O.#....#.....#.#.O...O..#.O.O.O....O....O.O..O#O.....O.O....O...O
.......O...O.O....O.O.OO...##..OOOOOO.O...O.##.O...#...O.O#O...#.O.O.O.........O..#..O.....##.#....#
...O..........#.O...O.O.OOO...##.....#OO.......O....#.....O.......#.#O.........O.##O#..##...O.O.#.O.
..OO#O.....O.##......O..#...#..OO.....#O#.O..#.O..#.O.#......O.#O.#..O.#.#.O..#.O......#.....O..#.O.
..O#...O..O.O.OO....#..O.......#..#.O.#....O..OO#.OO.O..#.#O..O.#.......O...O#.OO.......O#........O#
...OO....#..O...O.#....#.#OOO...O.#.#.#.O....OO...OO...#......#O..#..O.#O....#O...#..O..OO#....OO..O
.O.OO##....OO.#...O.......O...O.O.O.O.O.....O.O#..O#O......##O..#..###.O....O...O..#O.##O#....#..##.
.#..O.O....O#O..OO#........#O#.OOO...##.....O#..#.#..O#.OOO.O...#.....O#........O#O......##...O.....
..O#O.....O.#O...O...#.O.........#OO..#O.O.##..##O..OO....O...O.#.#...#..O...O#.O...O......#OO...#.#
O.........##.....###O.O.#.O##..##.O.#.O.OO......OOO...O#.O#..OO..........##.##O.O#O.O...##.O....O..O
O..##OO.OO.......#......O.#..#.....O...O#.O#..#...OO...#.O...#O.#.O..O..OOO#....O..O...#....O...#.O.
..#.....#OOO#..O..O.....O........#...O..OO.O.....OO....O.###.....#O.#..O..#...#.###O..O#...O.#...O..
O.O..#O..###.#....O#......###..#....O..OOO..O..O..OO..OO#O.....O..#..O##.#.#..OO..##..#.....O....O..
..O......O..#.......##..#O..OO.O.O....#..OOO..OO.#..#..OO.O...O.....#.OO#OOO.O.##.....O...O##.#...O.
O.OO.#O..#.#...#...##...O.#O....O##O...O##.OO#.O.###.#OO..OO#O......OOO..#OOOO...OO....#.O#.........
.#O#O#O.O.O....O.O.O...###O.O#.#OO#.O......#O#.O#O#........O...#OOO..OOO.........O...#O#...O......#.
.OO.O.#..#.#..O.....OO.O.O#.....O...O..O.#.OO.O.....#.....O.......O.OO......O...OO..O..O....#..O.#.O
.#...O#..O..#.#O...#O...O..O.....#.#.O#.OO#.#..#.....OO.#....O.#OO.....OO.O.O..O.O....#......O..OO..
...OO.##.O...#.#....##O.O..#.............#.#...#.#.#.OOO.##.......O#.O.O...OO....O..#....#.....O...O
.O...OO............#....O.#OO.#....#O....O###.#.....O.O.......#.O....#O...O...O.O.#..OO....#....#...
#.#......#O.....O..#.O.OO.........O...##....O....#..##OO..##O#..O.O......O....#.O..O......#..##....O
OOO....#.#.O.#.O#..#.....#O.O.O..O.OO.......#O.#......O##.O.#.#...#.#..OO#O.O...O..O..O.O.....#..#O.
##..O##..O...#.#..#..............#O..#O.O.O..#..O....O...#..#OO...O##....O...O...#O....OO...#.O..O..
.#...OO#.#O.......##..#O...O.#..#......O#O....#..OO..OO.#O..O...O.#.O.........O........#.##.##...O.#
...O.O..O...#O..O.O..OO.OO##O.O.O.#..#...O..##...O##O#O..#.#.O#.#O#..#..OO..O....O..OOOO..##..O.....
##........#............#....O.....O.#.O.#.O.O.#O...#O......O.#....O...#O#...OO#.#O##........O.......
OOO#O.OO..#.......OO.OO...O...##.O.##..O....OO...O.O.............##O......#.#..O..#O..##..#....#...O
.#O.......#O...O..#O......O#OO.O.O#.O......#O#......#..###.O.#.#..O#.....##.O.O...O..O.O.#..OO#.#.#O
.....O.O...O.#.....OO###..##.O.....O....#...O.O...........#O#..#O..#....#.O.......###...#..O.#....#O
#O.OO.........O#..#O.....O....O#......#....O..OOO..O.#.OO#...O..O##....##O..#..O#..O......O.OOO.O.OO
.#...O.#.#O....O..OO....#O..#...#O........#....O...O.....O..O..O.##.......O#...#..#O..#.#...O..O.O.O
......O..O...#....O....#OO#....#.#..##OO.....O.O.#......OO.O#..........#..O..#.O##.........###..O...
.O.O#..#O.#..#....O......O..OO.#O.O....O.#......#O.O.....O..OO#..O#.O.#....#...O.O...#O..#......##.O
......O.....O.#...#....#....O#O.##...O.......O.#.##.###O#.#........O.#O.O..#...#...#.O.....O.O.O#...
#.#O#.#..#.#.......#..#..#.....##O#.......#.#..##OO.........O.O....#O...O....O....#..##..#OO..O...OO
.O..#.O.O.......O....#.O........O...#O.#.....O#....#.......O.O...O...O#..##....O...#.OOO...O.#O##...
.......#..O..O...O....O#...O#....#O..O....##..#....##O....OO..#......#....O..#O.....O.O#OO..........
..O#......O.O......#.....#O.#.#.#.O...#OO.....O.O#.....O#.O.#.....OO.O.O.O....O..OO..##.#.....OO#..O
#O...O.#..#...OOO#...O......O#..O..O....###....OO..O#.#O..##.O#.O##....#.#.O#.##.....O.............O
.O.##.........#O....#..O#.O.#...O......O#.O#.OO#...O.O..........##.##O....O.#O#O..O.#...OO.O#.O.O...
#OO.O.OO.....O#....O.OO..###...O.....#..O##...O..###.O...#.O..#.O...O...OOO.#.O.#..O...O#..OO.#O...O
.###..#.OO#O..O.#..#.O..O...#....#.#..#O.O....#O....O#..#......#...#..O.....O.O.#O...O..O.....O#..#.
..###....OOOO......#O....O.....O.O.O..O.#O..##...#O#..#..#....#.O..#..O.......O...#.O#.#..#...O....#
......#O.#.......O..OO#O#....#.....OO#..#.OO..O.#....#...#O....#...O....O#.......O..#.O.#...#.#.O...

11
14/input_test Normal file
View File

@ -0,0 +1,11 @@
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....

361
14/main.s Normal file
View File

@ -0,0 +1,361 @@
global _start
[bits 32]
[section .text]
;%define LINE_LEN 11
;%define LINE_CNT 10
%define LINE_LEN 101
%define LINE_CNT 100
%define CYCLE_CNT 1000000000
%include "utils.s"
_start:
push CYCLE_CNT
fucking:
mov eax, [esp]
and eax, 0xfffff
jnz north
mov eax, [esp]
call print_dec
call newline
; north cycle
north:
; init next spots
mov edi, next_spot
mov eax, file
.init_spots:
stosd
inc eax
cmp eax, file+LINE_LEN
jb .init_spots
mov esi, file
; for chars in line
.do_stuff:
xor ebx, ebx ; col num
jmp .loop
.cont:
inc ebx ; col num
.loop:
lodsb
cmp al, 10 ; \n
je .line_done
cmp al, '#'
je .cube_rock
cmp al, 'O'
je .round_rock
; else .
jmp .cont
.cube_rock:
; move next spot to next line
lea eax, [esi+LINE_LEN-1]
mov [next_spot+ebx*4], eax
jmp .cont
.round_rock:
; move rock
mov byte [esi-1], '.'
mov edi, [next_spot+ebx*4]
mov byte [edi], 'O'
; move next spot to next line
add dword [next_spot+ebx*4], LINE_LEN
jmp .cont
.line_done:
cmp esi, file.over
jb .do_stuff
;mov esi, file
;mov ecx, file.over - file
;call print_string
;call newline
; west cycle
west:
; init next spots
mov edi, next_spot
mov eax, file ; start of first line
.init_spots:
stosd
add eax, LINE_LEN ; start of next line
cmp eax, file+(LINE_LEN*LINE_CNT)
jb .init_spots
mov esi, file ; start of first col
mov edx, esi ; save this whatever
; for chars in col
.do_stuff:
xor ebx, ebx ; line num
jmp .loop
.cont:
inc ebx ; line num
cmp esi, file.over
jae .line_done
.loop:
mov al, [esi]
add esi, LINE_LEN ; next line
cmp al, '#'
je .cube_rock
cmp al, 'O'
je .round_rock
; else .
jmp .cont
.cube_rock:
; move next spot to next column
lea eax, [esi-LINE_LEN+1]
mov [next_spot+ebx*4], eax
jmp .cont
.round_rock:
; move rock
mov byte [esi-LINE_LEN], '.'
mov edi, [next_spot+ebx*4]
mov byte [edi], 'O'
; move next spot to next column
inc dword [next_spot+ebx*4]
jmp .cont
.line_done:
inc edx ; next col
mov esi, edx
cmp esi, file+LINE_LEN
jb .do_stuff
;mov esi, file
;mov ecx, file.over - file
;call print_string
;call newline
; south cycle
south:
; init next spots
mov edi, next_spot
mov eax, file+((LINE_CNT-1)*LINE_LEN) ; start of final line
.init_spots:
stosd
inc eax
cmp eax, file+(LINE_CNT*LINE_LEN)
jb .init_spots
mov esi, file+((LINE_CNT-1)*LINE_LEN) ; start of final line
; for chars in line
.do_stuff:
xor ebx, ebx ; col num
jmp .loop
.cont:
inc ebx ; col num
.loop:
lodsb
cmp al, 10 ; \n
je .line_done
cmp al, '#'
je .cube_rock
cmp al, 'O'
je .round_rock
; else .
jmp .cont
.cube_rock:
; move next spot to previous line
lea eax, [esi-LINE_LEN-1]
mov [next_spot+ebx*4], eax
jmp .cont
.round_rock:
; move rock
mov byte [esi-1], '.'
mov edi, [next_spot+ebx*4]
mov byte [edi], 'O'
; move next spot to previous line
sub dword [next_spot+ebx*4], LINE_LEN
jmp .cont
.line_done:
sub esi, LINE_LEN*2
cmp esi, file
jae .do_stuff
;mov esi, file
;mov ecx, file.over - file
;call print_string
;call newline
; east cycle
east:
; init next spots
mov edi, next_spot
mov eax, file+LINE_LEN-2 ; end of first line
.init_spots:
stosd
add eax, LINE_LEN ; end of next line
cmp eax, file.over
jb .init_spots
mov esi, file+LINE_LEN-2 ; end of first col
mov edx, esi ; save this whatever
; for chars in col
.do_stuff:
xor ebx, ebx ; line num
jmp .loop
.cont:
inc ebx ; line num
cmp esi, file.over
jae .line_done
.loop:
mov al, [esi]
add esi, LINE_LEN ; next line
cmp al, '#'
je .cube_rock
cmp al, 'O'
je .round_rock
; else .
jmp .cont
.cube_rock:
; move next spot to previous column
lea eax, [esi-LINE_LEN-1]
mov [next_spot+ebx*4], eax
jmp .cont
.round_rock:
; move rock
mov byte [esi-LINE_LEN], '.'
mov edi, [next_spot+ebx*4]
mov byte [edi], 'O'
; move next spot to previous column
dec dword [next_spot+ebx*4]
jmp .cont
.line_done:
dec edx ; previous col
mov esi, edx
cmp esi, file
jae .do_stuff
;mov esi, file
;mov ecx, file.over - file
;call print_string
;call newline
mov dword [final_value], 0
int_calc_load:
xor ebx, ebx ; line num
mov edi, file+((LINE_CNT-1)*LINE_LEN) ; start of final line
.proc_line:
inc ebx
mov esi, edi
mov ecx, LINE_CNT ; skip \n
xor edx, edx ; O cnt
.add_stuff:
lodsb
cmp al, 'O'
jne .cont
inc edx
.cont:
loop .add_stuff
mov eax, edx
mul ebx
add [final_value], eax
sub edi, LINE_LEN
cmp edi, file
jae .proc_line
mov eax, [final_value]
mov esi, answer_bs
check_answers:
cmp esi, answer_bs.end
jb .w
mov esi, error_str
mov ecx, error_str.end - error_str
call print_string
jmp exit
.w:
cmp dword [esi], 0
je .new
cmp [esi], eax
je .found
.cont:
mov ebx, CYCLE_CNT
sub ebx, [esp]
mov edx, ebx
sub edx, 1000
cmp [esi+4], edx
jb .new
add esi, 16
jmp check_answers
.new:
mov [esi], eax
mov ebx, CYCLE_CNT
sub ebx, [esp]
mov [esi+4], ebx
mov dword [esi+8], 0 ; cycle
mov dword [esi+12], 1 ; seen times
jmp .whatever
.found:
mov ebx, CYCLE_CNT
sub ebx, [esp] ; found num
mov edx, ebx
sub edx, [esi+4] ; last found num
cmp dword [esi+8], 1
jbe .first_seen
cmp edx, [esi+8] ; last cycle
jne .cont
mov [esi+4], ebx
inc dword [esi+12] ; seen times
cmp dword [esi+12], 5
jl .whatever
mov eax, CYCLE_CNT-1
sub eax, dword [esi+4]
xor edx, edx
div dword [esi+8]
test edx, edx
jnz .whatever
mov dword [esp], 1
jmp .whatever
.first_seen:
mov [esi+4], ebx
mov [esi+8], edx
inc dword [esi+12] ; seen times
.whatever:
dec dword [esp]
jnz fucking
print_answer_cycles:
mov esi, answer_bs
.loop:
cmp dword [esi], 0
je game_over
cmp dword [esi+12], 5
jl .cont
mov eax, dword [esi]
call print_dec
call space
mov eax, dword [esi+4]
call print_dec
call space
mov eax, dword [esi+8]
call print_dec
call space
mov eax, dword [esi+12]
call print_dec
call space
cmp dword [esi+8], 1
jbe .zeroorone
mov eax, CYCLE_CNT-1
sub eax, dword [esi+4]
xor edx, edx
div dword [esi+8]
mov eax, edx
call print_dec ; one of these that gets printed as 0 is answer
.zeroorone:
call newline
.cont:
add esi, 16
jmp .loop
game_over:
jmp exit
[section .data]
final_value: dd 0
file: incbin "input"
.over:
error_str: db ":c too much super num"
.end:
[section .bss]
next_spot: resd 128
answer_bs: resd 1024 ; increase if failing ig
.end:

74
14/main_part1.s Normal file
View File

@ -0,0 +1,74 @@
global _start
[bits 32]
[section .text]
%include "utils.s"
_start:
mov esi, file
xor ebp, ebp ; line num
; for chars in line
do_line:
xor ebx, ebx ; col num
jmp .loop
.cont:
inc ebx ; col num
.loop:
lodsb
cmp al, 10 ; \n
je line_done
cmp al, '#'
je .cube_rock
cmp al, 'O'
je .round_rock
; else .
jmp .cont
.cube_rock:
; move the next spot to after this
lea eax, [ebp+1]
mov [col_next_spot+ebx], al
jmp .cont
.round_rock:
; add to next spot line load
movzx eax, byte [col_next_spot+ebx]
inc byte [line_load_cnt+eax]
; increment next spot
inc byte [col_next_spot+ebx]
jmp .cont
line_done:
inc ebp ; line num
cmp esi, file.over
jb do_line
lea ecx, [ebp-1]
output_loads:
mov ebx, ebp
sub ebx, ecx
mov eax, ebx
call print_dec
call space
movzx eax, byte [ecx+line_load_cnt-1]
call print_dec
call space
mul ebx
call print_dec
call newline
add [final_value], eax
loop output_loads
game_over:
mov eax, [final_value]
call print_dec
call newline
jmp exit
[section .data]
line_len: dd 0
final_value: dd 0
file: incbin "input"
.over:
[section .bss]
col_next_spot: resb 128
line_load_cnt: resb 128

258
14/utils.s Normal file
View File

@ -0,0 +1,258 @@
; 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
; string input in ESI
; value in EAX
; CF set if none, clear if some
; ESI set past checked area
sign_dec_parse:
push ebx
push ecx
push edx
push edi
xor eax, eax
xor edi, edi
xor ecx, ecx ; neg flag
mov ebx, 10 ; base
cmp byte [esi], '-'
jne .no_minus
inc esi
mov cl, 1
.no_minus:
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:
test ecx, ecx
jz .not_neg
neg edi
.not_neg:
clc ; clear CF
.done:
mov eax,edi
pop edi
pop edx
pop ecx
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
; input in EAX, all regs unmodified
print_sign_dec:
pushad ; save regs
; range -2147483648 to 2147483647 is 11 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, negative
xor ebp, ebp
test eax, eax
jz .zero
jns .positive
neg eax
mov ebp, 1
.positive:
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:
test ebp, ebp
jz .no_minus
dec ecx
inc edx
mov byte [ecx], '-'
.no_minus:
mov eax, 4 ; write
mov ebx, 1 ; stdout
int 0x80
add esp, 12 ; restore stack
popad ; restore regs
ret
; input in ESI, len in ECX, all regs unmodified
print_string:
pushad ; save regs
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov edx, ecx ; length
mov ecx, esi ; string
int 0x80
popad ; restore regs
ret