Difference between revisions of "BASIC programming language"

From SEGGER Wiki
Jump to: navigation, search
m (Later BASIC implementations)
m
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
[[Category:Knowledge Base]]
BASIC is a programming language designed for ease of use in the early 1960s.
 
The name stands for BASIC Beginner's All-purpose Symbolic Instruction Code.
+
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==
==Overview==
 
   
  +
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).
BASIC is designed as an interactive programming language to introduce people to computer programming. As such, 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).
 
  +
  +
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==
 
==Examples==
Line 10: Line 17:
 
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.
 
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:
 
The classic program is a two-liner:
   
Line 17: Line 25:
 
When executed, the <code>PRINT</code> 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 <code>END</code> statement, whereas others do not.
 
When executed, the <code>PRINT</code> 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 <code>END</code> statement, whereas others do not.
   
  +
===Conway's game of life===
==Later BASIC implementations==
 
   
  +
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.
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).
 
   
  +
==== Rules ====
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.
 
  +
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====
  +
  +
<syntaxhighlight lang="basic">
  +
' 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
  +
</syntaxhighlight>
  +
   
  +
Find more information about it here: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
SEGGER has an implementation of BASIC, called [https://www.segger.com/showcase/ emBASIC], that is targeted to micrcontrollers, and is written to only use SEGGER's embedded software.
 
   
 
==History==
 
==History==

Latest revision as of 14:03, 4 July 2019

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