Você está na página 1de 9

http://confect.co.uk/dl/public/snakeV2.

asm

# MIPS assembly program written by D.Taylor to be snake. .data # Store all important and starting data in this section, and make any other necessary notes # even if it does endup huge being enourmous. stageWidth: stageHeight: 16xScaling, 512x512 headX: memory headY: tailX: tailY: tailSize: size defaultDir: food01X: possitions in memory food01Y: food02X: food02Y: drawColour: bgColour: background .half 32 .half 32 .half 5 .half 5 .half 4 .half 5 .word 3 .word 0x01000000 .half 30 .half 30 .half 60 .half 60 .word 0x0022CC22 .word 0xFF003300 # Store colour to draw objects # Store colour to draw # Store player's initial tail # Store default dir as right # Store intial foods' # Store stage size # Usual settings $gp, 32x32, # Store player possition in

# I should explain at this point that I'll be using the otherwise unused alpha channel (leftmost two bytes) to store additional information relating to each tile. # Alpha channel meanings (00 to FF) should be noted also: # 00 to 31 kill player # 00 = wall # 01 = snake moving right # 02 = snake moving up # 03 = snake moving left # 04 = snake moving down # 32 to 3B grow player # 32 = food # FF is empty space .text # Main and other methods go here. Hopefully they'll cooperate and play nicely, because # I really want to play snake, and there's definately not any snake clones available elsewhere... Main: # Seed pseudorandom number generator 1 with lower order bytes of system clock ori $v0, $zero, 30 # Retrieve system time syscall or $a1, $zero, $a0 # Move the low order bytes to a1 ori $a0, $zero, 1 # Set generator number ori $v0, $zero, 40 # Seed the generator with it syscall Main_init: # Load player info lh $a0, headX # Get player's head possition

1 de 9

25/09/2013 11:58

http://confect.co.uk/dl/public/snakeV2.asm

lh $a1, headY jal CoordsToAddress memory nop or $s0, $zero, $v0 in s0 lh $a0, tailX lh $a1, tailY jal CoordsToAddress memory nop or $s1, $zero, $v0 in s1 lw $s3, tailSize lw $s4, defaultDir # Load colour info lw $s6, drawColour lw $s7, bgColour # Prepare the arena or $a0, $zero, $s7 colour jal FillMemory nop jal AddBoundaries nop # Draw player's initial head or $a0, $zero, $s6 or $a1, $zero, $s0 jal PaintMemory nop # Draw player's initial tail ori $a0, $s6, 0x01000000 right movement bytes or $a1, $zero, $s1 jal PaintMemory nop # Draw initial food jal GetFoodPos the new food nop ori $a0, $zero, 0x32FFFFFF attach the food bytes or $a1, $zero, $v0 jal PaintMemory nop Main_waitLoop:

# Calculate possition in stage # Store stage memory possition # Get player's tail possition # Calculate possition in stage # Store stage memory possition # Store tail size # in s3 # Store default dir in s4

# Store drawing colour in s6 # Store background colour in s7

# Clear stage to background

# Add walls

# Load draw colour # Load possition # Call the paint function # Load draw colour and attach # Load possition # Call the paint function

# Get a random possition for # Load white as the colour and # Load possition # Call the paint function

# Wait for the player to press a key jal Sleep # Zzzzzzzzzzz... nop lw $t0, 0xFFFF0000 # Retrieve transmitter control ready bit blez $t0, Main_waitLoop nop Main_gameLoop: # Check if a key was pressed

2 de 9

25/09/2013 11:58

http://confect.co.uk/dl/public/snakeV2.asm

# Main game loop in which the program should spend most of it's time jal Sleep that the game is playable nop blez $s3, Main_tailBegin food was picked up previously nop subi $s3, $s3, 1 b Main_skipTail length nop Main_tailBegin: lw $t7, ($s1) or $a0, $zero, $s7 or $a1, $zero, $s1 jal PaintMemory nop srl $t7, $t7, 24 tail colour data or $a0, $zero, $s1 or $a1, $zero, 1 address in the correct direction Main_tailRight: bne, $t7, 0x00000001, nop jal MoveRight nop j Main_tailDone nop Main_tailUp: bne, $t7, 0x00000002, nop jal MoveUp nop j Main_tailDone nop Main_tailLeft: bne, $t7, 0x00000003, nop jal MoveLeft nop j Main_tailDone nop Main_tailDown: bne, $t7, 0x00000004, nop jal MoveDown nop j Main_tailDone nop Main_tailNone: ori $v0, $zero, 10 syscall Main_tailDone: or $s1, $zero, $v0 possition Main_skipTail: # Now it's time to move the player's head jal GetDir # Get direction from keyboard # Set a0 to current possition # Set distance to one # Switch to move tail memory Main_tailUp # Right # Sleep for 60 miliseconds so

# Skip removing the tail if # Take one from food pick-up # Jump to skip reducing tail

# # # #

Backup tail colour data Remove tail peice Load addresss Call the paint function

# Extract direction byte from

Main_tailLeft # Up

