Change comments to C-style to not interfer with preprocessor definitions
This commit is contained in:
@@ -2,24 +2,24 @@
|
|||||||
#define HEIGHT 40
|
#define HEIGHT 40
|
||||||
|
|
||||||
.bss
|
.bss
|
||||||
# Array of longs representing the game field
|
// Array of longs representing the game field
|
||||||
.align 4
|
.align 4
|
||||||
world: .space WIDTH*HEIGHT*4
|
world: .space WIDTH*HEIGHT*4
|
||||||
|
|
||||||
.data
|
.data
|
||||||
# Current length of the snake
|
// Current length of the snake
|
||||||
snakeLength: .long 0
|
snakeLength: .long 0
|
||||||
# Snake position
|
// Snake position
|
||||||
snakePosX: .long WIDTH / 2
|
snakePosX: .long WIDTH / 2
|
||||||
snakePosY: .long HEIGHT / 2
|
snakePosY: .long HEIGHT / 2
|
||||||
# Snake direction
|
// Snake direction
|
||||||
snakeDirX: .long 1
|
snakeDirX: .long 1
|
||||||
snakeDirY: .long 0
|
snakeDirY: .long 0
|
||||||
# Number of apples
|
// Number of apples
|
||||||
numApples: .long 0
|
numApples: .long 0
|
||||||
# Optional score-keeping
|
// Optional score-keeping
|
||||||
score: .long 0
|
score: .long 0
|
||||||
# Utility strings
|
// Utility strings
|
||||||
snibbles: .ascii "NIBBLES"
|
snibbles: .ascii "NIBBLES"
|
||||||
snibbles_end:
|
snibbles_end:
|
||||||
sgameover: .string " Game over! Score: %i "
|
sgameover: .string " Game over! Score: %i "
|
||||||
@@ -32,19 +32,19 @@ stringtest: .string "> %i %i %c\n"
|
|||||||
.text
|
.text
|
||||||
.global start_game
|
.global start_game
|
||||||
start_game:
|
start_game:
|
||||||
# snakeLength = len
|
// snakeLength = len
|
||||||
movl 4(%esp), %eax
|
movl 4(%esp), %eax
|
||||||
movl %eax, snakeLength
|
movl %eax, snakeLength
|
||||||
# numApples = n_apples
|
// numApples = n_apples
|
||||||
movl 8(%esp), %eax
|
movl 8(%esp), %eax
|
||||||
movl %eax, numApples
|
movl %eax, numApples
|
||||||
|
|
||||||
pushl %ebx
|
pushl %ebx
|
||||||
pushl %edi
|
pushl %edi
|
||||||
|
|
||||||
# Call nib_init
|
// Call nib_init
|
||||||
call nib_init
|
call nib_init
|
||||||
# Seed randomness
|
// Seed randomness
|
||||||
pushl $0
|
pushl $0
|
||||||
call time
|
call time
|
||||||
addl $4, %esp
|
addl $4, %esp
|
||||||
@@ -52,22 +52,22 @@ start_game:
|
|||||||
call srand
|
call srand
|
||||||
addl $4, %esp
|
addl $4, %esp
|
||||||
|
|
||||||
# Print our arguments
|
// Print our arguments
|
||||||
#pushl numApples
|
//pushl numApples
|
||||||
#pushl snakeLength
|
//pushl snakeLength
|
||||||
#pushl $string1
|
//pushl $string1
|
||||||
#call printf
|
//call printf
|
||||||
#addl $4*3, %esp
|
//addl $4*3, %esp
|
||||||
|
|
||||||
# Print worm position
|
// Print worm position
|
||||||
#pushl snakePosY
|
//pushl snakePosY
|
||||||
#pushl snakePosX
|
//pushl snakePosX
|
||||||
#pushl $string2
|
//pushl $string2
|
||||||
#call printf
|
//call printf
|
||||||
#addl $4*3, %esp
|
//addl $4*3, %esp
|
||||||
|
|
||||||
# Initialize the snake array
|
// Initialize the snake array
|
||||||
# Set every cell to a time value greater than the snake length
|
// Set every cell to a time value greater than the snake length
|
||||||
movl $world, %edi
|
movl $world, %edi
|
||||||
movl snakeLength, %eax
|
movl snakeLength, %eax
|
||||||
movl $WIDTH*HEIGHT, %ebx
|
movl $WIDTH*HEIGHT, %ebx
|
||||||
@@ -78,7 +78,7 @@ start_game:
|
|||||||
cmpl %ebx, %ecx
|
cmpl %ebx, %ecx
|
||||||
jl 1b
|
jl 1b
|
||||||
|
|
||||||
# Spawn initial apples
|
// Spawn initial apples
|
||||||
movl numApples, %ecx
|
movl numApples, %ecx
|
||||||
1:
|
1:
|
||||||
pushl %ecx
|
pushl %ecx
|
||||||
@@ -86,9 +86,9 @@ start_game:
|
|||||||
popl %ecx
|
popl %ecx
|
||||||
loop 1b
|
loop 1b
|
||||||
|
|
||||||
# Loop forever
|
// Loop forever
|
||||||
loop:
|
loop:
|
||||||
# Sleep for a while
|
// Sleep for a while
|
||||||
pushl $250*1000
|
pushl $250*1000
|
||||||
pushl %ecx
|
pushl %ecx
|
||||||
call usleep
|
call usleep
|
||||||
@@ -100,49 +100,49 @@ loop:
|
|||||||
popl %ebx
|
popl %ebx
|
||||||
ret
|
ret
|
||||||
|
|
||||||
# Update stuff
|
// Update stuff
|
||||||
tick:
|
tick:
|
||||||
movl $world, %edi
|
movl $world, %edi
|
||||||
|
|
||||||
# Increment cell times
|
// Increment cell times
|
||||||
movl snakeLength, %ebx
|
movl snakeLength, %ebx
|
||||||
xorl %ecx, %ecx
|
xorl %ecx, %ecx
|
||||||
1:
|
1:
|
||||||
movl (%edi, %ecx, 4), %eax
|
movl (%edi, %ecx, 4), %eax
|
||||||
# If the value is negative it's an apple we don't want to disappear
|
// If the value is negative it's an apple we don't want to disappear
|
||||||
test %eax, %eax
|
test %eax, %eax
|
||||||
js 2f
|
js 2f
|
||||||
# No need to increment if it's already greater than snakeLength
|
// No need to increment if it's already greater than snakeLength
|
||||||
cmpl %ebx, %eax
|
cmpl %ebx, %eax
|
||||||
jg 2f
|
jg 2f
|
||||||
# Increment the cell value
|
// Increment the cell value
|
||||||
incl %eax
|
incl %eax
|
||||||
movl %eax, (%edi, %ecx, 4)
|
movl %eax, (%edi, %ecx, 4)
|
||||||
2: incl %ecx # Loop increment
|
2: incl %ecx // Loop increment
|
||||||
cmpl $WIDTH*HEIGHT, %ecx
|
cmpl $WIDTH*HEIGHT, %ecx
|
||||||
jne 1b
|
jne 1b
|
||||||
|
|
||||||
# Input
|
// Input
|
||||||
call nib_poll_kbd
|
call nib_poll_kbd
|
||||||
# Down
|
// Down
|
||||||
movl $258, %ebx
|
movl $258, %ebx
|
||||||
cmpl %eax, %ebx
|
cmpl %eax, %ebx
|
||||||
jne 1f
|
jne 1f
|
||||||
movl $0, snakeDirX
|
movl $0, snakeDirX
|
||||||
movl $1, snakeDirY
|
movl $1, snakeDirY
|
||||||
1: # Up
|
1: // Up
|
||||||
movl $259, %ebx
|
movl $259, %ebx
|
||||||
cmpl %eax, %ebx
|
cmpl %eax, %ebx
|
||||||
jne 1f
|
jne 1f
|
||||||
movl $0, snakeDirX
|
movl $0, snakeDirX
|
||||||
movl $-1, snakeDirY
|
movl $-1, snakeDirY
|
||||||
1: # Left
|
1: // Left
|
||||||
movl $260, %ebx
|
movl $260, %ebx
|
||||||
cmpl %eax, %ebx
|
cmpl %eax, %ebx
|
||||||
jne 1f
|
jne 1f
|
||||||
movl $-1, snakeDirX
|
movl $-1, snakeDirX
|
||||||
movl $0, snakeDirY
|
movl $0, snakeDirY
|
||||||
1: # Right
|
1: // Right
|
||||||
movl $261, %ebx
|
movl $261, %ebx
|
||||||
cmpl %eax, %ebx
|
cmpl %eax, %ebx
|
||||||
jne 1f
|
jne 1f
|
||||||
@@ -150,95 +150,95 @@ tick:
|
|||||||
movl $0, snakeDirY
|
movl $0, snakeDirY
|
||||||
1:
|
1:
|
||||||
|
|
||||||
# Move snake
|
// Move snake
|
||||||
# snakePosX = (snakePosX + snakeDirY + WIDTH) % WIDTH
|
// snakePosX = (snakePosX + snakeDirY + WIDTH) % WIDTH
|
||||||
movl snakePosX, %eax
|
movl snakePosX, %eax
|
||||||
movl snakeDirX, %ebx
|
movl snakeDirX, %ebx
|
||||||
addl %ebx, %eax
|
addl %ebx, %eax
|
||||||
addl $WIDTH, %eax # For modulo wrap-around
|
addl $WIDTH, %eax // For modulo wrap-around
|
||||||
xorl %edx, %edx
|
xorl %edx, %edx
|
||||||
movl $WIDTH, %ecx
|
movl $WIDTH, %ecx
|
||||||
divl %ecx
|
divl %ecx
|
||||||
mov %edx, snakePosX
|
mov %edx, snakePosX
|
||||||
# snakePosY = (snakePosY + snakeDirY + HEIGHT) % HEIGHT
|
// snakePosY = (snakePosY + snakeDirY + HEIGHT) % HEIGHT
|
||||||
movl snakePosY, %eax
|
movl snakePosY, %eax
|
||||||
movl snakeDirY, %ebx
|
movl snakeDirY, %ebx
|
||||||
addl %ebx, %eax
|
addl %ebx, %eax
|
||||||
addl $HEIGHT, %eax # For modulo wrap-around
|
addl $HEIGHT, %eax // For modulo wrap-around
|
||||||
xorl %edx, %edx
|
xorl %edx, %edx
|
||||||
movl $HEIGHT, %ecx
|
movl $HEIGHT, %ecx
|
||||||
divl %ecx
|
divl %ecx
|
||||||
mov %edx, snakePosY
|
mov %edx, snakePosY
|
||||||
|
|
||||||
# Get value at head position
|
// Get value at head position
|
||||||
movl snakePosY, %eax
|
movl snakePosY, %eax
|
||||||
movl $WIDTH, %edx
|
movl $WIDTH, %edx
|
||||||
mull %edx
|
mull %edx
|
||||||
addl snakePosX, %eax
|
addl snakePosX, %eax
|
||||||
movl (%edi, %eax, 4), %edx
|
movl (%edi, %eax, 4), %edx
|
||||||
|
|
||||||
# Collision with apple if negative
|
// Collision with apple if negative
|
||||||
test %edx, %edx
|
test %edx, %edx
|
||||||
js 1f
|
js 1f
|
||||||
# No need to increment if it's already greater than snakeLength
|
// No need to increment if it's already greater than snakeLength
|
||||||
movl snakeLength, %ebx
|
movl snakeLength, %ebx
|
||||||
cmpl %ebx, %edx
|
cmpl %ebx, %edx
|
||||||
jg 2f
|
jg 2f
|
||||||
# End the game when the snake collides with itself
|
// End the game when the snake collides with itself
|
||||||
jmp gameOver
|
jmp gameOver
|
||||||
1: # Spawn a new apple
|
1: // Spawn a new apple
|
||||||
pushl %edi
|
pushl %edi
|
||||||
pushl %eax
|
pushl %eax
|
||||||
call spawnApple
|
call spawnApple
|
||||||
popl %eax
|
popl %eax
|
||||||
popl %edi
|
popl %edi
|
||||||
# Extend the snake!
|
// Extend the snake!
|
||||||
incl snakeLength
|
incl snakeLength
|
||||||
2:
|
2:
|
||||||
|
|
||||||
# Set time value to 0 at snake head position
|
// Set time value to 0 at snake head position
|
||||||
# i = x + y * WIDTH
|
// i = x + y * WIDTH
|
||||||
# %edi = %ebx + %ecx * $WIDTH
|
// %edi = %ebx + %ecx * $WIDTH
|
||||||
movl $0, (%edi, %eax, 4)
|
movl $0, (%edi, %eax, 4)
|
||||||
|
|
||||||
jmp draw
|
jmp draw
|
||||||
#jmp debug_draw
|
//jmp debug_draw
|
||||||
|
|
||||||
# Draw stuff
|
// Draw stuff
|
||||||
draw:
|
draw:
|
||||||
# Loop Y
|
// Loop Y
|
||||||
movl $HEIGHT, %ecx
|
movl $HEIGHT, %ecx
|
||||||
1:
|
1:
|
||||||
# Loop X
|
// Loop X
|
||||||
movl $WIDTH, %ebx
|
movl $WIDTH, %ebx
|
||||||
2:
|
2:
|
||||||
# Save loop registers
|
// Save loop registers
|
||||||
pushl %ecx
|
pushl %ecx
|
||||||
pushl %ebx
|
pushl %ebx
|
||||||
subl $1, %ecx
|
subl $1, %ecx
|
||||||
subl $1, %ebx
|
subl $1, %ebx
|
||||||
|
|
||||||
# Call nib_put_scr
|
// Call nib_put_scr
|
||||||
|
|
||||||
# i = x + y * WIDTH
|
// i = x + y * WIDTH
|
||||||
# %eax = %ebx + %ecx * $WIDTH
|
// %eax = %ebx + %ecx * $WIDTH
|
||||||
movl %ecx, %eax
|
movl %ecx, %eax
|
||||||
movl $WIDTH, %edx
|
movl $WIDTH, %edx
|
||||||
mull %edx
|
mull %edx
|
||||||
addl %ebx, %eax
|
addl %ebx, %eax
|
||||||
# Fetch the value of the cell
|
// Fetch the value of the cell
|
||||||
movl $world, %edi
|
movl $world, %edi
|
||||||
movl (%edi, %eax, 4), %eax
|
movl (%edi, %eax, 4), %eax
|
||||||
|
|
||||||
# Compare with the snake length
|
// Compare with the snake length
|
||||||
cmpl snakeLength, %eax
|
cmpl snakeLength, %eax
|
||||||
# If the value is negative, it's an apple
|
// If the value is negative, it's an apple
|
||||||
test %eax, %eax
|
test %eax, %eax
|
||||||
js 4f
|
js 4f
|
||||||
# If it's greater or equal to snakeLength, it's empty
|
// If it's greater or equal to snakeLength, it's empty
|
||||||
cmpl snakeLength, %eax
|
cmpl snakeLength, %eax
|
||||||
jge 3f
|
jge 3f
|
||||||
# Else, it's part of the snake
|
// Else, it's part of the snake
|
||||||
pushl $'@'
|
pushl $'@'
|
||||||
jmp 5f
|
jmp 5f
|
||||||
3: pushl $' '
|
3: pushl $' '
|
||||||
@@ -246,89 +246,89 @@ draw:
|
|||||||
4: pushl $'o'
|
4: pushl $'o'
|
||||||
5:
|
5:
|
||||||
|
|
||||||
movl %ecx, %eax # Y
|
movl %ecx, %eax // Y
|
||||||
pushl %eax
|
pushl %eax
|
||||||
|
|
||||||
movl %ebx, %eax # X
|
movl %ebx, %eax // X
|
||||||
pushl %eax
|
pushl %eax
|
||||||
|
|
||||||
call nib_put_scr
|
call nib_put_scr
|
||||||
addl $4*3, %esp
|
addl $4*3, %esp
|
||||||
|
|
||||||
# Restore loop registers
|
// Restore loop registers
|
||||||
popl %ebx
|
popl %ebx
|
||||||
popl %ecx
|
popl %ecx
|
||||||
|
|
||||||
decl %ebx # Decrement X
|
decl %ebx // Decrement X
|
||||||
jnz 2b # End of X loop
|
jnz 2b // End of X loop
|
||||||
loop 1b # End of Y loop
|
loop 1b // End of Y loop
|
||||||
|
|
||||||
jmp loop
|
jmp loop
|
||||||
|
|
||||||
# Debug drawing
|
// Debug drawing
|
||||||
debug_draw:
|
debug_draw:
|
||||||
# Loop Y
|
// Loop Y
|
||||||
movl $HEIGHT, %ecx
|
movl $HEIGHT, %ecx
|
||||||
1:
|
1:
|
||||||
# Loop X
|
// Loop X
|
||||||
movl $WIDTH, %ebx
|
movl $WIDTH, %ebx
|
||||||
2:
|
2:
|
||||||
# Save loop registers
|
// Save loop registers
|
||||||
pushl %ecx
|
pushl %ecx
|
||||||
pushl %ebx
|
pushl %ebx
|
||||||
subl $1, %ecx
|
subl $1, %ecx
|
||||||
subl $1, %ebx
|
subl $1, %ebx
|
||||||
|
|
||||||
# Call nib_put_scr
|
// Call nib_put_scr
|
||||||
|
|
||||||
# i = x + y * WIDTH
|
// i = x + y * WIDTH
|
||||||
# %edi = %ebx + %ecx * $WIDTH
|
// %edi = %ebx + %ecx * $WIDTH
|
||||||
movl %ecx, %eax
|
movl %ecx, %eax
|
||||||
movl $WIDTH, %edx
|
movl $WIDTH, %edx
|
||||||
mull %edx
|
mull %edx
|
||||||
addl %ebx, %eax
|
addl %ebx, %eax
|
||||||
# Fetch the value of the cell
|
// Fetch the value of the cell
|
||||||
movl $world, %edi
|
movl $world, %edi
|
||||||
movl (%edi, %eax, 4), %eax
|
movl (%edi, %eax, 4), %eax
|
||||||
pushl %eax
|
pushl %eax
|
||||||
|
|
||||||
movl %ecx, %eax # Y
|
movl %ecx, %eax // Y
|
||||||
pushl %eax
|
pushl %eax
|
||||||
|
|
||||||
movl %ebx, %eax # X
|
movl %ebx, %eax // X
|
||||||
pushl %eax
|
pushl %eax
|
||||||
|
|
||||||
call nib_put_scr
|
call nib_put_scr
|
||||||
addl $4*3, %esp
|
addl $4*3, %esp
|
||||||
|
|
||||||
# Restore loop registers
|
// Restore loop registers
|
||||||
popl %ebx
|
popl %ebx
|
||||||
popl %ecx
|
popl %ecx
|
||||||
|
|
||||||
decl %ebx # Decrement X
|
decl %ebx // Decrement X
|
||||||
jnz 2b # End of X loop
|
jnz 2b // End of X loop
|
||||||
loop 1b # End of Y loop
|
loop 1b // End of Y loop
|
||||||
|
|
||||||
jmp loop
|
jmp loop
|
||||||
|
|
||||||
gameOver:
|
gameOver:
|
||||||
# Calculate string length
|
// Calculate string length
|
||||||
movl $sgameover_end, %eax
|
movl $sgameover_end, %eax
|
||||||
subl $sgameover, %eax
|
subl $sgameover, %eax
|
||||||
subl $1, %eax # Discount null terminator
|
subl $1, %eax // Discount null terminator
|
||||||
|
|
||||||
# Calculate X-pos
|
// Calculate X-pos
|
||||||
movl $2, %ebx
|
movl $2, %ebx
|
||||||
xorl %edx, %edx
|
xorl %edx, %edx
|
||||||
divl %ebx
|
divl %ebx
|
||||||
negl %eax
|
negl %eax
|
||||||
addl $WIDTH/2, %eax
|
addl $WIDTH/2, %eax
|
||||||
|
|
||||||
# Print game over message
|
// Print game over message
|
||||||
pushl snakeLength
|
pushl snakeLength
|
||||||
pushl $sgameover
|
pushl $sgameover
|
||||||
pushl %eax # X
|
pushl %eax // X
|
||||||
pushl $HEIGHT/2 # Y
|
pushl $HEIGHT/2 // Y
|
||||||
call mvprintw
|
call mvprintw
|
||||||
add $4*3, %esp
|
add $4*3, %esp
|
||||||
call refresh
|
call refresh
|
||||||
@@ -341,13 +341,13 @@ gameOver:
|
|||||||
spawnApple:
|
spawnApple:
|
||||||
pushl %ebx
|
pushl %ebx
|
||||||
|
|
||||||
# %ecx = rand() % HEIGHT
|
// %ecx = rand() % HEIGHT
|
||||||
call rand
|
call rand
|
||||||
xorl %edx, %edx
|
xorl %edx, %edx
|
||||||
movl $HEIGHT, %ebx
|
movl $HEIGHT, %ebx
|
||||||
divl %ebx
|
divl %ebx
|
||||||
movl %edx, %ecx
|
movl %edx, %ecx
|
||||||
# %ebx = rand() % WIDTH
|
// %ebx = rand() % WIDTH
|
||||||
pushl %ecx
|
pushl %ecx
|
||||||
call rand
|
call rand
|
||||||
xorl %edx, %edx
|
xorl %edx, %edx
|
||||||
@@ -356,14 +356,14 @@ spawnApple:
|
|||||||
movl %edx, %ebx
|
movl %edx, %ebx
|
||||||
popl %ecx
|
popl %ecx
|
||||||
|
|
||||||
# i = x + y * WIDTH
|
// i = x + y * WIDTH
|
||||||
# %eax = %ebx + %ecx * $WIDTH
|
// %eax = %ebx + %ecx * $WIDTH
|
||||||
movl %ecx, %eax
|
movl %ecx, %eax
|
||||||
movl $WIDTH, %edx
|
movl $WIDTH, %edx
|
||||||
mull %edx
|
mull %edx
|
||||||
addl %ebx, %eax
|
addl %ebx, %eax
|
||||||
|
|
||||||
# Set the cell negative to represent an apple
|
// Set the cell negative to represent an apple
|
||||||
movl $world, %ecx
|
movl $world, %ecx
|
||||||
movl $-1, %edx
|
movl $-1, %edx
|
||||||
movl %edx, (%ecx, %eax, 4)
|
movl %edx, (%ecx, %eax, 4)
|
||||||
|
|||||||
Reference in New Issue
Block a user