-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.asm
900 lines (768 loc) · 16.6 KB
/
snake.asm
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
;Salem Ahmed Abdullah Ba Suhai
;TP073526
%define ENTRY_NAME_SIZE 16
%define ENTRY_SCORE_SIZE 2
%define ENTRY_TOTAL_SIZE (ENTRY_NAME_SIZE + ENTRY_SCORE_SIZE)
%define MAX_ENTRIES 10
%define LEADERBOARD_SIZE (MAX_ENTRIES * ENTRY_TOTAL_SIZE)
fieldwidth equ 30
fieldheight equ 20
startlen equ 3
%define fieldsize (fieldwidth * fieldheight) ; need to define fieldsize here otherwise breaks the rest of the code.
ICANON equ 2
ECHO equ 8
section .rodata
; table formatting for leaderboard
table_header db '┌──────┬────────────────┬─────────┐', 10
db '│ RANK │ PLAYER │ SCORE │', 10
db '├──────┼────────────────┼─────────┤', 10, 0
table_row db '│ ', 0
table_footer db '└──────┴────────────────┴─────────┘', 10, 0
table_separator db ' │ ', 0
table_end_row db ' │', 10, 0
space_char db ' ', 0
name_prompt db 'Enter your name (max 15 chars): ', 0
name_buffer_size equ 16 ; 15 chars + null terminator
leaderboard_file db 'snake_scores.txt', 0
leaderboard_msg db 'Leaderboard', 10, 0
leaderboard_entry db ') ', 0
score_separator db ' = ', 0
file_mode db 'a+', 0
snakesymbol db 27, '[32m', ' *', 0
applesymbol db 27, '[31m', ' O', 0
clearsymbol db ' ', 0
clearscreen db 27, '[33m', 27, '[2J', 27, '[0;0f', 0
endofline db 27, '[33m', '║', 10, 27, '[u', 27, '[1B', 0
startofline db 27, '[33m', 27, '[s', '║', 0
top db '╔'
times (fieldwidth-4) db '═'
db 27, '[34m', 'TP073526', 27, '[33m'
times (fieldwidth-4) db '═'
db '╗', 10, 0
bottom db '╚'
times (fieldwidth*2) db '═'
db '╝', 0
lenmessage db 10, 27, '[0m', 'Score: ', 0
newline db 27, '[0m', 10, 0
gameover db 'Game Over!', 0
up db 27, '[A', 0
down db 27, '[B', 0
left db 27, '[C', 0
right db 27, '[D', 0
section .data
; Pre-allocating space for leaderboard entries using constant values
leaderboard_entries: times LEADERBOARD_SIZE db 0
; entry buffer
current_entry_name: times ENTRY_NAME_SIZE db 0
current_entry_score: dw 0
snakeposx dw 3
snakeposy dw 3
movx dw 0
movy dw 0
hasmoved db 0
isgameover db 0
snakelen dw startlen
applepos dw 0
sleeptime:
.s dq 0
.n dq 125000000
fd dd 0
eve dw 1
rev dw 0
sym db 1
screenbuffer:
.buffer times (fieldsize * 5) db 0
.end db 0
.pos dw 0
section .bss
buffer resb 128
stty resb 12
slflag resb 4
srest resb 44
tty resb 12
lflag resb 4
brest resb 44
field resw fieldsize
name_input: resb ENTRY_NAME_SIZE
file_handle: resq 1
section .text
global _start
_start:
rdtsc
xor rdx, rdx
mov rbx, fieldwidth
div rbx
mov word[snakeposx], dx
rdtsc
xor rdx, rdx
mov rbx, fieldheight
div rbx
mov word[snakeposy], dx
call setnoncan
call newapplepos
mainloop:
call update
call sleep
jmp mainloop
get_player_name:
push rax
push rbx
push rcx
push rdx
; display name prompt
mov rsi, name_prompt
call print.buffer
call print.flush
;restore canonical mode for name input
call setcan
; eead name
mov rax, 0 ; sys_read
mov rdi, 0 ; stdin
mov rsi, name_input
mov rdx, ENTRY_NAME_SIZE-1 ; leave room for null terminator
syscall
; eemove newline if present
mov rcx, 0
.find_newline:
cmp byte [name_input + rcx], 10
je .remove_newline
inc rcx
cmp rcx, ENTRY_NAME_SIZE-1
jl .find_newline
jmp .cleanup
.remove_newline:
mov byte [name_input + rcx], 0
.cleanup:
; null termination
mov byte [name_input + ENTRY_NAME_SIZE-1], 0
; copy name to current entry
mov rsi, name_input
mov rdi, current_entry_name
mov rcx, ENTRY_NAME_SIZE
rep movsb
call setnoncan
pop rdx
pop rcx
pop rbx
pop rax
ret
update_leaderboard:
push rax
push rbx
push rcx
push rdx
; save current score
mov ax, [snakelen]
sub ax, startlen
mov [current_entry_score], ax
; open file
mov rax, 2 ; sys_open
mov rdi, leaderboard_file
mov rsi, 2 ; O_RDWR
mov rdx, 0644o
syscall
cmp rax, 0
jl .create_file
mov [file_handle], rax
jmp .read_scores
.create_file:
mov rax, 85 ; sys_creat
mov rdi, leaderboard_file
mov rsi, 0644o
syscall
mov [file_handle], rax
.read_scores:
mov rax, 0 ; sys_read
mov rdi, [file_handle]
mov rsi, leaderboard_entries
mov rdx, LEADERBOARD_SIZE
syscall
; insert new score and sort
mov r8, 0 ; Counter
mov cx, [current_entry_score]
.insert_loop:
cmp r8, MAX_ENTRIES
je .write_back
; calculate current entry offset
mov rax, r8
mov rbx, ENTRY_TOTAL_SIZE
mul rbx
mov r10, rax ; r10 = current offset
; compare scores
mov dx, word [leaderboard_entries + r10 + ENTRY_NAME_SIZE]
cmp cx, dx
jle .next_score
; shift entries down
mov r9, 8
.shift_loop:
cmp r9, r8
jl .insert_here
; calculate source and destination offsets
mov rax, r9
mov rbx, ENTRY_TOTAL_SIZE
mul rbx
mov r11, rax ; r11 = source offset
mov rax, r9
inc rax
mul rbx
mov r12, rax ; r12 = destination offset
; copy entry
mov rsi, leaderboard_entries
add rsi, r11
mov rdi, leaderboard_entries
add rdi, r12
mov rcx, ENTRY_TOTAL_SIZE
rep movsb
dec r9
jmp .shift_loop
.insert_here:
; insert current entry
mov rdi, leaderboard_entries
add rdi, r10
; copy name
mov rsi, current_entry_name
mov rcx, ENTRY_NAME_SIZE
rep movsb
; copy score
mov ax, [current_entry_score]
mov [rdi], ax
jmp .write_back
.next_score:
inc r8
jmp .insert_loop
.write_back:
; seek to beginning of file
mov rax, 8 ; sys_lseek
mov rdi, [file_handle]
xor rsi, rsi
xor rdx, rdx
syscall
; write updated leaderboard
mov rax, 1 ; sys_write
mov rdi, [file_handle]
mov rsi, leaderboard_entries
mov rdx, LEADERBOARD_SIZE
syscall
; close file
mov rax, 3 ; sys_close
mov rdi, [file_handle]
syscall
pop rdx
pop rcx
pop rbx
pop rax
ret
display_leaderboard:
push rax
push rbx
push rcx
push rdx
mov rsi, table_header
call print.buffer
xor r8, r8 ; counter
.display_loop:
cmp r8, MAX_ENTRIES
je .end_display
; calculate entry offset
mov rax, r8
mov rbx, ENTRY_TOTAL_SIZE
mul rbx
mov r10, rax ; r10 = current offset
; start row
mov rsi, table_row
call print.buffer
; Display position number (fixed 4 characters)
mov rax, r8
inc rax
cmp rax, 1000
jge .print_pos
cmp rax, 100
jge .pad_one
cmp rax, 10
jge .pad_two
; Pad with three spaces for single digits
mov rsi, space_char
call print.buffer
mov rsi, space_char
call print.buffer
mov rsi, space_char
call print.buffer
jmp .print_pos
.pad_two:
; Pad with two spaces for double digits
mov rsi, space_char
call print.buffer
mov rsi, space_char
call print.buffer
jmp .print_pos
.pad_one:
; Pad with one space for triple digits
mov rsi, space_char
call print.buffer
.print_pos:
call print.numberbuf
; first separator
mov rsi, table_separator
call print.buffer
; display name (fixed 12 characters)
mov rcx, 14 ; Name display length
mov rsi, leaderboard_entries
add rsi, r10
.print_name:
mov al, [rsi]
test al, al
jz .pad_name
mov [buffer], al
mov byte [buffer + 1], 0
push rsi
push rcx
mov rsi, buffer
call print.buffer
pop rcx
pop rsi
inc rsi
dec rcx
jnz .print_name
jmp .name_done
.pad_name:
test rcx, rcx
jz .name_done
.pad_loop:
push rcx
mov rsi, space_char
call print.buffer
pop rcx
dec rcx
jnz .pad_loop
.name_done:
; second separator
mov rsi, table_separator
call print.buffer
; display score
xor rax, rax
mov ax, [leaderboard_entries + r10 + ENTRY_NAME_SIZE]
; convert to string first to get length
mov rdi, buffer
push rax
call number_to_string
mov rcx, 7 ; score column width
sub rcx, rax ; calculate padding needed
pop rax
; padding with spaces
.pad_score:
test rcx, rcx
jle .print_score
push rax
push rcx
mov rsi, space_char
call print.buffer
pop rcx
pop rax
dec rcx
jmp .pad_score
.print_score:
call print.numberbuf
; end row
mov rsi, table_end_row
call print.buffer
inc r8
jmp .display_loop
.end_display:
mov rsi, table_footer
call print.buffer
call print.flush
pop rdx
pop rcx
pop rbx
pop rax
ret
number_to_string:
push rbx
push rcx
push rdx
mov rbx, 10
xor rcx, rcx ; length counter
.convert_loop:
xor rdx, rdx
div rbx
add dl, '0'
push rdx
inc rcx
test rax, rax
jnz .convert_loop
mov rax, rcx ; save length
.store_loop:
pop rdx
mov [rdi], dl
inc rdi
loop .store_loop
mov byte [rdi], 0 ; null terminate
pop rdx
pop rcx
pop rbx
ret
exit:
call setcan
; Display game over and score
mov rsi, newline
call print.buffer
mov rsi, gameover
call print.buffer
mov rsi, lenmessage
call print.buffer
xor rax, rax
mov ax, [snakelen]
sub ax, startlen
call print.numberbuf
mov rsi, newline
call print.buffer
call print.flush ; flush before getting name
; get player name and update leaderboard
call get_player_name
call update_leaderboard
mov rsi, newline
call print.buffer
call display_leaderboard
;resetting terminal
mov rax, 16 ; sys_ioctl
mov rdi, 1 ; stdout
mov rsi, 21506 ; TCSETS
mov rdx, stty ; restore terminal settings
syscall
; Exit program
mov rax, 60 ; sys_exit
xor rdi, rdi ; status = 0
syscall
update:
cmp word[snakeposx], fieldwidth ; check if snake is dead
je .dead
cmp word[snakeposx], -1
je .dead
cmp word[snakeposy], fieldheight
je .dead
cmp word[snakeposy], -1
je .dead
jmp .noexit
.dead:
mov byte[isgameover], 2
.noexit:
call poll ; exit when 'q' is pressed
cmp al, 'q'
je exit
; control snake:
mov rax, buffer ; switch case through possible key presses
switch:
mov rsi, up
call strcmp
je .up
mov rsi, down
call strcmp
je .down
mov rsi, left
call strcmp
je .left
mov rsi, right
call strcmp
je .right
jmp endswitch
.up:
mov word[movx], 0
mov word[movy], -1
jmp .end
.down:
mov word[movx], 0
mov word[movy], 1
jmp .end
.left:
mov word[movx], 1
mov word[movy], 0
jmp .end
.right:
mov word[movx], -1
mov word[movy], 0
.end:
mov byte[hasmoved], 1
endswitch: ; control end
mov r8, 0 ; position counter // for counter
xor r9, r9 ; line counter
mov r10, field; field pointer
mov r11w, [movx] ; mov snake
add [snakeposx], r11w
mov r11w, [movy]
add [snakeposy], r11w
mov rsi, clearscreen ; clear screen
call print.buffer
mov rsi, top
call print.buffer
xor r11, r11
mov ax, [snakeposy] ; calc snake position
mov r11w, fieldwidth
mul r11w
mov r11w, [snakeposx]
add r11w, ax
cmp r11w, [applepos]
jne .noappleeaten
inc word[snakelen]
call newapplepos
.noappleeaten:
.loop:
cmp r8, fieldsize
je .end
cmp r9, 0 ; if start of line
jne .nostartline
mov rsi, startofline ; print '|'
call print.buffer
.nostartline:
mov ax, [snakeposy] ; calculate abs snake pos and put in r11
mov r11w, fieldwidth
mul r11w
mov r11w, [snakeposx]
add r11w, ax
cmp r8w, r11w ; if snake head on current position
jne .nohead
cmp word[r10], 0 ; if snake has eaten itself
je .hneh
cmp byte[hasmoved], 1 ; and has moved aleready
sete byte[isgameover] ; set game over
.hneh: ; put snake body on head position
mov ax, [snakelen]
mov word[r10], ax
.nohead:
cmp word[r10], 0 ; if snake body on current position
je .nosnake
dec word[r10] ; then print snake symbol (*) and decrease lifespan
mov rsi, snakesymbol
call print.buffer
jmp .snake
.nosnake: ; else
cmp r8w, [applepos] ; if apple on current position
jne .noapple
mov rsi, applesymbol ; then print apple symbol
call print.buffer
jmp .snake
.noapple:
mov rsi, clearsymbol ; else print whitespace
call print.buffer
.snake:
add r10, 2 ; inc r10 to next word // next cell
cmp r9, fieldwidth-1 ; if end of line reached
jne .noendline
mov rsi, endofline ; then print '|\n'
call print.buffer
mov r9, -1
.noendline:
inc r9
inc r8
jmp .loop
.end: ; print score
mov rsi, bottom
call print.buffer
cmp byte[isgameover], 0 ; if game over is set
jne exit ; go to exit
mov rsi, lenmessage ; print length
call print.buffer
xor rax, rax
mov ax, [snakelen]
sub ax, startlen
call print.numberbuf
mov rsi, newline
call print.buffer
call print.flush ; flush print buffer
ret
sleep: ; sleep $sleeptime
mov rax, 35
mov rdi, sleeptime
mov rsi, 0
mov rdx, 0
syscall
ret
print:
push rax
push rdi
push rdx
push rcx
push rsi
xor rdx, rdx
.getlen:
cmp byte[rsi], 0
je .out
inc rdx
inc rsi
jmp .getlen
.out:
pop rsi
mov rax, 1
mov rdi, 1
syscall
pop rcx
pop rdx
pop rdi
pop rax
ret
.buffer:
push r9
push r10
push r11
push rsi
xor r9, r9
mov r11, screenbuffer
xor r10, r10
mov r10w, word[screenbuffer.pos]
add r11, r10
.bloop:
cmp byte[rsi], 0
je .bend
mov r9b, byte[rsi]
mov byte[r11], r9b
inc r11
inc word[screenbuffer.pos]
inc rsi
jmp .bloop
.bend:
mov word[r11], 0
pop rsi
pop r11
pop r10
pop r9
ret
.flush:
mov rsi, screenbuffer
call print
xor rax, rax
mov ax, word[screenbuffer.pos]
mov word[screenbuffer.pos], 0
ret
.numberbuf:
mov rsi, buffer
add rsi, 128
mov byte[rsi], 0
.nloop:
dec rsi
xor rdx, rdx
mov rbx, 10
div rbx
xor dl, 48
mov [rsi], dl
cmp rax, 0
jne .nloop
.nout:
call print.buffer
ret
newapplepos:
rdtsc
xor rdx, rdx
mov rbx, fieldsize
div rbx
mov r12, field
add r12, rdx
add r12, rdx
cmp word[r12], 0
jne newapplepos
.eq:
mov word[applepos], dx
ret
strcmp:
push rax
push rsi
push r8
xor r8, r8
.loop:
cmp byte[rax], 0
je .e
mov r8b, [rsi]
cmp [rax], r8b
jne .ne
inc rax
inc rsi
jmp .loop
.e:
cmp byte[rsi], 0
jne .ne
pop r8
pop rsi
pop rax
cmp rax, rax
ret
.ne:
pop r8
pop rsi
pop rax
cmp rax, 0
ret
poll:
mov qword[buffer], 0
push rbx
push rcx
push rdx
push rdi
push rsi
mov rax, 7; the number of the poll system call
mov rdi, fd; pointer to structure
mov rsi, 1; monitor one thread
mov rdx, 0; do not give time to wait
syscall
test rax, rax; check the returned value to 0
jz .e
mov rax, 0
mov rdi, 0; if there is data
mov rsi, buffer; then make the call read
mov rdx, 3
syscall
xor rax, rax
mov al, byte [buffer]; return the character code if it was read
.e:
pop rsi
pop rdi
pop rdx
pop rcx
pop rbx
ret
setnoncan:
push stty
call tcgetattr
push tty
call tcgetattr
and dword[lflag], (~ ICANON)
and dword[lflag], (~ ECHO)
call tcsetattr
add rsp, 16
ret
setcan:
push stty
call tcsetattr
add rsp, 8
ret
tcgetattr:
mov rdx, qword [rsp+8]
push rax
push rbx
push rcx
push rdi
push rsi
mov rax, 16; ioctl system call
mov rdi, 0
mov rsi, 21505
syscall
pop rsi
pop rdi
pop rcx
pop rbx
pop rax
ret
tcsetattr:
mov rdx, qword [rsp+8]
push rax
push rbx
push rcx
push rdi
push rsi
mov rax, 16; ioctl system call
mov rdi, 0
mov rsi, 21506
syscall
pop rsi
pop rdi
pop rcx
pop rbx
pop rax
ret