; Simple Text-Based Snake Game in x86 Assembly section .data snake_body db 'O' ; Snake body character food db '*' ; Food character empty_space db '.' ; Empty space character board_size equ 10 ; Size of the game board section .bss snake_x resb 100 ; X positions of snake segments snake_y resb 100 ; Y positions of snake segments snake_length resb 1 ; Length of the snake food_x resb 1 ; X position of food food_y resb 1 ; Y position of food direction resb 1 ; Direction of movement (0: up, 1: right, 2: down, 3: left) game_over resb 1 ; Flag indicating game over section .text global _start _start: ; Initialize game state call init_game ; Main game loop game_loop: ; Render game board call render_board ; Move snake call move_snake ; Check for collisions call check_collisions ; Check for food eaten call check_food ; Check for game over cmp byte [game_over], 1 je game_over ; Pause for a moment call sleep ; Repeat game loop jmp game_loop game_over: ; Game over message mov eax, 4 ; syscall number for sys_write mov ebx, 1 ; file descriptor 1 (stdout) mov ecx, game_over_msg call print_string ; Exit program mov eax, 1 ; syscall number for sys_exit xor ebx, ebx ; exit code 0 int 0x80 ; call kernel init_game: ; Initialize snake position and direction mov byte [snake_length], 3 mov byte [snake_x], 5 mov byte [snake_y], 5 mov byte [direction], 1 ; Generate initial food position call generate_food ret render_board: ; Clear screen mov eax, 0x2 ; syscall number for sys_ioctl mov ebx, 1 ; file descriptor 1 (stdout) mov ecx, 0x5412 ; ioctl code for clear screen int 0x80 ; call kernel ; Render snake mov eax, 4 ; syscall number for sys_write mov ebx, 1 ; file descriptor 1 (stdout) mov ecx, snake_body mov edx, 1 ; message length call print_char ; Render food mov eax, 4 ; syscall number for sys_write mov ebx, 1 ; file descriptor 1 (stdout) mov ecx, food mov edx, 1 ; message length call print_char ; Render empty spaces mov eax, 4 ; syscall number for sys_write mov ebx, 1 ; file descriptor 1 (stdout) mov ecx, empty_space mov edx, 1 ; message length call print_char ; Move cursor to next line mov eax, 4 ; syscall number for sys_write mov ebx, 1 ; file descriptor 1 (stdout) mov ecx, newline mov edx, 1 ; message length call print_char ret move_snake: ; Update snake position based on direction cmp byte [direction], 0 je move_up cmp byte [direction], 1 je move_right cmp byte [direction], 2 je move_down cmp byte [direction], 3 je move_left ; Move up move_up: dec byte [snake_y] ret ; Move right move_right: inc byte [snake_x] ret ; Move down move_down: inc byte [snake_y] ret ; Move left move_left: dec byte [snake_x] ret check_collisions: ; Check if snake collided with walls or itself ; (not implemented in this example) ret check_food: ; Check if snake ate the food ; (not implemented in this example) ret generate_food: ; Generate random food position ; (not implemented in this example) ret sleep: ; Sleep for a moment to control game speed ; (not implemented in this example) ret print_string: ; Print a null-terminated string to stdout ; (not implemented in this example) ret print_char: ; Print a single character to stdout ; (not implemented in this example) ret ; Define game over message game_over_msg db 'Game over!', 0 ; Define newline character newline db 10, 0
Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly
and start coding.
Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.
Every assembler may have it's own assembly language designed for a specific computers or an operating system.
Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.
Assembly language usually consists of three sections,
Data section
To initialize variables and constants, buffer size these values doesn't change at runtime.
bss section
To declare variables
text section
_start
specifies the starting of this section where the actually code is written.
There are various define directives to allocate space for variables for both initialized and uninitialized data.
variable-name define-directive initial-value
Define Directive | Description | Allocated Space |
---|---|---|
DB | Define Byte | 1 byte |
DW | Define Word | 2 bytes |
DD | Define Doubleword | 4 bytes |
DQ | Define Quadword | 8 bytes |
DT | Define Ten Bytes | 10 bytes |
Define Directive | Description |
---|---|
RESB | Reserve a Byte |
RESW | Reserve a Word |
RESD | Reserve a Doubleword |
RESQ | Reserve a Quadword |
REST | Reserve a Ten Bytes |
Constants can be defined using
CONSTANT_NAME EQU regular-exp or value
%assign constant_name value
%define constant_name value
Loops are used to iterate a set of statements for a specific number of times.
mov ECX,n
L1:
;<loop body>
loop L1
where n specifies the no of times loops should iterate.
Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
procedure_name:
;procedure body
ret