# Examples

These are sample pieces of XC=BASIC source code.

## Example 1: Fibonacci series

```rem ** fibonacci series **

max = 32767
t1 = 0
t2 = 1

print "fibonacci series:"
loop:
print t1, " "
nx = t1 + t2
t1 = t2
t2 = nx
if nx < max then goto loop
end```

## Example 2: Kaleidoscope effect

Submitted by Eugenio Rapella. Note that it is valid to use line numbers instead of labels in XC=BASIC.

```1 const D12=12
2 const D55=55296
3 const D40=40
4 const D23=23

10 poke 53280,0:poke 53281,0
20 for t=0 to 999
30 poke 1024+t,81 : rem pallino
40 poke 55296+t,0 : rem nero
50 next t
60 let x!=cast!(D12*rnd%())
65 let y!=cast!(D12*rnd%())
67 let c!=rshift!(rnd!(),4)

70 m1 = D40 * y! : m2 = D40 * x!
75 n1 = D23 - x! : n2 = D23 - y!

90 poke D55+x!+m1,c!
95 poke D55+y!+m2,c!
100 poke D55+n1+m1,c!
105 poke D55+n2+m2,c!
110 poke D55+x!+D40*n2,c!
115 poke D55+y!+D40*n1,c!
120 poke D55+n1+D40*n2,c!
125 poke D55+n2+D40*n1,c!
130 goto 60```

## Example 3: Bouncing ball

```rem ** bouncing ball **

const SCREEN = 1024
const RASTER = 53266
const BALL = 81
const SPACE = 32

x = 1
y = 1
dx = 1
dy = 1
i = 0

gosub cls

loop:
charat x, y, BALL
gosub wait_frame
charat x, y, SPACE

if x = 0 then dx = 1
if x = 39 then dx = -1
if y = 0 then dy = 1
if y = 24 then dy = -1

x = x + dx
y = y + dy

goto loop

cls:
poke SCREEN + i,SPACE
inc i
if i < 1000 then goto cls
return

wait_frame:
if peek(RASTER) < 250 then goto wait_frame
return
```

## Example 4: USR() function

This example implements the << (left shift) operation on an integer for demonstration purposes.

```rem -- example implementation of the << (left shift)
rem -- operation using an USR function

print usr(@fn_leftShift, 896)
end

rem -- Function fn_leftShift(int argument)
rem --

fn_leftShift:
asm "
; The argument is available on the stack
tsx
asl \$0102,x
rol \$0101,x

; Note this is the only valid way to return from an user function
; The caller will pull back the result from the stack
jmp (\$02fe)
"```