From c0bd3fcbc3e2e72bf8afd32c8e80170bf181fceb Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Wed, 9 Jul 2025 13:26:06 +0200 Subject: [PATCH] fix pointer aritmetic --- src/drawing.asm | 39 ++++++++++++++++++--------------------- src/main.asm | 11 +++++++++++ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/drawing.asm b/src/drawing.asm index 5b4e19b..e8e0a08 100644 --- a/src/drawing.asm +++ b/src/drawing.asm @@ -6,16 +6,20 @@ section .bss extern term_rows extern term_cols + extern gameboard_size + simulation_running: RESB 1 section .rodata clear: db ESC_CHAR, "[2J", 0 reset: db ESC_CHAR, "[0m", 0 + resetLen: equ $-reset-1 + global resetLen home_cursor: db ESC_CHAR, "[H", 0 statusbar: db ESC_CHAR, "[100m", "Use arrow keys to move cursor, enter to invert cell h/j to change simulation speed, p to simulation", 0 - %define START_STOP_position $-statusbar-16 + START_STOP_pos: equ $-statusbar-16 start_str: db "START", 0 stop_str: db "STOP", 0 @@ -30,36 +34,29 @@ init_gameboard: xor rax, rax xor rcx, rcx - mov ax, [term_cols] - mov cx, [term_rows] - mul rcx - push rax - push rcx mov rdi, [gameboard_ptr] + push rdi mov rsi, 0x20; set rsi to SPACE character - mov rdx, rax + mov rdx, [gameboard_size] + push rdx call memory_set - pop rcx - pop rax + - mov rdx, rax - sub rdx, rcx; get pointer to start of last line - - mov rdi, [gameboard_ptr] - add rdi, rax; get end of gameboard - - sub rdi, 4; get space for reset sequence - + pop rdx + pop rdi + add rdi, rdx; get pointer to last char on screen + push rdi + inc rdi; so there is not empty chagacter at the end of screen lea rsi, [reset] - push rdx call string_copy - pop rdx - mov rdi, rdx - add rdi, [gameboard_ptr] + pop rdi + xor rax, rax + mov ax, [term_cols] + sub rdi, rax lea rsi, [statusbar] call string_copy diff --git a/src/main.asm b/src/main.asm index e085029..a1fa552 100644 --- a/src/main.asm +++ b/src/main.asm @@ -1,5 +1,7 @@ %include "symbols.asm" + + section .bss multipurpuse_buf: RESB 8 @@ -11,10 +13,16 @@ section .bss global gameboard_ptr gameboard_ptr: RESQ 1 + global gameboard_size + gameboard_size: RESQ 1 + extern cursor_rows extern cursor_cols +section .rodata + extern resetLen + section .text extern print_str extern unsigned_int_to_ascii @@ -55,7 +63,10 @@ _start: mov cx, [term_cols] mul rcx mov rdi, rax + mov qword [gameboard_size], rax inc rdi; addition byte for NULL BYTE + lea rax, [resetLen] + add rdi, rax call alloc mov [gameboard_ptr], rax; stores pointer to gameboard array call init_gameboard