Created
March 19, 2021 15:02
-
-
Save allexoll/7b72360556ad112cc566601dd0a27a07 to your computer and use it in GitHub Desktop.
Snake in arm-like ASM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define .score r1 | |
#define .var0 r2 | |
#define .var1 r3 | |
#define .count_0 r4 | |
#define .count_1 r5 | |
#define .count_2 r6 | |
#define .head_x r7 | |
#define .head_y r8 | |
#define .tail_x r9 | |
#define .tail_y r10 | |
#define .pomme_x r11 | |
#define .pomme_y r12 | |
&main | |
ldr count_0, #0 | |
ldr count_1, #0 | |
ldr var0, #0 | |
&clean_start | |
gmw count_0,count_1,#0 // clean the screen | |
add count_0,count_0,#1 | |
bcd count_0, var0, $clean_start | |
add count_1,count_1,#1 | |
bcd count_1,var0, $clean_start | |
&game_start | |
ldr head_x,#126 | |
ldr head_y,#128 | |
ldr tail_x,#126 | |
ldr tail_y,#128 | |
gmw tail_x,tail_y,#1 //init the snake (must be an odd length, here: 7) | |
add tail_x,tail_x,#1 | |
gmw tail_x,tail_y,#1 | |
add tail_x,tail_x,#1 | |
gmw tail_x,tail_y,#1 | |
add tail_x,tail_x,#1 | |
gmw tail_x,tail_y,#1 | |
add tail_x,tail_x,#1 | |
gmw tail_x,tail_y,#1 | |
add tail_x,tail_x,#1 | |
gmw tail_x,tail_y,#1 | |
add tail_x,tail_x,#1 | |
gmw tail_x,tail_y,#1 | |
add tail_x,tail_x,#1 | |
gmw tail_x,tail_y,#1 | |
add tail_x,tail_x,#1 | |
gmw tail_x,tail_y,#1 | |
ldr score,#0 // starting score | |
&pick_pomme | |
add r0,tail_y,#157 | |
add lr,pc,#3 | |
push lr | |
b $rand | |
ldr pomme_x,r0 | |
and pomme_x,pomme_x,#0xFE //make it even so the snake can eat it | |
add lr,pc,#3 | |
push lr | |
b $rand | |
ldr pomme_y,r0 | |
and pomme_y,pomme_y,#0xFE //make it even so the snake can eat it | |
ldr var0,#1 | |
gmr var1,pomme_x,pomme_y | |
gmr var1,pomme_x,pomme_y //double read for gram | |
bce var1,var0, $pick_pomme // pick while it is occupied | |
gmw pomme_x,pomme_y,#1 // draw apple | |
&snake_move | |
ldr count_0,#0 | |
ldr count_1,#0 | |
ldr count_2,#0 | |
ldr var0,#0 | |
ldr var1,#80 | |
sub var1,var1,score //score changes the speed | |
&time_div | |
add count_0,count_0,#1 | |
bcd count_0, var0, $time_div | |
add count_1,count_1,#1 | |
bcd count_1,var0, $time_div | |
add count_2,count_2,#1 | |
bcd count_2,var1, $time_div | |
// actual_snake_move | |
ldm var0, #0 //load direction | |
ldm var1, #1 // load previous direction | |
eor var1, var1,#0b00000010 | |
bcd var1,var0,$keep_on_going | |
eor var0,var1,#0b00000010 | |
&keep_on_going | |
stm var0,#1 | |
//chose head_direction_________________ | |
ldr count_0,#0 //future comparison | |
ldr var1,#0 | |
bce var1,var0,$head_dir_up | |
ldr var1,#1 | |
bce var1,var0,$head_dir_right | |
ldr var1,#2 | |
bce var1,var0,$head_dir_down | |
ldr var1,#3 | |
bce var1,var0,$head_dir_left | |
&head_dir_up | |
sub head_y,head_y,#1 | |
gmw head_x,head_y,#1 | |
sub head_y,head_y,#1 | |
gmr var0,head_x,head_y | |
gmr var0,head_x,head_y | |
bcd var0,count_0,$deal_with_obstacle | |
gmw head_x,head_y,#1 | |
b $quit_step_head | |
&head_dir_right | |
add head_x,head_x,#1 | |
gmw head_x,head_y,#1 | |
add head_x,head_x,#1 | |
gmr var0,head_x,head_y | |
gmr var0,head_x,head_y | |
bcd var0,count_0,$deal_with_obstacle | |
gmw head_x,head_y,#1 | |
b $quit_step_head | |
&head_dir_down | |
add head_y,head_y,#1 | |
gmw head_x,head_y,#1 | |
add head_y,head_y,#1 | |
gmr var0,head_x,head_y | |
gmr var0,head_x,head_y | |
bcd var0,count_0,$deal_with_obstacle | |
gmw head_x,head_y,#1 | |
b $quit_step_head | |
&head_dir_left | |
sub head_x,head_x,#1 | |
gmw head_x,head_y,#1 | |
sub head_x,head_x,#1 | |
gmr var0,head_x,head_y | |
gmr var0,head_x,head_y | |
bcd var0,count_0,$deal_with_obstacle | |
gmw head_x,head_y,#1 | |
b $quit_step_head | |
&deal_with_obstacle | |
bcd head_x,pomme_x, $lost | |
bcd head_y,pomme_y, $lost | |
add score,score, #4 | |
b $pick_pomme | |
//quit step head | |
&quit_step_head | |
ldr var1,#1 | |
sub count_0,tail_y,#1 | |
gmr var0, tail_x,count_0 | |
gmr var0, tail_x,count_0 | |
bce var0,var1, $tail_up | |
add count_0,tail_x,#1 | |
gmr var0,count_0,tail_y | |
gmr var0,count_0,tail_y | |
bce var0,var1, $tail_right | |
add count_0,tail_y,#1 | |
gmr var0,tail_x,count_0 | |
gmr var0,tail_x,count_0 | |
bce var0,var1, $tail_down | |
sub count_0,tail_x,#1 | |
gmr var0,count_0,tail_y | |
gmr var0,count_0,tail_y | |
bce var0,var1, $tail_left | |
&tail_up | |
gmw tail_x, tail_y, #0 | |
gmw tail_x, count_0,#0 | |
sub tail_y,tail_y,#2 | |
b $tail_quit | |
&tail_right | |
gmw tail_x, tail_y, #0 | |
gmw count_0, tail_y,#0 | |
add tail_x,tail_x,#2 | |
b $tail_quit | |
&tail_down | |
gmw tail_x,tail_y ,#0 | |
gmw tail_x,count_0,#0 | |
add tail_y,tail_y,#2 | |
b $tail_quit | |
&tail_left | |
gmw tail_x, tail_y, #0 | |
gmw count_0,tail_y,#0 | |
sub tail_x,tail_x,#2 | |
&tail_quit | |
b $snake_move | |
&lost | |
ldr count_0,#0 | |
ldr count_1,#0 | |
ldr count_2,#0 | |
ldr var0,#0 | |
ldr var1,#0 | |
&loose_time_div | |
add count_0,count_0,#1 | |
bcd count_0,count_2,$loose_time_div | |
nop//add count_1,count_1,#1 | |
nop//bcd count_1,count_2,$loose_time_div | |
gmw var0,var1,#1 | |
add var0,var0,#1 | |
bcd var0,count_2,$loose_time_div | |
add var1,var1,#1 | |
bcd var1,count_2,$loose_time_div | |
b $main | |
&rand | |
push count_0 | |
push count_1 | |
push count_2 | |
ldr count_0,#0xEE | |
ldr count_1,#3 | |
ldr count_2,#73 | |
mul count_1,count_1,r0 | |
mul r0,r0,r0 | |
mul count_0,count_0,r0 | |
add r0,count_1,count_2 | |
add r0,r0,count_0 | |
pop count_2 | |
pop count_1 | |
pop count_0 | |
pop pc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment