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: 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 3: 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)
  "

Example 4: Puralax!, a game written in XC=BASIC