Intel 9800758-02 manual Writing to a Random 1/0 File, Integer, Single-precision value

Page 39

BASIC-SO

Disk File Input/Output

It's not quite so simple to print or do calculations with numeric values from random I/O files, however, because they are represented as strings. You must convert each string that represents a number to its corresponding numeric value, either integer, single-precision floating-point, or double-precision floating point. The CVI, CVS, and CVD functions, respectively, perform this conversion.

Assume that there is an additional 2-character field following the 9-character social security number that represents the number of hours worked in the previous week. The following statements open the file, define the fields, read the first (or next) record, and increment the variable HW by the number of hours worked:

10 OPEN "R", #3, ":F1 :PERSON"

20FIELD #3, 20 AS N$, 9 AS SS$, 2 AS H$

30GET #3

40PRINT N$; SS$; "HOURS ="; CVI (H$)

50HW = HW + CVI (H$)

60PRINT "TOTAL HOURS ="; HW

The two characters defined as H$ are converted to an integer. To convert a string to a numeric variable, the number of string characters must equal the number of bytes required to store the corresponding numeric data type:

Integer

CVI

2 bytes

Single-precision value

CVS

4 bytes

Double-precision value

CVD

8 bytes

If the number of string characters represented by the argument of the function is fewer than required, an ILLEGAL FUNCTION CALL error message is printed and execution halts. If the string variable is longer than required, the extra characters are ignored.

Writing to a Random 1/0 File

To store data in a random disk file, you follow the same sequence of OPEN and FIELD statements as when reading from a random disk file. The PUT statement writes the 128 bytes of the I/O buffer to the disk file.

The FIELD statement, however, defines fixed-length fields in the I/O buffer. To get the data to be written to disk into the I/O buffer, there are two special assignment statements: LSET and RSET. LSET left-aligns a variable in the buffer field, and RSET right-aligns it. In both cases, if the variable is shorter than the field, it is pad- ded with ASCII blanks (20H); if the variable is longer than the field, it is truncated.

For example, assume you have opened a file for random I/O, assigned it file number 3, and defined the first 20 characters as N$. You must use LSET or RSET to place values in the buffer:

10OPEN "R", #3, ":F1 :PERSON"

20FIELD #3, 20 AS N$

30LSET N$ = A$

40PUT #3,1

This technique may be used for storing variables for use in another program, linked to the first program with the RUN statement (see description of RUN, chapter 6).

Whatever the length of A$, its leftmost 20 characters are now identified as N$. If A$ is less than 20 characters long, the remaining characters of N are set to ASCII blanks (20H). Statement 40 writes all 128 bytes of the I/O buffer to disk file number #3.

5-7

