From 6e6ee64bb4cf10a98c7bd92e06090502eb67e402 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 17 Feb 2026 21:54:18 +0100 Subject: initial commit --- .gitignore | 1 + .vscode/launch.json | 23 ++++++++++++++ CMakeLists.txt | 19 ++++++++++++ CMakePresets.json | 21 +++++++++++++ src/SDL_interface.S | 26 ++++++++++++++++ src/main.S | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 178 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 CMakeLists.txt create mode 100644 CMakePresets.json create mode 100644 src/SDL_interface.S create mode 100644 src/main.S diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b148580 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,23 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug", + "type": "cppdbg", + "request": "launch", + "program": "${command:cmake.launchTargetPath}", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..90d304a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION "4.2.0") + +project("snake.s" + DESCRIPTION "The assembly snake!" + VERSION "1.0.0" + LANGUAGES ASM +) + +find_package("SDL2" REQUIRED) + +add_executable("snake.s" + "src/main.S" +) + +target_link_libraries("snake.s" PRIVATE + "SDL2::SDL2" +) + +install(TARGETS "snake.s") diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..dc09fc2 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,21 @@ +{ + "version": 10, + "configurePresets": [ + { + "name": "default", + "displayName": "Default Configuration", + "generator": "Ninja", + "binaryDir": "${sourceDir}/build", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + } + ], + "buildPresets": [ + { + "name": "default", + "displayName": "Default Build", + "configurePreset": "default" + } + ] +} \ No newline at end of file diff --git a/src/SDL_interface.S b/src/SDL_interface.S new file mode 100644 index 0000000..02302b4 --- /dev/null +++ b/src/SDL_interface.S @@ -0,0 +1,26 @@ +#define SDL_INIT_TIMER 0x00000001u +#define SDL_INIT_AUDIO 0x00000010u +#define SDL_INIT_VIDEO 0x00000020u +#define SDL_INIT_JOYSTICK 0x00000200u +#define SDL_INIT_HAPTIC 0x00001000u +#define SDL_INIT_GAMECONTROLLER 0x00002000u + +#define SDL_WINDOW_SHOWN 0x00000004u + +#define SDL_WINDOWPOS_UNDEFINED 0x1FFF0000u + +// int SDL_Init(uint32_t flags) +.type SDL_Init, @function +.extern SDL_Init + +// SDL_Window * SDL_CreateWindow(const char *title, int x, int y, int w, int h, uint32_t flags) +.type SDL_CreateWindow, @function +.extern SDL_CreateWindow + +// void SDL_DestroyWindow(SDL_Window *window) +.type SDL_DestroyWindow, @function +.extern SDL_DestroyWindow + +// void SDL_Quit(void) +.type SDL_Quit, @function +.extern SDL_Quit \ No newline at end of file diff --git a/src/main.S b/src/main.S new file mode 100644 index 0000000..0a21f83 --- /dev/null +++ b/src/main.S @@ -0,0 +1,88 @@ +#include "SDL_interface.S" + +#define SCREEN_WIDTH 800 +#define SCREEN_HEIGHT 600 + +// integer-params: RDI, RSI, RDX, RCX, R8, R9 +// caller-saved: RAX, RCX, RDX, RSI, RDI, R8-R11 +// callee-saved: RBX, RSP, RBP, R12-R15 + +.section .rodata + failed_to_create_window: .string "Window could not be created\nSDL_error: %s\n" + failed_to_initialize_sdl: .string "SDL could not be initialized\nSDL_error: %s\n" + greeting: .string "Hello, SDL!\n" + window_title: .string "snake.s" + +.section .text + .global main + + // void _print_sdl_error(char const * format) + .type _print_sdl_error, @function + _print_sdl_error: + push %rbp + mov %rsp, %rbp + + push %rdi + sub $8, %rsp + + call SDL_GetError@PLT + mov %rax, %rsi + + add $8, %rsp + pop %rdi + + call printf@PLT + jmp .Lexit + + leave + ret + + .type main, @function + main: + push %rbp + mov %rsp, %rbp + + // initialize SDL + mov $SDL_INIT_VIDEO, %rdi + call SDL_Init@PLT + + // check if initialization was successful + cmp $0, %rax + jae 1f + lea failed_to_initialize_sdl(%rip), %rdi + call _print_sdl_error@PLT + mov $1, %rax + jmp .Lexit + 1: + + // create a window + lea window_title(%rip), %rdi + mov $SDL_WINDOWPOS_UNDEFINED, %rsi + mov $SDL_WINDOWPOS_UNDEFINED, %rdx + mov $SCREEN_WIDTH, %rcx + mov $SCREEN_HEIGHT, %r8 + mov $SDL_WINDOW_SHOWN, %r9 + call SDL_CreateWindow@PLT + + cmp $0, %rax + jne 1f + lea failed_to_create_window(%rip), %rdi + call _print_sdl_error@PLT + mov $1, %rax + jmp .Lexit + 1: + + mov %rax, %rdi + call SDL_DestroyWindow@PLT + call SDL_Quit@PLT + + .Lsuccess: + lea greeting(%rip), %rdi + xor %rax, %rax + call printf@PLT + + xor %rax, %rax + + .Lexit: + leave + ret -- cgit v1.2.3