Graphics Fun with Q8

To mix things up a little, this project is no math, just visuals. Here we are going to write a little code to bounce a "ball" from one side of the screen to the other. We're going to do this the simple way, with two loops. One loop to go across the screen right, and one to go back left again. A keen reader might spot a way to make this a single loop, probably reducing the code redundancy considerably. I encourage you to explore that avenue as a fun little bonus challenge.

C-like Program

while (true) {
    int position = 240;
    for (int i = 15; i > 0; i--) {
        //draw 0 @ array[position]
		position++;
        //draw 1 @ array[position]
    }
    for (int i = 15; i > 0; i--) {
        //draw 0 @ array[position]
        position--;
        //draw 1 @ array[position]
    }
}

Bytecode Program

Assembly Program

>0
inc-init:
LOAD A ball_start ; read in ball start position
LOAD B bounce_len ; read in bounce size
STORE A ball_pos ; make copy of ball start position
STORE B i ; make copy of bounce size as new i
JMP inc-loop

>32
inc-loop:
LOAD A i ; read in i
ISZERO A ; is i == 0?
JZ dec-init
DEC A ; i--
STORE A i ; write new i

>40
inc-draw:
SET A 0 ; load ball clear
STOREI A ball_pos ; clear old position
LOAD A ball_pos ; read position
INC A ; position++
STORE A ball_pos ; write position to grid
SET A 1 ; load ball
STOREI A ball_pos ; write ball to new position
JMP inc-loop

>64
dec-init:
LOAD A bounce_len ; read in bounce size
STORE A i ; make copy of bounce size as new i
JMP dec-loop

>80
dec-loop:
LOAD A i ; read in i
ISZERO A ; is i == 0?
JZ inc-init
DEC A ; i--
STORE A i ; write new i

>88
dec-draw:
SET A 0 ; load ball clear
STOREI A ball_pos ; clear old position
LOAD A ball_pos ; read position
DEC A ; position--
STORE A ball_pos ; write position to grid
SET A 1 ; load ball
STOREI A ball_pos ; write ball to new position
JMP dec-loop

; DATA
>128
ball_start: $240
bounce_len: $15
>144
ball_pos: $0
i: $0