├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── images └── showcase.gif ├── print.asm ├── print.mac ├── print_int.mac ├── snake.asm ├── syscall.asm ├── syscall.mac ├── syscall_int.mac ├── term.asm └── utils.asm /.gitignore: -------------------------------------------------------------------------------- 1 | /snake-asm 2 | /build 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Nikita Ivanov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | AS := nasm 2 | 3 | PREFIX ?= /usr/local 4 | BINPREFIX = $(DESTDIR)${PREFIX}/bin 5 | 6 | BUILDDIR := build 7 | 8 | BIN := snake-asm 9 | SRC := $(wildcard *.asm) 10 | OBJ := ${SRC:%.asm=${BUILDDIR}/%.o} 11 | DEP := ${OBJ:.o=.d} 12 | BINTAR := ${BIN}.tar.gz 13 | 14 | ASFLAGS += -felf64 15 | 16 | ${BIN}: ${OBJ} 17 | $(LD) -o $@ $(LDFLAGS) $^ 18 | 19 | ${BUILDDIR}/%.o: %.asm 20 | @mkdir -p ${@D} 21 | @$(AS) -o $@ -M -MF ${@:.o=.d} $< 22 | $(AS) -o $@ $(ASFLAGS) $< 23 | 24 | ${BINTAR}: ${BIN} 25 | tar czf $@ $^ 26 | 27 | dist: ${BINTAR} 28 | 29 | install: ${BIN} 30 | install -d ${BINPREFIX} 31 | install ${BIN} ${BINPREFIX} 32 | 33 | uninstall: 34 | $(RM) ${BINPREFIX}/${BIN} 35 | 36 | clean: 37 | $(RM) ${BIN} ${OBJ} ${DEP} ${BINTAR} 38 | 39 | -include ${DEP} 40 | 41 | .PHONY: dist install uninstall clean 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # snake-asm 2 | 3 | I wanted to learn some assembly language, so I made this tiny snake game for x86_64 Linux using [NASM](https://www.nasm.us/). 4 | 5 | ![showcase](images/showcase.gif) 6 | 7 | ## Download binary 8 | 9 | You can download a compiled version of the game from the [releases page](https://github.com/NikitaIvanovV/snake-asm/releases/latest). 10 | 11 | ## Compile and install 12 | 13 | *NOTE: [NASM](https://www.nasm.us/) is required in order to compile the game.* 14 | 15 | ```sh 16 | git clone https://github.com/NikitaIvanovV/snake-asm 17 | cd snake-asm 18 | sudo make install 19 | ``` 20 | 21 | Uninstall with `sudo make uninstall` 22 | 23 | ## Package manager 24 | 25 | If you are an Arch Linux user, you can install [`snake-asm`](https://aur.archlinux.org/packages/snake-asm/) AUR package: 26 | 27 | ```sh 28 | yay -S snake-asm 29 | ``` 30 | -------------------------------------------------------------------------------- /images/showcase.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NikitaIvanovV/snake-asm/b695abd0d973fff4cffde9cf83fcb5c5968029fb/images/showcase.gif -------------------------------------------------------------------------------- /print.asm: -------------------------------------------------------------------------------- 1 | %include "print_int.mac" 2 | %include "syscall.mac" 3 | 4 | section .data 5 | 6 | DEF_STR_DATA newline, 10 7 | 8 | section .bss 9 | 10 | print_num_buf resb 8 11 | print_num_buf_end equ $ 12 | 13 | section .text 14 | 15 | global print 16 | global print_num 17 | 18 | global newline 19 | global newline_len ; generated by DEF_STR_DATA 20 | 21 | ; rax: pointer to string 22 | ; rdx: string length 23 | print: 24 | mov rcx, STDOUT 25 | call write 26 | ret 27 | 28 | ; rax: number 29 | print_num: 30 | push rbx 31 | push rsi 32 | 33 | mov rbx, 10 ; for idiv 34 | mov rcx, 0 ; counter for digits 35 | 36 | .loop: 37 | ; divide [rdx:rax] by rbx 38 | ; rdx contains remainder - digit 39 | mov rdx, 0 40 | idiv rbx 41 | 42 | ; add code of 0 to get ASCII char code 43 | add dl, '0' 44 | 45 | ; save digit in the first free cell in 46 | ; buffer from the end 47 | inc rcx 48 | mov rsi, print_num_buf_end 49 | sub rsi, rcx 50 | mov [rsi], dl ; save 51 | 52 | cmp rax, 0 53 | jne .loop 54 | 55 | mov rax, print_num_buf_end 56 | sub rax, rcx 57 | mov rdx, rcx 58 | call print 59 | 60 | pop rsi 61 | pop rbx 62 | ret 63 | 64 | ; vim:ft=nasm 65 | -------------------------------------------------------------------------------- /print.mac: -------------------------------------------------------------------------------- 1 | %ifndef PRINT_MAC 2 | %define PRINT_MAC 3 | 4 | %include "print_int.mac" 5 | 6 | extern print 7 | extern print_num 8 | extern newline 9 | extern newline_len 10 | 11 | %endif 12 | 13 | ; vim:ft=nasm 14 | -------------------------------------------------------------------------------- /print_int.mac: -------------------------------------------------------------------------------- 1 | %ifndef PRINT_INT_MAC 2 | %define PRINT_INT_MAC 3 | 4 | %macro DEF_STR_DATA 2+ 5 | %1 db %2 6 | %1_len equ $-%1 7 | %endmacro 8 | 9 | %macro PRINT_STR_DATA 1 10 | mov rax, %1 11 | mov rdx, %1_len 12 | call print 13 | %endmacro 14 | 15 | %macro PRINT_NEW_LINE 0 16 | PRINT_STR_DATA newline 17 | %endmacro 18 | 19 | %endif 20 | 21 | ; vim:ft=nasm 22 | -------------------------------------------------------------------------------- /snake.asm: -------------------------------------------------------------------------------- 1 | %include "print.mac" 2 | %include "syscall.mac" 3 | 4 | %define MAIN _start 5 | 6 | %define MOVE_EVERY_TICK 2 7 | %define SIZE_X 21 8 | %define SIZE_Y 21 9 | %define EATEN_APPLES_INIT 3 10 | %define UPD_DEL_SEC 0 11 | %define UPD_DEL_NANO 50000000 12 | 13 | %assign SIZE_N SIZE_X*SIZE_Y 14 | %assign SNAKE_X_INIT SIZE_X/2 15 | %assign SNAKE_Y_INIT SIZE_Y/2 16 | 17 | %define DIR_UP 0 18 | %define DIR_RIGHT 1 19 | %define DIR_DOWN 2 20 | %define DIR_LEFT 3 21 | 22 | %define MAP_FREE 0 23 | %define MAP_WALL 1 24 | %define MAP_HEAD 2 25 | %define MAP_BODY 3 26 | %define MAP_APPLE 4 27 | 28 | %define STATUS_RUN 0 29 | %define STATUS_EXIT 1 30 | %define STATUS_DIE 2 31 | 32 | %define CELL_TEXT ' ' 33 | %strlen CELL_TEXT_LEN CELL_TEXT 34 | 35 | %macro DEF_ESC_SEQ 3+ 36 | DEF_STR_DATA %1, 27, '[', %3, %2 37 | %endmacro 38 | 39 | %macro DEF_COLOR_SEQ 3 40 | DEF_ESC_SEQ color_%1_seq, 'm', '1', ';', %2, ';', %3 41 | %endmacro 42 | 43 | %macro MAP_BUFFER 1 44 | %1: 45 | times SIZE_X db MAP_WALL ; top wall 46 | %rep SIZE_Y-2 ; cells between top and bottom walls 47 | db MAP_WALL ; left wall 48 | times SIZE_X-2 db MAP_FREE ; free space 49 | db MAP_WALL ; right wall 50 | %endrep 51 | times SIZE_X db MAP_WALL ; bottom wall 52 | %endmacro 53 | 54 | %define ESC_SEQ_MAX_LEN 10 55 | 56 | %assign MAX_CELL_PRINT_SIZE ESC_SEQ_MAX_LEN*2+CELL_TEXT_LEN 57 | 58 | %macro PRINT_BUFFER 1 59 | %1 resb SIZE_N*MAX_CELL_PRINT_SIZE 60 | %endmacro 61 | 62 | section .data 63 | 64 | %assign SCREEN_Y SIZE_Y+1 65 | %defstr SCREEN_Y_STR SCREEN_Y 66 | 67 | ; ANSI escape seqences 68 | DEF_ESC_SEQ cur_reset_seq, 'A', SCREEN_Y_STR 69 | DEF_ESC_SEQ cur_home_seq, 'H', '0', ';', '0' 70 | DEF_ESC_SEQ cur_hide_seq, 'l', '?', '25' 71 | DEF_ESC_SEQ cur_show_seq, 'h', '?', '25' 72 | DEF_ESC_SEQ clear_seq, 'J', '0' 73 | DEF_ESC_SEQ color_reset_seq, 'm', '0' 74 | DEF_COLOR_SEQ bright_red, '91', '101' 75 | DEF_COLOR_SEQ blue, '34', '44' 76 | DEF_COLOR_SEQ yellow, '33', '43' 77 | DEF_COLOR_SEQ bright_yellow, '93', '103' 78 | 79 | ; Strings 80 | DEF_STR_DATA text_score, "Score: " 81 | DEF_STR_DATA text_controls, "Move: wasd Quit: q", 10 82 | DEF_STR_DATA text_game_over, "GAME OVER!", 10 83 | DEF_STR_DATA cell_sym, CELL_TEXT 84 | 85 | ; Some global vars 86 | status db STATUS_RUN 87 | 88 | MAP_BUFFER map 89 | 90 | length dq 1 91 | score dq 0 92 | eaten dq EATEN_APPLES_INIT 93 | snake_x dq SNAKE_X_INIT 94 | snake_y dq SNAKE_Y_INIT 95 | 96 | input db 0 97 | frame dq 0 98 | 99 | ; dir in which snake will move 100 | move_dir db DIR_RIGHT 101 | future_move_dir db DIR_RIGHT 102 | 103 | section .bss 104 | 105 | ; buf to store free map cells for get_free_cells 106 | map_free_buf resq SIZE_N 107 | map_free_buf_len resq 1 108 | 109 | PRINT_BUFFER print_buf 110 | print_buf_len resq 1 111 | 112 | snake_cells_buf resq SIZE_N 113 | 114 | section .text 115 | 116 | global MAIN 117 | 118 | extern memcpy 119 | extern rand 120 | extern set_noncanon 121 | extern set_canon 122 | 123 | ; rax: x 124 | ; rdx: y 125 | ; returns: map index in rax 126 | map_coord_to_index: 127 | imul rdx, SIZE_X 128 | add rax, rdx 129 | ret 130 | 131 | %macro HANDLE_KEY 2 132 | cmp byte [input], %2 133 | je .%1 134 | %endmacro 135 | 136 | handle_key: 137 | ; old move dir 138 | mov al, [move_dir] 139 | 140 | HANDLE_KEY quit, 'q' 141 | HANDLE_KEY right, 'd' 142 | HANDLE_KEY down, 's' 143 | HANDLE_KEY left, 'a' 144 | HANDLE_KEY up, 'w' 145 | 146 | jmp .exit 147 | 148 | .quit: 149 | mov byte [status], STATUS_EXIT 150 | jmp .exit 151 | 152 | .right: 153 | cmp al, DIR_LEFT 154 | je .exit 155 | mov byte [future_move_dir], DIR_RIGHT 156 | jmp .exit 157 | 158 | .down: 159 | cmp al, DIR_UP 160 | je .exit 161 | mov byte [future_move_dir], DIR_DOWN 162 | jmp .exit 163 | 164 | .left: 165 | cmp al, DIR_RIGHT 166 | je .exit 167 | mov byte [future_move_dir], DIR_LEFT 168 | jmp .exit 169 | 170 | .up: 171 | cmp al, DIR_DOWN 172 | je .exit 173 | mov byte [future_move_dir], DIR_UP 174 | jmp .exit 175 | 176 | .exit: 177 | ret 178 | 179 | %macro PRINT_BUF_APPEND 1 180 | mov rax, print_buf 181 | add rax, [print_buf_len] 182 | mov rdx, %1 183 | mov rcx, %1_len 184 | add [print_buf_len], rcx 185 | call memcpy 186 | %endmacro 187 | 188 | %macro DRAW_CELL 0 189 | PRINT_BUF_APPEND cell_sym 190 | %endmacro 191 | 192 | %macro DRAW_COLOR_CELL 1 193 | PRINT_BUF_APPEND color_%1_seq 194 | DRAW_CELL 195 | PRINT_BUF_APPEND color_reset_seq 196 | %endmacro 197 | 198 | print_term_buf: 199 | mov rax, print_buf 200 | mov rdx, [print_buf_len] 201 | call print 202 | mov qword [print_buf_len], 0 203 | ret 204 | 205 | ; rax: cell 206 | draw_cell: 207 | cmp rax, MAP_WALL 208 | je .wall 209 | 210 | cmp rax, MAP_HEAD 211 | je .head 212 | 213 | cmp rax, MAP_BODY 214 | je .body 215 | 216 | cmp rax, MAP_APPLE 217 | je .apple 218 | 219 | jmp .free 220 | 221 | .free: 222 | DRAW_CELL 223 | jmp .exit 224 | 225 | .wall: 226 | DRAW_COLOR_CELL blue 227 | jmp .exit 228 | 229 | .head: 230 | DRAW_COLOR_CELL yellow 231 | jmp .exit 232 | 233 | .body: 234 | DRAW_COLOR_CELL bright_yellow 235 | jmp .exit 236 | 237 | .apple: 238 | DRAW_COLOR_CELL bright_red 239 | jmp .exit 240 | 241 | .exit: 242 | ret 243 | 244 | draw_map: 245 | push rbx 246 | push r11 247 | 248 | mov bh, 0 ; x counter 249 | mov bl, 0 ; y counter 250 | mov r11, 0 ; map cell 251 | 252 | .loop_y: 253 | .loop_x: 254 | mov rax, 0 255 | mov al, [map+r11] 256 | call draw_cell 257 | 258 | inc r11 259 | 260 | inc bh 261 | cmp bh, SIZE_X 262 | jne .loop_x 263 | 264 | PRINT_BUF_APPEND newline 265 | 266 | mov bh, 0 267 | 268 | inc bl 269 | cmp bl, SIZE_Y 270 | jne .loop_y 271 | 272 | PRINT_BUF_APPEND text_score 273 | 274 | call print_term_buf 275 | 276 | mov rax, [score] 277 | call print_num 278 | PRINT_NEW_LINE 279 | 280 | pop r11 281 | pop rbx 282 | ret 283 | 284 | clear_screen: 285 | PRINT_BUF_APPEND cur_reset_seq 286 | ret 287 | 288 | move_snake: 289 | mov al, [future_move_dir] 290 | mov [move_dir], al 291 | 292 | cmp al, DIR_RIGHT 293 | je .right 294 | 295 | cmp al, DIR_DOWN 296 | je .down 297 | 298 | cmp al, DIR_LEFT 299 | je .left 300 | 301 | cmp al, DIR_UP 302 | je .up 303 | 304 | .right: 305 | inc qword [snake_x] 306 | jmp .exit 307 | 308 | .down: 309 | inc qword [snake_y] 310 | jmp .exit 311 | 312 | .left: 313 | dec qword [snake_x] 314 | jmp .exit 315 | 316 | .up: 317 | dec qword [snake_y] 318 | jmp .exit 319 | 320 | .exit: 321 | mov rax, [snake_x] 322 | mov rdx, [snake_y] 323 | call map_coord_to_index ; rax: map index of old head 324 | call update_state 325 | call update_map_snake 326 | 327 | ret 328 | 329 | update_map_snake: 330 | mov rcx, [length] 331 | 332 | ; save tail position 333 | push qword [snake_cells_buf+rcx*8-8] 334 | 335 | .loop: 336 | dec rcx 337 | cmp rcx, 0 338 | jle .loop_exit 339 | 340 | ; shift data in the array, so 1st cell becomes 2nd, 341 | ; 2nd becomes 3rd, etc... 342 | mov rax, [snake_cells_buf+rcx*8-8] 343 | mov [snake_cells_buf+rcx*8], rax 344 | 345 | ; set map cell 346 | mov byte [map+rax], MAP_BODY 347 | 348 | jmp .loop 349 | 350 | .loop_exit: 351 | mov rcx, [length] 352 | 353 | mov rax, [snake_x] 354 | mov rdx, [snake_y] 355 | call map_coord_to_index 356 | 357 | ; set new head position 358 | mov [snake_cells_buf], rax 359 | mov byte [map+rax], MAP_HEAD 360 | 361 | ; restore tail position 362 | pop rax 363 | 364 | ; check if snake needs to grow 365 | cmp qword [eaten], 0 366 | jg .grow 367 | 368 | ; free old tail cell 369 | mov byte [map+rax], MAP_FREE 370 | jmp .exit 371 | 372 | .grow: 373 | mov byte [map+rax], al 374 | mov [snake_cells_buf+rcx*8], rax 375 | 376 | dec qword [eaten] 377 | inc qword [length] 378 | 379 | .exit: 380 | ret 381 | 382 | ; rax: new snake head pos 383 | update_state: 384 | mov dl, [map+rax] 385 | 386 | cmp dl, MAP_WALL 387 | je .die 388 | 389 | cmp dl, MAP_HEAD 390 | je .die 391 | 392 | cmp dl, MAP_BODY 393 | je .die 394 | 395 | cmp dl, MAP_APPLE 396 | je .grow 397 | 398 | jmp .exit 399 | 400 | .die: 401 | mov byte [status], STATUS_DIE 402 | jmp .exit 403 | 404 | .grow: 405 | inc qword [eaten] 406 | inc qword [score] 407 | call place_apple 408 | jmp .exit 409 | 410 | .exit: 411 | ret 412 | 413 | update: 414 | call move_snake 415 | call clear_screen 416 | call draw_map 417 | 418 | inc qword [frame] 419 | 420 | ret 421 | 422 | get_free_cells: 423 | mov rcx, 0 ; counter 424 | mov [map_free_buf_len], rcx 425 | 426 | .loop: 427 | cmp byte [map+rcx], MAP_FREE 428 | jne .loop_inc 429 | 430 | mov rax, [map_free_buf_len] 431 | mov [map_free_buf+rax*8], rcx 432 | inc qword [map_free_buf_len] 433 | 434 | .loop_inc: 435 | inc rcx 436 | cmp rcx, SIZE_N 437 | jne .loop 438 | 439 | ret 440 | 441 | place_apple: 442 | call get_free_cells 443 | 444 | ; amount of free cells 445 | mov rax, [map_free_buf_len] 446 | 447 | cmp rax, 0 448 | je .exit 449 | 450 | ; stores rand num from 0 to rax in rax 451 | call rand 452 | 453 | mov rdx, [map_free_buf+rax*8] 454 | 455 | mov byte [map+rdx], MAP_APPLE 456 | 457 | .exit: 458 | ret 459 | 460 | run: 461 | push rbx 462 | 463 | ; update count 464 | mov rbx, 0 465 | 466 | .loop: 467 | mov rax, input 468 | mov rdx, 1 469 | call poll 470 | 471 | call handle_key 472 | 473 | cmp byte [status], STATUS_EXIT 474 | je .exit 475 | 476 | cmp byte [status], STATUS_DIE 477 | je .die 478 | 479 | cmp rbx, MOVE_EVERY_TICK 480 | je .update 481 | 482 | inc rbx 483 | 484 | mov rax, UPD_DEL_SEC 485 | mov rdx, UPD_DEL_NANO 486 | call sleep 487 | 488 | jmp .loop 489 | 490 | .update: 491 | call update 492 | mov rbx, 0 493 | jmp .loop 494 | 495 | .die: 496 | PRINT_STR_DATA text_game_over 497 | 498 | .exit: 499 | pop rbx 500 | ret 501 | 502 | init: 503 | mov rax, [snake_x] 504 | mov rdx, [snake_y] 505 | call map_coord_to_index ;snake pos in rax 506 | 507 | ; add snake to map 508 | mov byte [map+rax], MAP_HEAD 509 | 510 | ; init snake_cells_buf 511 | mov qword [snake_cells_buf], rax 512 | 513 | ; add first apple 514 | call place_apple 515 | 516 | ; init print buffer 517 | mov qword [print_buf_len], 0 518 | 519 | call set_noncanon 520 | 521 | PRINT_BUF_APPEND cur_home_seq 522 | PRINT_BUF_APPEND clear_seq 523 | PRINT_BUF_APPEND text_controls 524 | PRINT_BUF_APPEND cur_hide_seq 525 | 526 | ret 527 | 528 | shutdown: 529 | PRINT_STR_DATA cur_show_seq 530 | call set_canon 531 | ret 532 | 533 | MAIN: 534 | call init 535 | 536 | call draw_map 537 | 538 | call run 539 | 540 | call shutdown 541 | 542 | mov rax, 0 543 | call exit 544 | 545 | ; vim:ft=nasm 546 | -------------------------------------------------------------------------------- /syscall.asm: -------------------------------------------------------------------------------- 1 | %include "print.mac" 2 | %include "syscall_int.mac" 3 | 4 | %define READ 0 5 | %define WRITE 1 6 | %define POLL 7 7 | %define IOCTL 16 8 | %define NANOSLEEP 35 9 | %define EXIT 60 10 | 11 | %macro SYS 1 12 | mov rax, %1 13 | syscall 14 | test rax, rax 15 | js syscall_err 16 | %endmacro 17 | 18 | section .data 19 | 20 | sleep_tv: 21 | .sec dq 0 22 | .usec dq 0 23 | 24 | section .text 25 | 26 | global sleep 27 | 28 | ; rax: seconds 29 | ; rdx: nanoseconds 30 | sleep: 31 | push rdi 32 | push rsi 33 | 34 | mov qword [sleep_tv.sec], rax 35 | mov qword [sleep_tv.usec], rdx 36 | mov rdi, sleep_tv ; timespec struct 37 | mov rsi, 0 ; don't store remaining time 38 | SYS NANOSLEEP 39 | 40 | pop rsi 41 | pop rdi 42 | ret 43 | 44 | global ioctl 45 | 46 | %define TCGETS 21505 ; attr to get struct 47 | %define TCPUTS 21506 ; attr to put struct 48 | 49 | ; rax - termios struct pointer 50 | ; rdx - 0: get, 1: put 51 | ioctl: 52 | push rdi 53 | push rsi 54 | 55 | add rdx, TCGETS 56 | mov rsi, rdx 57 | mov rdx, rax 58 | mov rdi, 0 59 | SYS IOCTL 60 | 61 | pop rsi 62 | pop rdi 63 | ret 64 | 65 | global exit 66 | 67 | ; rax: exit code 68 | exit: 69 | mov rdi, rax 70 | mov rax, EXIT 71 | syscall 72 | 73 | ; this part must never execute, 74 | ; but still... 75 | ret 76 | 77 | section .data 78 | 79 | ; poll function struct arg 80 | poll_fd: 81 | dd STDIN ; fd 82 | dw 1 ; events 83 | dw 0 ; revents 84 | 85 | section .text 86 | 87 | global poll 88 | 89 | ; rax: buffer 90 | ; rdx; count 91 | poll: 92 | push rdi 93 | push rsi 94 | 95 | push rax ; save buffer 96 | push rdx ; save count 97 | 98 | ; poll event 99 | mov rdi, poll_fd ; pointer to struct 100 | mov rsi, 1 ; only 1 fd - stdin 101 | mov rdx, 0 ; timeout 102 | SYS POLL 103 | 104 | mov rsi, rax 105 | pop rdx ; restore count 106 | pop rax ; restore buffer 107 | 108 | test rsi, rsi ; test if no event 109 | jz .no_event 110 | 111 | ; read input 112 | call read 113 | 114 | jmp .exit 115 | 116 | .no_event: 117 | mov byte [rax], -1 118 | 119 | .exit: 120 | pop rsi 121 | pop rdi 122 | ret 123 | 124 | global write 125 | 126 | ; rax: pointer to string 127 | ; rdx: string length 128 | ; rcx: fd 129 | write: 130 | push rdi 131 | push rsi 132 | 133 | mov rsi, rax ; string pointer 134 | mov rdi, rcx ; fd 135 | SYS WRITE 136 | 137 | pop rsi 138 | pop rdi 139 | ret 140 | 141 | global read 142 | 143 | ; rax: buffer 144 | ; rdx: count 145 | read: 146 | push rdi 147 | push rsi 148 | 149 | mov rsi, rax 150 | mov rdi, STDIN 151 | 152 | .loop: 153 | SYS READ 154 | 155 | ; exit if EOF 156 | test rax, rax 157 | je .exit 158 | 159 | ; exit if read as many bytes as requested 160 | cmp rax, rdx 161 | je .exit 162 | 163 | ; read less then requested, repeat syscall 164 | sub rdx, rax 165 | jmp .loop 166 | 167 | .exit: 168 | pop rsi 169 | pop rdi 170 | ret 171 | 172 | section .data 173 | 174 | DEF_STR_DATA text_syscall_err, "System call failed!", 10 175 | 176 | section .text 177 | 178 | syscall_err: 179 | mov rax, text_syscall_err 180 | mov rdx, text_syscall_err_len 181 | mov rcx, STDERR 182 | call write 183 | call exit 184 | 185 | ; vim:ft=nasm 186 | -------------------------------------------------------------------------------- /syscall.mac: -------------------------------------------------------------------------------- 1 | %ifndef SYSCALL_MAC 2 | %define SYSCALL_MAC 3 | 4 | %include "syscall_int.mac" 5 | 6 | extern ioctl 7 | extern sleep 8 | extern ioctl 9 | extern exit 10 | extern poll 11 | extern write 12 | 13 | %endif 14 | 15 | ; vim:ft=nasm 16 | -------------------------------------------------------------------------------- /syscall_int.mac: -------------------------------------------------------------------------------- 1 | %ifndef SYSCALL_INT_MAC 2 | %define SYSCALL_INT_MAC 3 | 4 | %define STDIN 0 5 | %define STDOUT 1 6 | %define STDERR 2 7 | 8 | struc termios_s 9 | resb 12 10 | .flags: resb 12 ; flags 11 | resb 44 12 | endstruc 13 | 14 | %endif 15 | 16 | ; vim:ft=nasm 17 | -------------------------------------------------------------------------------- /term.asm: -------------------------------------------------------------------------------- 1 | %include "syscall.mac" 2 | 3 | %define ICANON (1<<1) 4 | %define ECHO (1<<3) 5 | 6 | section .bss 7 | 8 | ; termios structs 9 | stty resb termios_s_size 10 | tty resb termios_s_size 11 | 12 | section .text 13 | 14 | global set_noncanon 15 | 16 | set_noncanon: 17 | ; store old termios 18 | mov rax, stty 19 | mov rdx, 0 20 | call ioctl 21 | 22 | ; store termios 23 | mov rax, tty 24 | mov rdx, 0 25 | call ioctl 26 | 27 | ; remove icanon and echo flags 28 | and dword [tty+termios_s.flags], (~ICANON) 29 | and dword [tty+termios_s.flags], (~ECHO) 30 | 31 | ; set attrs 32 | mov rax, tty 33 | mov rdx, 1 34 | call ioctl 35 | ret 36 | 37 | global set_canon 38 | 39 | set_canon: 40 | ; restore from saved termios struct 41 | mov rax, stty 42 | mov rdx, 1 43 | call ioctl 44 | ret 45 | 46 | ; vim:ft=nasm 47 | -------------------------------------------------------------------------------- /utils.asm: -------------------------------------------------------------------------------- 1 | section .text 2 | 3 | global memcpy 4 | global rand 5 | 6 | ; rax: destination 7 | ; rdx: source 8 | ; rcx: size 9 | memcpy: 10 | mov rsi, rdx 11 | mov rdi, rax 12 | 13 | cld ; increment in rep 14 | 15 | ; copy [rsi] to [rdi] 16 | ; increment rsi and rdi 17 | ; repeat rcx times 18 | rep movsb 19 | 20 | ret 21 | 22 | ; rax: upper limit 23 | ; returns: random num in rax 24 | rand: 25 | mov rcx, rax ; store upper limit 26 | 27 | rdrand rax ; store random number 28 | 29 | mov rdx, 0 30 | div rcx ; rdx now stores the remainder 31 | 32 | mov rax, rdx ; ret in rax 33 | ret 34 | 35 | ; vim:ft=nasm 36 | --------------------------------------------------------------------------------