Image 39
Contents BASIC-SO Reference Manual A108/0979/7500 FL Preface Page Contents Illustrations Invoking BASIC-80 Chapter Introduction to BASIC-80Filename ExamplesIntroduction to BASIC-80 Basic filename MEMTOPaddressDeleting a File Listing the Directory of a DiskRenaming a File Changing File AttributesSave LP ,A You can now run, list, or edit the programStatements CommandsBASIC-80 Statements BASIC-80 Commands ContdBASIC-80 Functions BASIC-80 Statements ContdBASIC-80 Functions Contd Functions Representing DataHexadecimal Integer Constants ConstantsInteger Constants Decimal Integer ConstantsOctal Integer Constants Single-Precision Floating-Point ConstantsDouble-Precision Floating-Point Constants $ = Enter next data string String ConstantsString Variables This is a string constantYI,1 L4! = Csng l4String Arrays BASIC-SO Operators in Order of Precedence Contd Arithmetic OperatorsString Expressions Logical OperatorsString Operator Numeric ExpressionsEntering and Editing Programs 30 A--=8xx*522537 Control-R a = 8*37 30 A=8*5230 A=8 30 A=8*52 30 A=8*37If AB then SubcommandCommand 3D press 3, then D results Integer DIf AB then 120 Else Null SET At this point, the other editing subcommands may be used Syntax of the X subcommand isPrint Undefined SET. Enter a L Print Undefined SET The E subcommand is enteredInteger C character character Move the cursor to PRINT. Enter 2C RE LBASIC-80 Error Messages Syntax Error MessagesOverflow, Underflow, and Divide-by-Zero Error Trapping Trace Facility If line 40 is replaced with Error SimulationError Handling Restarting Program ExecutionOpen 1,#1,F1DATES Opening a Sequential FileIf executed four times, it would read all eight values Refer to for further details of Print UsingWriting to a Sequential File Reading from a Sequential FileValue of R$ would be Closing a Sequential FileBuffers Defining a Random 1/0 Field-FIELDField #3, 20 AS N$ Field #3, 20 AS N$, 9 AS SS$ Disk File Input/OutputTo read the next record Opening and Closing a Random Disk FileReading from a Random 1/0 File Any of the parameters can be variablesDouble-precision value Writing to a Random 1/0 FileInteger Single-precision valueMKI$ Clear expression,address Attrib Fdrive numberfilename, W1Attrib Fdrive numberfilename, WO Auto first lineJ, incrementClose Commands and StatementsRules for function name are the same as for variable name DEF FNXDefsng Defdbl Defstr Defint Delete DIMEND ErrorDIR EditFOR-NEXT-STEP Error expressionExit FieldGET I file number ,record number GETGosub For variable=expression to expression Step expressionIF-THEN-ELSE InputGoto List KillLET Line InputLoad LSET, RsetNEW Merge9 16 NextOn ... Gosub On ... GotoOpen Option Base OUTPoke Print Using PrintString Fields If X$=SEVEN and Y$=EIGHT, the results of line 40 would be Numeric fieldsPUT PrunRead RandomizeREM Return Resume60 END SaveRUN line numberlstring expression ,F Line number RUN filename? 5,8,2 TRON, TroffWidth WaitATN ABSCHR$ CintFunctions AO/o = CsngCVI CVS 10 A# = 1.00/3.00EOF DskfOk· FIXHexs INPInputs INT InstrLEFT$ LOG LENLOC LOFMID$ MKI$ MKS$ MKD$OCT$ RIGHT$ string,integer RightsPeek expression POS integer10 a =1 SGNSIN SpacesSQRexpression SPCSPC integer If A$ = 2 then Print Correct Else GotoSTR$ expression TAB expressionTAN expression 60 AO/o = Here is an example of how the USRn statement is usedTable A-I. BASIC-80 Error Codes Appendix a BASIC-SO Error CodesTable A-I. BASIC-SO Error Codes Contd Appendix B BASIC-SO Reserved Words Page To resume program execution after it is stopped by To halt program execution and return to commandLevel To tab across the linePage Appendix D Ascii Codes Table D-l. Ascii Code ListBEL Table D-2. Ascii Code DefinitionAppendixe Calling Subroutines Figure B-1. Internal Representation of Numbers and Strings RESULT=USR%1VARPTRA, VARPTRB, VARPTRC» PRINTA+B+C= Result Some Real ExamplesFigure E-2 /8085 Assembly Language Program Figure E-3. PL/M-80 Program Appendix F RMX/SO BASIC-SO ISIS-II BASIC-SO Initializing the Predefined RMX/SO BASIC-SO ConfigurationOOOOH-OFFFH Table F-l. Sample Configuration Jumper WiringBASIC-80 Executable Files Generating Boot-Loaded and PROM-Based VersionsBASIC-80 Source Files BASIC-80 Object FilesGenerating a Boot-Loaded RMX/80 BASIC-80 This option enables your user-written I/O drivers if you That are not boot loadedThis option is used to allocate memory. It is 1 if the boot Setting baud rates, refer to the RMX/SO Users GuideISBC 80/20-4 Generating a PROM-Based RMX/80 BASIC-80 Prom F1 RMX820.L1BSTART, & FOBASCM.OBJ,& FORMXBAS.LlB F1 DFSDIR. L1BDIRECTORY ,RENAM E,& F1 MTI810.L1B ISBC SO/10 System Clock Configuring DFS on an iSBC 80/10Adding BASIC-SO to an Existing RMX/SO Configuration Configuration RequirementsPublic Variables Open 0,#1, L1LlST Adding User-Written I/O Drivers to RMX/SO BASIC-SOFigure F-S. Sample User-Written 1/0 Driver Routine Burning a BASIC-80 Program Into Prom Altering BASIC-80 WorkspaceBaprom F1HEATER.BAS Page Index Ase BASIC-80 RIGHT$,7-1O Request for Readerscomments 111111