1
0
Fork 0

Compare commits

...

5 Commits

Author SHA1 Message Date
chylex 4ccd7125e8
Setup x64 ASM 2022-02-11 08:17:45 +01:00
chylex abb0bcaa20
Fix project name of 2015 - Day 1 2022-02-11 07:14:11 +01:00
chylex 5e198a7989
Fix access violation in 2015 - Day 1 2022-02-11 06:53:50 +01:00
chylex 1bbcccefb4
Reorganize CMake project & update README 2022-02-11 06:49:33 +01:00
chylex 0538a6d11a
Add benchmark timing to ASM projects 2022-02-11 06:49:33 +01:00
13 changed files with 138 additions and 29 deletions

1
.gitignore vendored
View File

@ -1,6 +1,5 @@
/.gradle/
/build/
/cmake-build-debug/
/.idea/compiler.xml
/.idea/dictionaries

11
.idea/cmake.xml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeSharedSettings">
<configurations>
<configuration PROFILE_NAME="Debug x86" ENABLED="true" CONFIG_NAME="Debug" TOOLCHAIN_NAME="Visual Studio x86" />
<configuration PROFILE_NAME="Release x86" ENABLED="true" CONFIG_NAME="Release" TOOLCHAIN_NAME="Visual Studio x86" />
<configuration PROFILE_NAME="Debug x64" ENABLED="true" CONFIG_NAME="Debug" TOOLCHAIN_NAME="Visual Studio x64" />
<configuration PROFILE_NAME="Release x64" ENABLED="true" CONFIG_NAME="Release" TOOLCHAIN_NAME="Visual Studio x64" />
</configurations>
</component>
</project>

View File

@ -1,7 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/2015">
<contentRoot DIR="$PROJECT_DIR$" />
</component>
<component name="CidrRootsConfiguration">
<sourceRoots>
<file path="$PROJECT_DIR$/2015" />
</sourceRoots>
<excludeRoots>
<file path="$PROJECT_DIR$/.gradle" />
<file path="$PROJECT_DIR$/build" />

View File

@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2015 - Day 01" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" WORKING_DIR="file://$PROJECT_DIR$/2015/01" PASS_PARENT_ENVS_2="true" PROJECT_NAME="AOC" TARGET_NAME="AOC_2015_1" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="AOC" RUN_TARGET_NAME="AOC_2015_1">
<configuration default="false" name="2015 - Day 01" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" WORKING_DIR="file://$PROJECT_DIR$/2015/01" PASS_PARENT_ENVS_2="true" PROJECT_NAME="AOC" TARGET_NAME="AOC_2015_01" CONFIG_NAME="Debug x86" RUN_TARGET_PROJECT_NAME="AOC" RUN_TARGET_NAME="AOC_2015_01">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>

4
2015/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/cmake-build-debug-x86/
/cmake-build-debug-x64/
/cmake-build-release-x86/
/cmake-build-release-x64/

View File