Main_tailDown # Left

Main_tailNone # Down

# Syscall terminate # Store player's new tail

3 de 9

25/09/2013 11:58

http://confect.co.uk/dl/public/snakeV2.asm

nop or $t6, $zero, $v0 or $a0, $zero, $s0 ori $a1, $zero, 1 Main_headRight:

# Backup direction from keyboard # Load possition # Set distance to move

# Switch to move head memory address in the correct direction bne, $t6, 0x01000000, Main_headUp nop jal MoveRight # Right nop j Main_headDone nop Main_headUp: bne, $t6, 0x02000000, Main_headLeft nop jal MoveUp # Up nop j Main_headDone nop Main_headLeft: bne, $t6, 0x03000000, Main_headDown nop jal MoveLeft # Left nop j Main_headDone nop Main_headDown: bne, $t6, 0x04000000, Main_headNone nop jal MoveDown # Down nop j Main_headDone nop Main_headNone: or $t6, $zero, $s4 b Main_headRight nop Main_headDone: or $s4, $zero, $t6 previous direction add $a0, $s6, $t6 direction headed in the alpha bytes or $a1, $zero, $s0 jal PaintMemory nop or $s0, $zero, $v0 possition lw $t0, ($s0) into srl $t0, $t0, 24 ble $t0, 0x31, Main_reset nop blt $t0, 0x32, Main_noFood not food nop bgt $t0, 0x3B, Main_noFood nop addiu $s3, $s3, 2 # Otherwise add 2 to food register - this will extend the snake by two pixels next iteration # Exit if it's a wall # Skip adding to food if it's # Check status of tile moved # Redraw current possition with # Backup new direction as #default to previous direction

# Store player's new head

4 de 9

25/09/2013 11:58

http://confect.co.uk/dl/public/snakeV2.asm

# Draw new food jal GetFoodPos nop ori $a0, $zero, 0x32FFFFFF alpha bytes or $a1, $zero, $v0 jal PaintMemory nop Main_noFood: or $a0, $zero, $s6 or $a1, $zero, $s0 jal PaintMemory nop b Main_gameLoop the game nop Main_reset: # You died. Oh dead :C ori $v0, $zero, 32 ori $a0, $zero, 1200 syscall b Main nop Main_exit: ori $v0, $zero, 10 syscall

# Set colour to white with food

# Draw new head possition

# Loop around again to continue

# Syscall sleep # For this many miliseconds

# Syscall terminate

######################################################################################## ### # Sleep function for game loop # Takes none # Returns none Sleep: ori $v0, $zero, 32 # Syscall sleep ori $a0, $zero, 60 # For this many miliseconds syscall jr $ra # Return nop ######################################################################################## ### # Function to convert coordinates into stage memory addresses # Takes a0 = x, a1 = y # Returns v0 = address CoordsToAddress: or $v0, $zero, $a0 # Move x coordinate to v0 lh $a0, stageWidth # Load the screen width into a0 multu $a0, $a1 # Multiply y coordinate by the screen width nop mflo $a0 # Retrieve result from lo register addu $v0, $v0, $a0 # Add the result to the x coordinate and store in v0 sll $v0, $v0, 2 # Multiply v0 by 4 (bytes) using a logical shift addu $v0, $v0, $gp # Add gp to v0 to give stage memory address jr $ra # Return nop ######################################################################################## ### # Function to draw the given colour to the given stage memory address (gp) # Takes a0 = colour, a1 = address

5 de 9

25/09/2013 11:58

http://confect.co.uk/dl/public/snakeV2.asm

# Returns none PaintMemory: sw $a0, ($a1) # Set colour jr $ra # Return nop ######################################################################################## ### # Function to move a given stage memory address right by a given number of tiles # Takes a0 = address, a1 = distance # Returns v0 = new address MoveRight: #address + (distance*width*4) or $v0, $zero, $a0 # Move address to v0 sll $a0, $a1, 2 # Multiply distance by 4 using a logical shift add $v0, $v0, $a0 # Add result to v0 jr $ra # Return nop ######################################################################################## ### # Function to move a given stage memory address up by a given number of tiles # Takes a0 = address, a1 = distance # Returns v0 = new address MoveUp: or $v0, $zero, $a0 # Move address to v0 lh $a0, stageWidth # Load the screen width into a0 multu $a0, $a1 # Multiply distance by screen width nop mflo $a0 # Retrieve result from lo register sll $a0, $a0, 2 # Multiply v0 by 4 using a logical shift subu $v0, $v0, $a0 # Add result to v0 jr $ra # Return nop ######################################################################################## ### # Function to move a given stage memory address left by a given number of tiles # Takes a0 = address, a1 = distance # Returns v0 = new address MoveLeft: or $v0, $zero, $a0 # Move address to v0 sll $a0, $a1, 2 # Multiply distance by 4 using a logical shift subu $v0, $v0, $a0 # Subtract result from v0 jr $ra # Return nop ######################################################################################## ### # Function to move a given stage memory address down by a given number of tiles # Takes a0 = address, a1 = distance # Returns v0 = new address MoveDown: or $v0, $zero, $a0 # Move address to v0 lh $a0, stageWidth # Load the screen width into a0 multu $a0, $a1 # Multiply distance by screen width nop mflo $a0 # Retrieve result from lo register sll $a0, $a0, 2 # Multiply v0 by 4 using a logical shift addu $v0, $v0, $a0 # Subtract result from v0 jr $ra # Return nop ######################################################################################## ### # Function to retrieve input from the kyboard and return it as an alpha channel direction # Takes none

