os8 basic reference

Upload: simoneforbesxua

Post on 02-Jun-2018

244 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Os8 Basic Reference

    1/58

    CHAPTER 6

    BASIC

    6.1

    INTRODUCTION

    OS/78

    BASIC* is an interactive program ming language used in scientific and business environments t o solve mathe-

    matical problems with a minimum o f programming effor t. It also is used by educators and stud ents as a problem-

    solving to ol and as an aid to learning through programmed in struction and simulation.

    In m any respects the BASIC language is similar to oth er programm ing languages (such as FOR TRA N IV), but BASIC

    is aimed at facilitating commu nication between the user and the com puter. BASIC simply requires th at yo u type in

    the comp utational procedure as a series of numbered statements, making use of co mm on English words and familiar

    mathematical notations. Because of the small num ber of comm ands necessary and its easy application in solving

    problems, BASIC is an easy com puter language to learn . With experience, the advanced techniques available can be

    added in the language to perform m ore intricate manipulations or to express a problem m ore efficiently and

    concisely.

    6.2 MAJOR COMPONENTS OF

    OS/78

    BASIC

    The BASIC subsystem has four major components

    1. BASIC Editor (BASICS V)

    2 .

    Compiler (BCOM PSV)

    3 . Loader (BLOADSV)

    4.

    BASIC Run Time System (BR TS SV , BASIC.AF, BASIC SF, BASIC.FF)

    Th e BASIC editor is used to create and edit program source files. During this process, the editor creates a file called

    BASIC.WS containing the current program.

    Once the program has been prepared for execution, entering a RUN command cm ses the editor to chain to the

    BASIC compiler. The com piler converts the statements in BASIC.WS into relocatable binary in structions.

    Following compilation, the BASIC loader is automatically requested. The loade : onverts the relocatable binary

    data outp ut by the compiler into executable form and loads the result into mem ory.

    The BASIC loader then chains to the BASIC Run Time System (BRTS) w h ch executes the program. The modules

    BASIC.AF, BASIC.FF, and B ASIC SF are overlays to BRT SSV .

    6.3 BASIC INSTRUCTION REPERTOIRE

    BASIC instructions and commands can be g rouped in th ree categories as follows:

    1. BASIC Editor comm ands that allow you t o

    a. Create or mod ify a program,

    b. Execute a program,

    c. Retrieve a program from d iskette, and

    d. Save a program .

    *BASIC is a registered trademark

    of

    the trustees of Dartmouth College.

    6-1

  • 8/11/2019 Os8 Basic Reference

    2/58

    BASIC

    2 .

    3 .

    BASIC statements, comprising the BASIC language, tha t are th e building blocks used t o create and struc-

    ture BASIC programs.

    BASIC Fu nctio ns, represented b y subroutines, that are built into BASIC primarily t o facilitate problem

    solving activities.

    6.4

    CALLING BASIC

    To enter the BASIC subsystem, type

    e

    B A S

    C

    C

    -

    in response to the Monitor do t. This command invokes the BASIC editor.

    6.5

    BASIC EDITO R COMMANDS

    6.5.1

    Using the BASIC Editor

    The BASIC editor incorp orates all the too ls and capabilities necessary to create , correct, mod ify, execu te, save and

    retrieve BASIC programs. After calling BASIC, the BASIC editor respond s with the displayed que ry

    Editing is now co ntinued in one of two ways:

    1.

    Typing NEW with a file name instru cts the system to initiate the creation

    of

    a new

    file.

    2 .

    Typing OL D with a file name in structs the system to retrieve an old file containing a previously-generated

    program.

    Figure 6-1 summ arizes user actions implem ented by the BASIC editor. This figure shows two arbitrarily selected

    file names (MAKER/ALTER). File names may contain no more than six alphanumeric symbols.

    The left side of Figure

    6-1

    shows the types and patterns o f activities tha t yo u ordinarily pursue after typ ing the NEW

    comm and. The right side of the illustration shows activities freque ntly carried ou t after typing the OLD com mand .

    Note tha t mo st of the BASIC editor com mands appear on both sides of the figure. Only the sequence in which they

    are used differs.

    6.5.2

    BASIC Editor C ommands

    This section summ arizes the BASIC editor commands. Letters that are not required in naming the comm and are

    shown as lower case in the Com mand/Param eter d escription. Fo r exam ple, only NE is required t o be recognized by

    the BASIC editor as the NEW co mm and .

    NOTE

    The R ETU RN key must be pressed following each BASIC

    editor command.

    6.5.2.1

    NEW Command

    The NEW co mm and clears the mem ory w orksp rce and sp ecifies the nam e of the pro -

    gram that is to be inp ut.

    Command Parameters

    NEW file [.ex]

    file .ex is the new file name and extension of the program ab ou t to be typed in. If the extension

    is om itte d, the ed itor assigns .BA.

    6-2

  • 8/11/2019 Os8 Basic Reference

    3/58

    BASIC

    Correction

    Keys

    J

    NEW

    F I LE

    NAME

    /

    I

    Add BASIC Statements

    I

    PRESERVE

    Your Program Using

    SAVE

    8 M t C

    / \

    \

    QLES

    A R E A D Y

    CREATE

    Your Program

    u;i;;;;\

    MODIFY

    f

    our Program Using

    REVIEW

    Your Program Using

    PRESERVE

    Your Program Using

    SJIV-15

    EXECUTE

    Your Program Using

    {

    zH

    RROR MESSAGE

    1

    \

    F I LE NAME

    \

    ALTER

    t

    Miscellaneous BASIC

    Editor Commands

    BYE

    SCRATCH

    CTRL/C

    EXECUTE

    REVIEW

    Your Program Using

    Your Program Using

    MODIFY

    Your Program Using

    ~ ~~

    End of BASIC Session

    1. *Keyboard Monitor Command

    2. Shaded areas indicateuser

    action at the console

    Figure 6-1 BASIC Editor Commands and Related Uses

    6

    -3

  • 8/11/2019 Os8 Basic Reference

    4/58

    BASIC

    An

    alternate metho d is to type NEW w ithout a file name, followed by the R ETUR N key. BASIC displays

    F I L E

    NAME I

    in response to w hich the file name and extension is typ ed .

    For exam ple, to clear the workspace and name the new program TEST.BA, type

    NEW TEST

    or

    NE

    TEST+RA

    6.5.2.2

    OLD Command

    The OLD com mand clears the mem ory w orkspace, and causes the editor to find a

    program on a diskette and place it into th e workspace.

    Command Parameters

    OLd dev:file

    [

    ex]

    dev: file ex is the device, file nam e, and extens ion of the program on the disk. If the extension

    is om itted , BASIC assumes .BA.

    Another m ethod is to ty pe O LD without a file name. BASIC displays

    F I L E

    NAME

    --

    in response to w hich the dev ice, file name, and extension is typ ed . When no device is specified, the BASIC editor

    defaults to

    DSK

    (usually

    = SYS).

    For example,

    to

    bring TEST.BA in to the workspace from RXAl, type

    OLKI

    R X A i :TEST

    B A

    or

    6.5.2.3

    LIST/LISTNH

    Commands

    The LIST command displays the current program along with

    a

    header line,

    containing the program name, da te, and the revision num ber of BASIC. The d ate

    is

    displayed only if the current

    date has been entered into the system.

    Command Parameters

    LIst

    b l

    If n

    is

    om itted all program statem ents in the workspa ce are displayed. When n

    is

    specified, line n and all subsequen t

    lines are displayed. Ty pe CTRL/O t o terminate a listing.

    6

  • 8/11/2019 Os8 Basic Reference

    5/58

    BASIC

    For example, typing LIST (or LI) displays the program PROG.BA:

    LIST

    PROG B A 5 A

    26- JUL-77

    LO FOR A = I TO 5

    20

    FRXNT

    A

    30

    NEXT

    A

    40

    END

    REASIY

    Typing LI

    30

    displays line

    30

    and all subsequent lines:

    1-1

    30

    P R O G B A 5 A 26.- U L - 7 7

    30

    NEXT A

    4Q

    END

    R E A D Y

    The LISTNH comm and also displays the program statem ents in the workspace but withou t the header.

    Command Parameters

    LISTNH bl

    n

    has the same effects as specified for the LIST co mm and, but does not display the header

    6.5.2.4

    SAVE

    Command

    The SAVE command writes the program in the workspace on to the diskette as a

    permanently saved file. Do no t confuse this comman d with the Monitor SAVE command.

    Comm and Parame ers

    SAve [dev:file.ex]

    dev

    is the device on which you want t o store your program.

    file .ex

    is

    the file name and ex tension that the program will have on the diskette. If bot h are left

    ou t , BASIC will use the cu rrent file name and extension o f the p rogram in the wo rkspace.

    If only the exten sion is omitted

    BASIC

    assigns

    .BA. If DEV:

    is omitted ,

    BASIC

    assumes the

    device is

    DSK.

    In

    the following example, the program TEST.BA is in the workspace. To store it

    on

    RXAl under th e same file

    name and extension, type

    or

    6-5

  • 8/11/2019 Os8 Basic Reference

    6/58

    BASIC

    6.5.2.5

    RUN lRUNN H Commands The RUN command executes the program in the workspace. N ote that

    this command differs from the monitor RUN co mm and.

    Command Parameters

    Run (none)

    Prior to program ex ec utio n, this comm and d isplays a heading consisting of the file name and exten sion, BASIC

    version numb er and system dat e, assuming tha t the curren t date has been en tered in the system. When program

    execution is com pleted, BASIC displays

    READY

    Th e following example shows the program PROG .BA in the workspace displaying the result of a calculation :

    R U N

    FROG Bc)

    5 A 26-JAN-76

    3.179

    The RUNNH command executes the program in the w orkspace, but does not display the header line.

    Command Parameters

    RUNNH (none)

    Thus, the only difference between the RUNNH and RUN commands is that RUN prints the header and RUNNH

    does not display the header.

    6.5.2.6

    NAME Comm and

    The NAME co mm and allows the user to renam e the program in the w orkspace.

    Command Parameters

    NAme newfil[ .ex]

    newfil.ex

    is the new file name and ex tension of the program in the workspace. If the extension

    is om itte d, the editor assigns .BA.

    To change the nam e of the program in the workspace to PROG.BA, type

    N A M E

    F'ROG,BA

    or

    NA PROG

    6.5.2.7

    SCRATCH Comm and The SCRATCH command erases all statements from the workspace, tha t is,

    it clears the workspace.

    Command Parameters

    Scratch (none)

    6.5.2.8 BYE

    Command

    The BYE command ex its from BASIC and returns con trol to the M onitor from

    BASIC.

    6 6

  • 8/11/2019 Os8 Basic Reference

    7/58

    BASIC

    Command Parameters

    BYE (none)

    Typing BYE before SA VEing a newly created prog ram will delete the p rogram . Whenever the BASIC editor is

    waiting for user inp ut, typing CTR L/C performs the same function as BYE .

    6.5.3

    BASIC

    Control Keys

    This section describes the c ontro l keys that are used to correct errors, eliminate and sub stitute program links, and

    con trol program listings.

    6.5.3.1

    Correcting Typing and Form at Errors (DELE TE, CTRL/U)

    Errors made while typin g programs at the

    terminal are easily corrected. Pressing the DE LETE key causes deletion of the last character typ ed . One charac ter is

    deleted each time th e key is pressed.

    Som etimes it is easier to delete a line being typed and retyp e the line rather tha n attem pt a correction using a series

    of DELETEs. Typing CTRL/U will delete the entire line currently being worked on and echoe DELETED and a

    carriage return-line feed. Use of the CTR L/U key is equivalent to typ ing DELE TES back t o the beg inning of the line.

    6.5.3.2

    Eliminating and Substituting Program Lines (RETURN )

    To delete a program line that h as already been

    entered into the com puter, simply type the line number and then press the RETU RN key . Both the line numb er

    and the statement(s) are removed from the program.

    Change individual lines by simply retyping them in again. Whenever a line is entered , it replaces any existing line

    having the sam e line numb er. New lines may be inserted anyw here in th e program by giving them uniqu e line

    numbers.

    6.5.3.3 Interrupting Program Execution Program execution may be terminated by typing CTRL/C. BASIC

    responds by displaying the READY message allowing you to correct or add statements to the program.

    NOTE

    BASIC responds to C TRL/C with a REA DY message

    on ly if you have already given the RUN com man d.

    Typin g CTRL /C when the BASIC Editor is operational

    causes a return t o the Monitor.

    6.5.3.4 Controlling Program Listings at the Terminal Console (CTRL/S, CTRL/C an CTRL/O) For programs

    exceeding a single display fram e 24 lines) the user may w ish to s top the scrolling effect that occurs after typing the

    LISTlLISTNH com man d. Three sets of control keys are provided to do this. They are as follows:

    1,

    CTR L/S keys. Simu ltaneously pressing these keys suspends listing (scrolling) of the progra m. However

    it leaves the program in a state where yo u m ay resume listing.

    2. CTR L/Q key s. Simu ltaneous ly pressing these keys (after having suspended scrolling with C TRL /S),

    resumes the listing process.

    3.

    CTR L/O keys. Simu ltaneously pressing these keys ab orts the listing process and causes the BASIC editor

    to display READY.

    6.5.4 Resequencing Programs (RESEQ )

    If a program is extensively mod ified, you may find that some portions of th e program have line num bers spaced

    so

    closely together that the y d o no t permit any further addition of stateme nts. Renum bering the lines in the program

    to provide

    a

    practical increment between line numbers can be done by using the RESEQ program. Note tha t the

    RESEQ program mo difies the line numb ers in GOSUB and IF-THEN statemen ts to agree with the new line num bers

    assigned to program statements by RESEQ. Line lengths must no t exceed

    80

    characters and programs may n ot

    exceed

    350

    lines.

    6 -7

  • 8/11/2019 Os8 Basic Reference

    8/58

    BASIC

    Typically, the program wo uld be used as follows:

    SAVE DSK:SAMPLE BA

    READY

    OLD D SK:RESEQ

    READY

    RUNNH

    FILE :DSK:SAMPLE BA

    START,STEP:

    100,lO

    READY

    OLD

    DSK:SAMPLE.BA

    READY

    LISTNH

    User saves program SAMPLE which requires renumb ering.

    BASIC is ready for next comm and.

    User calls for program RES EQ.

    BASIC is ready for next com man d.

    User runs RESEQ program.

    Program asks for filename. User responds with dev ice, nam e, and exten sion of

    program t o be renumbered.

    Program asks for a starting line numb er (START) and for the increm ent

    between line numbers (STEP). User requests that SAM PLE start with line

    number

    100

    and each line be incremented by

    10.

    Renum bering is accomplished. BASIC ready for next co mm and .

    User calls back his program .

    BASIC ready for next com man d.

    User gets listing of program SAMPLE for further mo dification.

    6.6

    DATA FOR MATS ACCEPTABLE TO

    BASIC

    6.6.1

    Numeric Information

    6.6.2

    Numbers

    Num bers are expressed in decimal or E (exponential) for m at. Exam ples of numbers in both categories are as follows:

    Decim al E TYpe

    0

    10.23E27

    7 6.21Et27

    t 6 9

    -

    7.232E6

    -

    52 2.2

    1

    1

    E-

    3

    -

    3.9265 -2.2 1 14E-4

    0.123

    - 0 . 7 6 9

    In the decimal format, the decimal point is optional for integers. That is, BASIC assumes a decimal point after the

    rightmost digit of an integer. In E type form at, BASIC assumes a positive expoh ent when no plus ( t ) or minus (-)

    sign follows the E. Substitute th e words times ten t o the power o f for the letter E w hen reading

    E

    type format

    numbers.

    6 -8

  • 8/11/2019 Os8 Basic Reference

    9/58

    BASIC

    Num eric da ta may be inp ut in either format. Results of comp utations with an absolute value outside the range

    +.OOOOOl

  • 8/11/2019 Os8 Basic Reference

    10/58

    BASIC

    Examples:

    10 LET 1=53721

    20 LET B3=456.9

    30

    LET X=20E9

    40

    INPUT

    Q

    50

    DATA 5,6,7

    6.6.4 Subscripted Variables

    In addition

    to

    simple variables, BASIC accep ts an oth er class of variables called su bsc ripte d variables. Sub scripted

    variables provide ad ditional co mp uting capabilities for handling array s, lists, tables, matrices, or an y set of re lated

    variables. Variables are allowed o ne or tw o sub scripts. A single letter or a letter followed b y a digit form s the name

    of the variable. Th e subscript is forme d by o ne or tw o integers enclosed in parentheses and separated by com ma s.

    Up

    to 31 arrays are possible in an y program, subject only t o the amo unt of m emory available for d ata storage. For

    examp le, an array might be described as A(J) w here

    J

    goes from

    1

    to 5 , as follows:

    This allows reference t o be m ade t o each

    of

    the five elements

    in

    the array A.

    A

    two-dimensional array A(J,K) can be

    defined in a similar man ner, bu t th e sub scripted variable

    A

    must always have the same number of subsc ripts (that is,

    A(J) an d A(3 ,K) can not be used in th e same program). It is possible, howev er, to use the same variable nam e

    as

    b o t h

    a subsc ripted and an unsubscripted variable. Both A and A(J) are valid variable names and can be used in the same

    program. F )r more info rmation on arrays and the use of sub scripts, see the description of the DIM statem ent

    (Section

    6.7.10.1).

    6.6.5 Arithmetic Operations

    6.6.5.1

    Operators BASIC performs add ition, sub tractio n, mu ltiplication, division and exp on entia tion, as well as

    mo re complicated op eratio ns (explained in detail later

    in

    th e manual). The five ope rato rs used in writing mos t

    formulas are :

    Symbol

    Op erator Meaning Example

    t Addit ion A+B

    Sub tractio n A- B

    Multiplication A*B

    -

    *

    I

    Divis ion A D

    -(or**) Ex pon entia tion A^B o r (A**B)

    (Raise A t o th e B Power)

    6 .6 .5 .2 Priority

    In any given mathematical formu la, BASIC performs th e arithm etic operations in th e follow-

    ing order:

    1 . Parentheses receive to p prio rity. Any expression within parentheses is evaluated before an unparen-

    thesized expression.

    2. In the absence of parentheses, the order or priority is:

    a. Exponentiat ion

    b. Multiplication and Division (of equa l priority)

    c. Addition and Subtraction (of equal priority)

    3 .

    If either sequence 1 or

    2

    above does not clearly designate the order of priority, then the evaluation o f

    expressions proceeds from left

    to

    right.

    6-1

    0

  • 8/11/2019 Os8 Basic Reference

    11/58

    BASIC

    The expression A+B-C is evaluated from le ft t o right as follows:

    1

    A+B = step

    1

    2 . (result of step 1)-C = answer

    The expression A/B*C is also evaluated from left to right since multiplication and division are of equal priority:

    1

    A/B

    =

    step

    1

    2 . (result of step l)*C

    =

    answer

    6.6.5.3

    Parentheses Parentheses may be used to change the orde r

    or

    priority because expressions within paren -

    thesis are always evaluated first. Th us, by enclosing expressions appro priate ly, the order

    of

    evaluation can be

    controlled. Parentheses may be ne sted, that is, enclosed by o ne

    or

    mo re sets of parentheses. In this case, the ex-

    pression within the innermo st parentheses is evaluated first, and then t he n ext in nerm ost, and so

    on,

    un til all have

    been evaluate d. Consider th e following examp le:

    The o rder of p riority is:

    1 BA2 =

    step

    1

    2 . (result of step 1)+4 = step 2

    3 . (result of step 2)/X

    =

    step 3

    4 . (result of step 3)*7 = answer

    Parentheses also prevent an y confusion or d ou bt as to how th e expression is evaluated .

    For

    example,

    A*BA2/7tB/C+DA2

    ((A*BA2>/7)+((B/C)+D12)

    Both of these formulas will be executed in the same way. However, the second example may b e easier t o und er-

    sta nd . Spaces may also be used to increase readab ility. Since th e BASIC compiler ignores spaces, th e tw o

    statements:

    10 LET B=DA2+1

    10LETB=DT+l

    are identical, bu t spaces in th e first statem ent provide ease in reading.

    6.6.5.4 Rules

    for

    Exp one ntiatio n Th e following rules apply in evaluating the expression AAB.

    1 If B=O, then A-B=l 3^0=1

    3 .

    4 .

    If

    B is an integer

    X,

    hen A^B=A, *A2 *A 3. . .*A,, where n=B

    5 . If

    B is an integer

    O

    then A B=l/A1 *A2*A ,.

    .

    .*A,), where n=B

    6 .

    If B is a decimal (non integ er) and A>o hen A^B=EXP(B*LOG(A))

    7 .

    If

    B is a positive or negative decimal (nonin tege r) and A X , he

    2 . If A=O and B X , hen AAB=O 0 -2=0

    If A=O and B

  • 8/11/2019 Os8 Basic Reference

    12/58

    BASIC

    6.6.5.5 Relational Op erato rs A program may require tha t two values be compared at some poin t to discover

    their relation to on e anoth er.

    To

    accomplish th is, BASIC makes use of the following relational opera tors:

    equal

    =

    >