@ -1,9 +1,5 @@
set(PROJECT_NAME AOC_2015_1)
set(PROJECT_NAME AOC_2015_01)
project(${PROJECT_NAME} C ASM_NASM)
enable_language(ASM_NASM)
set(CMAKE_NASM_LINK_EXECUTABLE "ld <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CAN_USE_ASSEMBLER TRUE)
add_executable(${PROJECT_NAME} main.asm ../utils/main.c ../utils/file.h)
add_executable(${PROJECT_NAME} main.asm ../utils/main.c ../utils/file.h ../utils/benchmark.h)

View File

@ -1,13 +1,8 @@
bits 32
extern _printf
section .data
print_final_floor: db `Final floor: %d\n`, 0
print_first_entered_basement: db `First entered basement: %d\n`, 0
section .text
extern _print
global _entryPoint
_entryPoint:
@ -51,13 +46,22 @@ _entryPoint:
.end:
push eax
push dword print_final_floor
call _printf
call _print
pop eax
pop eax
push edi
push dword print_first_entered_basement
call _printf
call _print
pop edi
pop edi
pop edi
pop ebx
leave
ret
section .data
print_final_floor: db `Final floor: %d\n`, 0
print_first_entered_basement: db `First entered basement: %d\n`, 0

11
2015/CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.20)
project(AOC)
enable_language(ASM_NASM)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX")
set(CMAKE_NASM_LINK_EXECUTABLE "ld <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CAN_USE_ASSEMBLER TRUE)
add_subdirectory(01)

67
2015/utils/benchmark.h Normal file
View File

@ -0,0 +1,67 @@
#ifndef ADVENTOFCODE_BENCHMARK_H
#define ADVENTOFCODE_BENCHMARK_H
#include "stdio.h"
#if WIN32
#include "windows.h"
#else
#include "time.h"
#endif
static int sortDoubles(const void* a, const void* b) {
double x = *(double*) a;
double y = *(double*) b;
if (x > y) {
return 1;
}
else if (x < y) {
return -1;
}
else {
return 0;
}
}
static void formatMillis(const char* text, const double ms) {
if (ms >= 0.1) {
printf("\n%s: %.2f ms", text, ms);
}
else {
printf("\n%s: %.2f us", text, ms * 1000.0);
}
}
typedef void (*entryPointCallback)(char* input);
#define BENCHMARK_RUNS 50001
void runBenchmark(const entryPointCallback ep, char* input) {
double benchmarkResults[BENCHMARK_RUNS];
for (int run = 0; run < BENCHMARK_RUNS; run++) {
#if WIN32
LARGE_INTEGER startTime, endTime, frequency;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&startTime);
ep(input);
QueryPerformanceCounter(&endTime);
benchmarkResults[run] = (double) (endTime.QuadPart - startTime.QuadPart) * 1000.0 / (double) frequency.QuadPart;
#else
clock_t startTime = clock();
ep(input);
clock_t endTime = clock();
benchmarkResults[run] = ((endTime - startTime) * 1000.0) / CLOCKS_PER_SEC;
#endif
}
qsort(benchmarkResults, BENCHMARK_RUNS, sizeof(double), sortDoubles);
formatMillis("Median elapsed time", benchmarkResults[BENCHMARK_RUNS / 2]);
formatMillis("Q1 elapsed time", benchmarkResults[(BENCHMARK_RUNS * 25) / 100]);
formatMillis("Q3 elapsed time", benchmarkResults[(BENCHMARK_RUNS * 75) / 100]);
}
#undef BENCHMARK_RUNS
#endif //ADVENTOFCODE_BENCHMARK_H

View File

@ -56,7 +56,7 @@ char* readFile(const char* filename) {
fclose(file);
if (position != fileSize) {
printf("Error reading whole file, read only %d bytes out of %d\n", position, fileSize);
printf("Error reading whole file, read only %zu bytes out of %d\n", position, fileSize);
free(contents);
return NULL;
}

View File

@ -1,15 +1,30 @@
#include "stdarg.h"
#include "file.h"
#include "benchmark.h"
void entryPoint(char* input);
void entryPoint(char *input);
int main() {
char* input = readFile("input/1.txt");
int enableOutput = 1;
extern void print(const char *format, ...) {
if (enableOutput) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
}
int main(void) {
char *input = readFile("input/1.txt");
if (input == NULL) {
return 1;
}
else {
entryPoint(input);
enableOutput = 0;
runBenchmark(entryPoint, input);
return 0;
}
}

View File

@ -1,5 +0,0 @@
cmake_minimum_required(VERSION 3.20)
project(AOC)
add_subdirectory(2015/01)

View File

@ -12,13 +12,15 @@ The repository contains a Gradle project (`build.gradle.kts`) that sets up every
The source code is in `main.kt`. The run configuration executes the `main()` method in this file.
## NASM x86 Assembly
## NASM x86 / x64 Assembly
The repository contains a CMake project (`CMakeLists.txt`) that sets up every day as a CMake subproject. You should be able to load the CMake project into [CLion](https://www.jetbrains.com/clion/).
The repository contains a CMake project (`CMakeLists.txt`) in the respective year's folder, which sets up every day as a CMake subproject. You should be able to load the CMake project into [CLion](https://www.jetbrains.com/clion/), as long as you have two toolchains set up:
- Visual Studio x86
- Visual Studio x64
The source code is in `main.c`, which is either in the puzzle's own folder, or in `utils` if no adjustments are needed. By default, `main.c` reads the whole input file into a buffer, and passes it as a parameter to the `entryPoint` function defined in `main.asm` which implements the logic and output of each puzzle.
Note that everything is targeted for Windows x86 or WoW64, and assembly is not portable, so running on a different OS will most likely require some changes. You will need to:
Note that everything is targeted for Windows and assembly is not portable, so running on a different OS will most likely require some changes. You will need to:
1. Install [Visual Studio](https://visualstudio.microsoft.com/) with `MSVC x64/x86 Build Tools`
2. Install [NASM](https://www.nasm.us/pub/nasm/releasebuilds/?C=M;O=D) (the "Executable only" version will suffice, as long as you setup the system `%PATH%` environment variable to include the folder with `nasm.exe`)