-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovement.asm
More file actions
175 lines (154 loc) · 3.28 KB
/
movement.asm
File metadata and controls
175 lines (154 loc) · 3.28 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
; Move player character on field
%define SYS_read 0x2000003
%define SYS_write 0x2000004
%define SYS_exit 0x2000001
%define STDIN 0
%define STDOUT 1
%define WIDTH_SIZE 10
%define HEIGHT_SIZE 6
%macro syscall_1 2
mov rax, %1 ; syscall number
mov rdi, %2 ; single parameter
syscall
%endmacro
%macro syscall_2 3
mov rax, %1 ; syscall number
mov rdi, %2 ; parameters
mov rsi, %3
syscall
%endmacro
%macro syscall_3 4
mov rax, %1 ; syscall number
mov rdi, %2 ; parameters
mov rsi, %3
mov rdx, %4
syscall
%endmacro
%macro write_player_cell 0
syscall_3 SYS_write,STDOUT,playerCell,1
%endmacro
%macro write_empty_cell 0
syscall_3 SYS_write,STDOUT,emptyCell,1
%endmacro
%macro write_space 0
syscall_3 SYS_write,STDOUT,space,1
%endmacro
%macro write_new_line 0
syscall_3 SYS_write,STDOUT,newLine,1
%endmacro
global _main ; Entry point
section .data
space: db " "
emptyCell: db "_"
playerCell: db "X"
newLine: db 10
section .bss
playerX resb 1
playerY resb 1
inputBuffer resb 2
inputBufferSize equ $-inputBuffer
firstInputChar resb 1
section .text
_main:
call init
call game_loop
call exit
ret
init:
mov byte [rel playerX], WIDTH_SIZE / 2
mov byte [rel playerY], HEIGHT_SIZE / 2
ret
game_loop:
.while_loop_body:
call render
call input
call update
mov al, [rel inputBuffer] ; Repeat until \n is found
cmp al, [rel newLine]
jne .while_loop_body
call exit
ret
read_input:
syscall_3 SYS_read,STDIN,inputBuffer,inputBufferSize
ret
render:
xor cl, cl ; y = 0
.for_y_body:
xor bl, bl ; x = 0
.for_x_body:
push rbx ; Save counters to prevent modifications inside syscall
push rcx
call write_cell
write_space
pop rcx ; Restore counters
pop rbx
inc bl ; x++
cmp bl, WIDTH_SIZE ; x == WIDTH_SIZE
jne .for_x_body
push rcx
write_new_line
pop rcx
inc cl ; y++
cmp cl, HEIGHT_SIZE ; y == HEIGHT_SIZE
jne .for_y_body
ret
; Write empty or player cell
write_cell:
cmp cl, [rel playerY]
jne .empty
cmp bl, [rel playerX]
jne .empty
write_player_cell
ret
.empty:
write_empty_cell
ret
input:
syscall_3 SYS_read,STDIN,inputBuffer,inputBufferSize
ret
update:
; %1 - register storage
; %2 - limiter
%macro update_position_inc 2
inc %1
cmp %1, %2
jl .exit
mov %1, 0
jmp .exit
%endmacro
; %1 - register storage
; %2 - limiter
%macro update_position_dec 2
dec %1
cmp %1, 0
jge .exit
mov %1, %2 - 1
jmp .exit
%endmacro
mov al, [rel inputBuffer]
mov bl, [rel playerX] ; Save position to temp registers
mov cl, [rel playerY]
cmp al, 97 ; a
je .dec_x
cmp al, 100 ; d
je .inc_x
cmp al, 119 ; w
je .dec_y
cmp al, 115 ; s
je .inc_y
ret
.inc_x:
update_position_inc bl,WIDTH_SIZE
.dec_x:
update_position_dec bl,WIDTH_SIZE
.inc_y:
update_position_inc cl,HEIGHT_SIZE
.dec_y:
update_position_dec cl,HEIGHT_SIZE
.exit:
mov [rel playerX], bl ; Load position from temp registers
mov [rel playerY], cl
ret
exit:
syscall_1 SYS_exit,0
ret