6 de 9

25/09/2013 11:58

http://confect.co.uk/dl/public/snakeV2.asm

# Returns v0 = direction GetDir: lw $t0, 0xFFFF0004 GetDir_right: bne, $t0, 100, GetDir_up nop ori $v0, $zero, 0x01000000 j GetDir_done nop GetDir_up: bne, $t0, 119, GetDir_left nop ori $v0, $zero, 0x02000000 j GetDir_done nop GetDir_left: bne, $t0, 97, GetDir_down nop ori $v0, $zero, 0x03000000 j GetDir_done nop GetDir_down: bne, $t0, 115, GetDir_none nop ori $v0, $zero, 0x04000000 j GetDir_done nop GetDir_none: GetDir_done:

# Load input value

# Right

# Up

# Left

# Down

# Do nothing jr $ra # Return nop ######################################################################################## ### # Function to fill the stage memory with a given colour # Takes a0 = colour # Returns none FillMemory: lh $a1, stageWidth # Calculate ending possition lh $a2, stageHeight multu $a1, $a2 # Multiply screen width by screen height nop mflo $a2 # Retreive total tiles sll $a2, $a2, 2 # Multiply by 4 add $a2, $a2, $gp # Add global pointer or $a1, $zero, $gp FillMemory_l: sw $a0, ($a1) add $a1, $a1, 4 blt $a1, $a2, FillMemory_l nop jr $ra # Return nop ######################################################################################## ### # Function to add boundary walls # Takes none # Returns none AddBoundaries: lh $a1, stageWidth # Calculate ending possition sll $a1, $a1, 2 # Multiply by 4 add $a2, $a1, $gp # Add global pointer # Set loop var to global pointer

7 de 9

25/09/2013 11:58

http://confect.co.uk/dl/public/snakeV2.asm

or $a1, $zero, $gp AddBoundaries_t: sw $s6, ($a1) add $a1, $a1, 4 blt $a1, $a2, AddBoundaries_t nop lh $a1, stageWidth lh $a2, stageHeight multu $a1, $a2 nop mflo $a2 sub $a2, $a2, $a1 sll $a2, $a2, 2 add $a2, $a2, $gp subi $a1, $a1, 1 sll $a3, $a1, 2 or $a1, $zero, $gp AddBoundaries_s: sw $s6, ($a1) add $a1, $a1, $a3 sw $s6, ($a1) add $a1, $a1, 4 blt $a1, $a2, AddBoundaries_s nop or $a3, $zero, $a1 lh $a1, stageWidth lh $a2, stageHeight multu $a1, $a2 nop mflo $a2 sll $a2, $a2, 2 add $a2, $a2, $gp or $a1, $zero, $a3 AddBoundaries_b: sw $s6, ($a1) add $a1, $a1, 4 blt $a1, $a2, AddBoundaries_b nop

# Set loop var to global pointer

# Calculate next ending condition # Multiply screen width by screen height # # # # Retreive total tiles Minus one width Multiply by 4 Add global pointer

# Take 1 from width # Multiply by 4 to get mem to add # Set loop var to global pointer

# backup a1 (current possition) # Calculate final ending possition # Multiply screen width by screen height # Retreive total tiles # Multiply by 4 # Add global pointer # restore previous possition

jr $ra # Return nop ######################################################################################## ### # Function to return a viable address for a new peice of food # Takes none # Returns v0 = viable address GetFoodPos: lh $a1, stageWidth # Calculate the max lh $a2, stageHeight multu $a1, $a2 # Multiply screen width by screen height nop mflo $a3 # Retreive total tiles sub $a3, $a3, 2 # take two for width boundaries sub $a3, $a3, $a2 # take two stage widths for height boundaries sub $a3, $a3, $a2

8 de 9

25/09/2013 11:58

http://confect.co.uk/dl/public/snakeV2.asm

GetFoodPos_tryAgain: ori $v0, $zero, 42 integer under this number ori $a0, $zero, 1 or $a1, $zero, $a3 syscall add $v0, $a0, 1 add $v0, $v0, $a2 sll $v0, $v0, 2 add $v0, $v0, $gp

# use syscall 42 to get a random

# add 1 for width border # add a width for height border # multiply by 4 for word size # add to global pointer

lw $t0, ($v0) # Check status of tile chosen at random srl $t0, $t0, 24 bne $t0, 0xFF, GetFoodPos_tryAgain # Try and find a different spot, this one is taken nop jr $ra # Return nop ######################################################################################## ###

9 de 9

25/09/2013 11:58

Você também pode gostar