; 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
 
by

Assembly Online Compiler

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.

About Assembly

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.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

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.

Procedures

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