BASIC programming language

From SEGGER Wiki
Revision as of 14:03, 4 July 2019 by Rolf (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

BASIC stands for Beginner's All-purpose Symbolic Instruction Code. BASIC is designed as an interactive programming language to introduce people to computer programming, designed for ease of use in the early 1960s. It is usually implemented as an interpreter (which interprets the program line by line) rather than a compiler (which translates the program as a whole to machine code).

BASIC implementations

BASIC's popularity peaked in the 1980s with the microcomputer revolution, with companies such as Apple, Commodore, Acorn, and Sinclair Research producing wildly popular microcomputers. Each of these had incompatible BASIC implementation making it impossible to write anything of significance to run on all (without major modification).

Later BASIC implementations added structured programming and many other features, but typically remained as interpreters rather than compilers; Microsoft's QuickBASIC and GW-BASIC were popular on the IBM PC, and the Microsoft BASIC family lives on in Microsoft Visual Basic .NET.

SEGGER has an implementation of BASIC, called emBASIC, which is targeted to microcontrollers and is written to use SEGGER's embedded software. It is showcased here, with a fully functional simulator: https://www.segger.com/showcase/#embasic

Examples

Classic BASIC uses line numbers to number program lines. Programs can be entered in any line number order and the BASIC system sorts them into ascending line number order for listing and execution.

Hello world

The classic program is a two-liner:

10 PRINT "Hello, world!"
20 END

When executed, the PRINT statement on line 10 writes a message on the terminal. The statement on line 20 stops program execution and returns control to the user. Some BASIC implementations require an END statement, whereas others do not.

Conway's game of life

Conways's game of life is a simulation of the evolution of a population of simple organisms. Every pixel represents one cell and can be in one of 2 states: Present(alive) or empty(dead), typically represented by 1 and 0, and an illuminated or black pixel. The rules for each generation are quite simple.

Rules

The organism will be alive in the next generation if it either

  • Is alive and has 2 or 3 neighbors
  • Is dead and has exactly 3 neigbors

Listing

' Conway's Game Of Life in emBASIC.
'
CLG
'
' Set number of cells in the Y direction
DY = 70
'
' Set the probability that a square contains a living cell
P = 0.25
'
' Compute dependent grid parameters
DX = GFX.WIDTH / GFX.HEIGHT * DY
DD = (GFX.WIDTH MIN GFX.HEIGHT) / DY
'
ORIGIN - DD / 2, - DD / 2
'
' Create first and second generation working storage.
DIM G0(DX + 2, DY + 2), G1(DX + 2, DY + 2)
'
' Random grid.
FOR X = 1 TO DX
  FOR Y = 1 TO DY
    G0(X, Y) = (RND 1) < P
    IF G0(X, Y) THEN
      COLOR %SPECTRUM RND 1
      FILL X * DD, Y * DD STEP DD - 2, DD - 2
    ENDIF
  NEXT Y
NEXT X
'
' Run life for 10 seconds.
WHILE TIMER < 10000
  '
  ' Compute next generation.
  FOR X = 1 TO DX
    FOR Y = 1 TO DY
      '
      ' Compute cell's neighbor count.
      NC = G0(X - 1, Y) + G0(X + 1, Y)
      NC = NC + G0(X - 1, Y - 1) + G0(X, Y - 1) + G0(X + 1, Y - 1)
      NC = NC + G0(X - 1, Y + 1) + G0(X, Y + 1) + G0(X + 1, Y + 1)
      '
      ' Run cell life rules for next generation.
      O = G0(X, Y)
      N = NC = 3 OR (O = 1 AND NC = 2)
      G1(X, Y) = N
      '
      ' Update display.
      IF N <> O THEN
        COLOR IFF(N, %SPECTRUM RND 1, %BLACK)
        FILL X * DD, Y * DD STEP DD - 2, DD - 2
      ENDIF
    NEXT Y
  NEXT X
  '
  ' Prepare for next generation.
  T = G0 : G0 = G1 : G1 = T
WEND
'
END


Find more information about it here: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

History

https://en.wikipedia.org/wiki/BASIC