aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2026-02-18 15:24:23 +0100
committerFelix Morgner <felix.morgner@gmail.com>2026-02-18 15:24:23 +0100
commit943e24c81926f2392c341001cca69b58536c8129 (patch)
tree4b1873fa69c75af91b33b3a384218f8a38743b1a
parent2a1f31365dade481c32efc3307f94cc052d00f7f (diff)
downloadsnake.s-943e24c81926f2392c341001cca69b58536c8129.tar.xz
snake.s-943e24c81926f2392c341001cca69b58536c8129.zip
feat: create a simple square
-rw-r--r--libs/SDL.S1
-rw-r--r--libs/SDL/rect.S10
-rw-r--r--src/helpers/function.S12
-rw-r--r--src/main.S21
4 files changed, 42 insertions, 2 deletions
diff --git a/libs/SDL.S b/libs/SDL.S
index 838f482..96efe31 100644
--- a/libs/SDL.S
+++ b/libs/SDL.S
@@ -1,4 +1,5 @@
#include "SDL/error.S"
#include "SDL/init.S"
+#include "SDL/rect.S"
#include "SDL/render.S"
#include "SDL/video.S" \ No newline at end of file
diff --git a/libs/SDL/rect.S b/libs/SDL/rect.S
new file mode 100644
index 0000000..f402324
--- /dev/null
+++ b/libs/SDL/rect.S
@@ -0,0 +1,10 @@
+//! @file rect.S
+//!
+//! Assembler bindings for SDL2 (SDL_rect.h)
+
+#define OFFSET_SDL_Rect_x 0
+#define OFFSET_SDL_Rect_y 4
+#define OFFSET_SDL_Rect_w 8
+#define OFFSET_SDL_Rect_h 12
+
+#define SIZE_SDL_Rect 16 \ No newline at end of file
diff --git a/src/helpers/function.S b/src/helpers/function.S
index 5d9c155..af41905 100644
--- a/src/helpers/function.S
+++ b/src/helpers/function.S
@@ -52,6 +52,13 @@
mov \register, .L\scope\()_\name\()(%rbp)
.endm
+.macro _function_address_of_local scope, name, register
+ _function_require_locals
+ lea .L\scope\()_\name\()(%rbp), \register
+.endm
+
+
+
.macro function_begin name
_function_require_no_function
@@ -66,6 +73,10 @@
_function_allocate_local_variables \name
.endm
+ .macro address_of_local var_name, register
+ _function_address_of_local \name, \var_name, \register
+ .endm
+
.macro load_local var_name, register
_function_load_local \name, \var_name, \register
.endm
@@ -92,6 +103,7 @@
.set .L_FUNCTION_IS_IN_FUNCTION_DEFINITION, 0
.set .L_FUNCTION_LOCALS_ALLOCATED, 0
.purgem allocate_locals
+ .purgem address_of_local
.purgem function_end
.purgem function_exit
.purgem load_local
diff --git a/src/main.S b/src/main.S
index 3a0da8a..9069c8a 100644
--- a/src/main.S
+++ b/src/main.S
@@ -1,5 +1,6 @@
#include "helpers/function.S"
#include "SDL/init.S"
+#include "SDL/rect.S"
#include "SDL/render.S"
#include "SDL/video.S"
@@ -27,6 +28,7 @@
define_local window_handle, 8
define_local renderer_handle, 8
+ define_local rect, SIZE_SDL_Rect
allocate_locals
// initialize SDL
@@ -61,7 +63,7 @@
lea failed_to_create_window(%rip), %rdi
call _print_sdl_error@PLT
mov $1, %rax
- function_exit
+ jmp .Lquit_sdl
1:
lea succeeded_to_create_window(%rip), %rdi
xor %rax, %rax
@@ -85,16 +87,31 @@
xor %rax, %rax
call printf@PLT
+ mov $(SCREEN_HEIGHT / 2), %r8
+ mov $(SCREEN_WIDTH / 2), %r9
+
+ address_of_local rect, %rdi
+ mov %r8, OFFSET_SDL_Rect_w(%rdi)
+ mov %r8, OFFSET_SDL_Rect_h(%rdi)
+ mov %r9, %r10
+ shr $1, %r9
+ sub %r9, %r10
+ mov %r10, OFFSET_SDL_Rect_x(%rdi)
+ mov %r8, %r10
+ sub %r9, %r10
+ mov %r10, OFFSET_SDL_Rect_y(%rdi)
+
.Ldestroy_renderer:
// destroy the renderer
load_local renderer_handle, %rdi
- call SDL_DestroyRenderer
+ call SDL_DestroyRenderer@PLT
.Ldestroy_window:
// destroy the window
load_local window_handle, %rdi
call SDL_DestroyWindow@PLT
+ .Lquit_sdl:
call SDL_Quit@PLT
xor %rax, %rax