-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.asm
More file actions
391 lines (301 loc) · 10.3 KB
/
Copy pathmove.asm
File metadata and controls
391 lines (301 loc) · 10.3 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
extern getRandomNumber
section .data
GRID_SIZE equ 4
ELEMENT_SIZE equ 4 ; Assuming each element in the board is a 32-bit integer
section .text
global move
move:
cmp esi, 0
je .right
cmp esi, 1
je .left
cmp esi, 2
je .down
cmp esi, 3
je .up
ret
.up:
mov ecx, GRID_SIZE ; Loop counter for rows
mov r12, 0 ; current row
.row_loop_up:
mov r13, 0 ; current column
.column_loop_up:
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r13 ; rax = row * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][column]
mov ebx, dword [rax] ; ebx = board[row][column] (32-bit value)
cmp ebx, 0
je .next_column_up
mov r14, r12 ; r14 = current position
cmp r14, 0
je .next_column_up
dec r14 ; Start from i-1
.shift_loop_up:
cmp r14, 0
jl .update_position_up
mov rax, r14 ; rax = current position
imul rax, GRID_SIZE ; rax = current position * GRID_SIZE
add rax, r13 ; rax = current position * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (current position * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[current position][column]
cmp dword [rax], 0
jne .update_position_up
dec r14
jmp .shift_loop_up
.update_position_up:
inc r14 ; k += 1
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r13 ; rax = row * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][column]
mov dword [rax], 0 ; board[row][column] = 0
mov rax, r14 ; rax = k
imul rax, GRID_SIZE ; rax = k * GRID_SIZE
add rax, r13 ; rax = k * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (k * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[k][column]
mov dword [rax], ebx ; board[k][column] = board[row][column]
; check to merge
dec r14
mov r15, r14 ; rax = k
imul r15, GRID_SIZE ; rax = k * GRID_SIZE
add r15, r13 ; rax = k * GRID_SIZE + column
imul r15, ELEMENT_SIZE ; rax = (k * GRID_SIZE + column) * ELEMENT_SIZE
add r15, rdi ; rax = address of board[k][column]
cmp dword [r15], ebx
jne .next_column_up
imul ebx, 2
mov dword [r15], ebx
mov dword [rax], 0
.next_column_up:
inc r13
cmp r13, GRID_SIZE
jl .column_loop_up
inc r12
cmp r12, GRID_SIZE
jl .row_loop_up
jmp .new_piece
.right:
mov ecx, GRID_SIZE ; Loop counter for columns
mov r13, GRID_SIZE - 1 ; current column (starting from the rightmost column)
.column_loop_right:
mov r12, 0 ; current row
.row_loop_right:
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r13 ; rax = row * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][column]
mov ebx, dword [rax] ; ebx = board[row][column] (32-bit value)
cmp ebx, 0
je .next_row_right
mov r14, r13 ; r14 = current position
cmp r14, GRID_SIZE - 1
je .next_row_right
inc r14 ; Start from j+1
.shift_loop_right:
cmp r14, GRID_SIZE - 1
jg .update_position_right
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r14 ; rax = row * GRID_SIZE + current position
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + current position) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][current position]
cmp dword [rax], 0
jne .update_position_right
inc r14
jmp .shift_loop_right
.update_position_right:
dec r14 ; k -= 1
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r13 ; rax = row * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][column]
mov dword [rax], 0 ; board[row][column] = 0
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r14 ; rax = row * GRID_SIZE + k
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + k) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][k]
mov dword [rax], ebx ; board[row][k] = board[row][column]
; check to merge
inc r14
mov r15, r12 ; rax = row
imul r15, GRID_SIZE ; rax = row * GRID_SIZE
add r15, r14 ; rax = row * GRID_SIZE + k
imul r15, ELEMENT_SIZE ; rax = (row * GRID_SIZE + k) * ELEMENT_SIZE
add r15, rdi ; rax = address of board[row][k]
cmp dword [r15], ebx
jne .next_row_right
imul ebx, 2
mov dword [r15], ebx
mov dword [rax], 0
.next_row_right:
inc r12
cmp r12, GRID_SIZE
jl .row_loop_right
dec r13
cmp r13, 0
jge .column_loop_right
jmp .new_piece
.left:
mov ecx, GRID_SIZE ; Loop counter for columns
mov r13, 0 ; current column
.column_loop:
mov r12, 0 ; current row
.row_loop:
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r13 ; rax = row * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][column]
mov ebx, dword [rax] ; ebx = board[row][column] (32-bit value)
cmp ebx, 0
je .next_row
mov r14, r13 ; r14 = current position
cmp r14, 0
je .next_row
dec r14 ; Start from j-1
.shift_loop:
cmp r14, 0
jl .update_position
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r14 ; rax = row * GRID_SIZE + current position
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + current position) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][current position]
cmp dword [rax], 0
jne .update_position
dec r14
jmp .shift_loop
.update_position:
inc r14 ; k += 1
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r13 ; rax = row * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][column]
mov dword [rax], 0 ; board[row][column] = 0
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r14 ; rax = row * GRID_SIZE + k
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + k) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][k]
mov dword [rax], ebx ; board[row][k] = board[row][column]
;check to merge
dec r14
mov r15, r12 ; rax = row
imul r15, GRID_SIZE ; rax = row * GRID_SIZE
add r15, r14 ; rax = row * GRID_SIZE + k
imul r15, ELEMENT_SIZE ; rax = (row * GRID_SIZE + k) * ELEMENT_SIZE
add r15, rdi ; rax = address of board[row][k]
cmp dword [r15], ebx
jne .next_row
imul ebx, 2
mov dword [r15], ebx
mov dword [rax], 0
.next_row:
inc r12
cmp r12, GRID_SIZE
jl .row_loop
inc r13
cmp r13, GRID_SIZE
jl .column_loop
jmp .new_piece
.down:
mov ecx, GRID_SIZE ; Loop counter for rows
mov r12, GRID_SIZE - 1 ; current row (starting from the bottom row)
.row_loop_down:
mov r13, 0 ; current column
.column_loop_down:
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r13 ; rax = row * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][column]
mov ebx, dword [rax] ; ebx = board[row][column] (32-bit value)
cmp ebx, 0
je .next_column_down
mov r14, r12 ; r14 = current position
cmp r14, GRID_SIZE - 1
je .next_column_down
inc r14 ; Start from i+1
.shift_loop_down:
cmp r14, GRID_SIZE - 1
jg .update_position_down
mov rax, r14 ; rax = current position
imul rax, GRID_SIZE ; rax = current position * GRID_SIZE
add rax, r13 ; rax = current position * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (current position * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[current position][column]
cmp dword [rax], 0
jne .update_position_down
inc r14
jmp .shift_loop_down
.update_position_down:
dec r14 ; k -= 1
mov rax, r12 ; rax = row
imul rax, GRID_SIZE ; rax = row * GRID_SIZE
add rax, r13 ; rax = row * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (row * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[row][column]
mov dword [rax], 0 ; board[row][column] = 0
mov rax, r14 ; rax = k
imul rax, GRID_SIZE ; rax = k * GRID_SIZE
add rax, r13 ; rax = k * GRID_SIZE + column
imul rax, ELEMENT_SIZE ; rax = (k * GRID_SIZE + column) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[k][column]
mov dword [rax], ebx ; board[k][column] = board[row][column]
; check to merge
inc r14
mov r15, r14 ; rax = k
imul r15, GRID_SIZE ; rax = k * GRID_SIZE
add r15, r13 ; rax = k * GRID_SIZE + column
imul r15, ELEMENT_SIZE ; rax = (k * GRID_SIZE + column) * ELEMENT_SIZE
add r15, rdi ; rax = address of board[k][column]
cmp dword [r15], ebx
jne .next_column_down
imul ebx, 2
mov dword [r15], ebx
mov dword [rax], 0
.next_column_down:
inc r13
cmp r13, GRID_SIZE
jl .column_loop_down
dec r12
cmp r12, 0
jge .row_loop_down
jmp .new_piece
.new_piece:
; new block here
push rdi
mov edi, GRID_SIZE
call getRandomNumber
mov ebx, eax
call getRandomNumber
mov edx, eax
pop rdi
mov rax, rbx ; rax = row index
imul rax, GRID_SIZE ; rax = row index * GRID_SIZE
add rax, rdx ; rax = row index * GRID_SIZE + column index
imul rax, ELEMENT_SIZE ; rax = (row index * GRID_SIZE + column index) * ELEMENT_SIZE
add rax, rdi ; rax = address of board[random_row][random_column]
; Check if the random position is empty
cmp dword [rax], 0
jne .new_piece ; if it isnt empty try again
mov dword [rax], 2
mov rax, 1
mov rcx, GRID_SIZE * GRID_SIZE ; Total number of cells
mov r15, rdi ; Pointer to the board
.loop:
cmp dword [r15], 0 ; Set the current cell to 0
je .done
add r15, 4 ; Move to the next cell
loop .loop
mov rax, 0
.done:
ret