-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.nasm
68 lines (57 loc) · 1.47 KB
/
game.nasm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
; Copyright (C) 2023 Ethan Uppal. All rights reserved.
;
; game.nasm:
; - _game
bits 64
%include "config.nasm"
%include "struct_tui.nasm"
section .text
extern _malloc
extern _free
extern _tui_begin
extern _tui_end
extern _tui_keys
extern _tui_draw
; int game(void);
; keep r12 = struct tui* tui;
global _game
_game:
push rbp
mov rbp, rsp
; make tui struct
mov rdi, SZ_STRUCT_TUI
call _malloc ; struct tui* tui = malloc(sizeof(*tui)); // goes in rax
cmp rax, 0 ; if (!tui)
jz _game.error ; game_error();
mov r12, rax ; // <- tui in r12 forever :sunglasses:
; begin game (tui in r12)
mov rdi, r12
mov esi, W
mov edx, H
call _tui_begin ; tui_begin(tui /* mov rdi, r12 */, W, H);
; game loop
.loop:
mov rdi, r12 ; while (true) {
call _tui_keys ; tui_keys(tui);
cmp byte [r12 + OFF_tui_c], 'q' ; if (tui->c == 'q')
je .end ; goto end;
jmp _game.loop ; }
.end:
; end game
mov rdi, r12
call _tui_end ; tui_end(tui /* mov rdi, r12 */);
mov rdi, r12
call _free ; free(tui);
xor eax, eax
mov rsp, rbp
pop rbp
ret
.error:
$print error_msg, error_msg_len
mov eax, 1
mov rsp, rbp
pop rbp
ret
section .rodata
error_msg: db "A problem occured while running the game :(", 10
error_msg_len: equ $ - error_msg