HP 9845 BASIC

Here now is a special section on the special BASIC used in the HP 9845 series.

BASIC is an acronym for "Beginner’s All-purpose Symbolic Instruction Code". Developed 1964 by John George Kemeny and Thomas Eugene Kurtz at Dartmouth College in New Hampshire, BASIC had originally been one of the earlier programming languages. So actually, BASIC is almost exactly as old as I am.

BASIC

BASIC had been developed with non-experts in mind, and showed an astonishing durability and flexibility throughout the years. Once starting with 15 statements, 13 operators and 10 functions, BASIC initially had been definitely one of the less complex programming languages. Some of the design principles, like independence of underlaying hardware, easy beginning and prototyping with small programs, and low requirements concerning system resources made BASIC the first choice for the upcoming home computer market. Drawbacks, like not supporting structured programming, showed not to be that important for systems with 8 kBytes of user memory.

To give an impression, the hello-world program in BASIC is as easy as

10 PRINT "Hello world!"

(Actually, the standard also requires a terminating END statement, but most implementations weren't as strict as returning a program error on this triviality, however 9845 BASIC does...).

There had been two major attempts for standardization of BASIC by ANSI, the American National Institute for Standardization. The first approach defined the so-called "ANSI Standard for Minimal BASIC" in 1978, which in fact specified only the most essential syntax and evaluation rules. The limited capabilities inevitably caused computer companies to add their own extensions, which again made most of the standardization efforts useless.

Probably it would have been a better idea to standardize several levels of implementation. But if you never participated in standardization committees, you probably will never understand how difficult it is to commit even on the simplest agreements. It was symptomatic that Dartmouth BASIC already had been in the sixth version at standardization time, and included many of advanced features which exceeded the ANSI standard by far. It took another nine (!) years to release the next ANSI "full" BASIC standard.

BASIC and Time-Sharing

The development of BASIC had been motivated with the change of computer usage during the 60's. From the very beginning, computers mostly were operated in so-called 'batch' mode. This simply referred to the fact, that programs were developed on paper, transferred to tape or cards and collected in 'batches', which were then fed into the 'number crunchers' one after the other. Results of processing then were output on printers, so that programmers could pick their printouts several hours after the batch was brought in. Obviously any program error resulted in wasting lots of time. Trial and error wasn't the best choice at the time.

Now a new type of computer came up: the time-sharing system. Instead of processing one complete job after the other, users could use teletype terminals to directly connect to the core system, and inputs, outputs and processing in between was done on a time slice base in the order of milliseconds, and interactively. So there was the illusion of concurrent processing and immediate response, which was revolutionary for users who used to work with the batch mode systems before. And time-sharing opened the computers to non-experts, who simply wanted to create small programs for simple tasks or to get a feeling on how computers work. This kind of programming also got the label "conversational" in contrast to batch programming (the term "interactive" wasn't yet set).

For those users, FORTRAN and ALGOL (which, although quite popular, were orginally developed with batch mode systems in mind) showed to be too complex to start with. This was exactly the point, where the project at Dartmouth College started in 1964 with the design of a simple programming language, easy to understand, with as few statements and programming rules to keep in mind as possible, and designed for interactive use on time-sharing systems, and it proved to become extremely successful.

Looking at the home computer scene of the time, BASIC had been almost ubiquitous. Starting in 1975 with the MITS Altair 8800, BASIC was into the market. As was a small company called "Micro-Soft", with Bill Gates and Paul Allen as founders, wo started with the development of Altair BASIC one of the most exciting success stories of the computer age. Next, with BASIC for CP/M a capable software architecture was available for the low end business market. And most popular home computers of the time offered a version of Microsoft's BASIC, including the Commodore PET, the Apple II and the TRS-80.

Today BASIC still is alive, especially with Micosoft's Visual Basic and Visual Basic für Applications. Now these modern dialects effectively do not have too much in common with the original Dartmouth dialect, but, essentially, BASIC has proved to be a valid choice for many application areas where easy programming is needed and there is not too much system specific stuff to handle. And this probably also had been the main reason for chosing BASIC as alternative to HPL, HP's first own programming language. It definitely matched the concept of providing computing power primarily for engineers and scientists, and not for computer experts.

Rocky Mountain BASIC

The history of HP's BASIC reaches back to the late sixties/early seventies, when HP delevoped its first BASIC implementation for their first computers, the 2100 series. This was just four years after BASIC was invented at Dartmouth. HP offered their BASIC as loadable system, requiring at least 8k of core memory (6k for the BASIC, 2k for user programs). It was available as Conversational BASIC for single user (with a 2116 plus teletypewriter) or a time-sharing system (also 2116 based with terminals for each user). This first HP BASIC already implemented many of HP's extensions and concepts, which later characterized Rocky Mountain Basic.

Not much later, in 1972, the 9830A was the first HP desktop BASIC computer. In contrast to the 2116, the 9830 BASIC was fully available in ROM. Full source code of that BASIC by the way is included in the U.S. patent 4012725. Whereas the later 9825 was programmed in HPL, the 9835, 9845 and 9020 again were designed with BASIC as the main programming language, and again with the whole BASIC in ROM.

Although the 9845 BASIC covered the functionality of most of the BASIC implementations of the time, HP starting with the 9830A developed the somewhat odd strategy not to build everything needed into the BASIC, but to provide expansion modules, so-called option ROMs - at extra charge. Later HP changed their strategy and included all the BASIC extensions into a new BASIC package called WS BASIC (WS stood for workstation, since WS BASIC was intruduced for the HP 9000 Series 200 workstations). Another name for this BASIC dialect was Rocky Mountain BASIC, with reference to the HP labs in Colorado, where this BASIC version was created.

Rocky Mountain BASIC was chosen by HP as the standard programming language for controlling all the measuring equipment via the HP-IB bus. Many programs for instrument control and data processing were created in this language, and native support for the HP-IB was completely built into this language (old HP 9845 BASIC needed the I/O option ROM for this). With the appearance of UNIX and of the IBM compatible PC, HP tried to port Rocky Mountain BASIC to both platforms. The success story of Rocky Mountain BASIC ended at the time when MS Windows entered the stage. Another company, TransEra, developed a clone which ran under Windows and did support the most common IEEE488/GPIB expansion cards for PC. HP decided not to continue their own development of Rocky Mountain BASIC, but rather to re-brand TransEra's product as "HP BASIC for Windows".

BASIC for the 9845 Series

The BASIC dialect used on the HP 9845 series had been referred to first as "System 45 BASIC" and later as "HP Compatible BASIC Level I". "HP Compatible BASIC" stood for a certain set of statements and functions, and "Level I" should indicate that the language set was meant for the highest performance systems. Obviously HP thought about software reuse and wanted to assert inhouse compatibility at least among certain system families. Actually, there never had been a "Level II" system, so this addendum was abandoned soon.

Now what's special with the 9845, compared to other BASIC dialects of the time? What appears first to the user, is that the user interface is obviously different. Whereas most other computers did provide full screen editors, where you could randomly move around the screen with the cursor keys, and which always used the current line for input, HP chose a completely different approach. The screen was divided into several areas, output was restricted to the 20-line printout area and one special prompt line, and input to the two-line keyboard entry area. The bottom line was reserved for error messages and system status.

Screen Partitioning

Screen Partitioning

Since System 45 BASIC was the successor of 9830 BASIC, I guess this screen partitioning mostly goes back to the 9830 implementation of BASIC, which provided only one single line of display and input in terms of a fixed alphanumeric LED display line, and used a top-mounted line printer to do the PRINT output. So what the 9845 system designers essentially did, was to add a scrollable printout area which works similar like the original 9830 line printer. And in fact, all PRINT statements in BASIC simply print to this area, leaving the other lines untouched.

9830A With Printer

9830A BASIC Calculator With Printer (from MoHPC)

Although in general unfamiliar, this type of screen separation also has its advantages. So you - for example - can always provide information which is not scrolled on the display line, and also offer means to enter commands while a program is running (yes, and even currently executing program lines can be altered during execution, which is in fact one of the most remarkable differences compared to other BASICs).

Anyway, for efficient program development you need to use the full screen in order to get an overview of the program section you are currently in. This is what the EDIT LINE mode provides. But again, instead of simply providing a full screen editor, HP decided to use a windowing mode, which shows the program lines before and after the line which is currently edited. So the line to edit is always in the center of the screen, and the window moves up and down depending on which line shall be edited next. And again, this looks much like the 9830 BASIC, with the extended possibility to view the context of the currently edited line.

It is interesting that HP kept this principle in all of its BASIC versions, and even TransEra's HT-Basic, which is some kind of devisee of HP BASIC, still uses this type of screen interaction.

But there are more important differences between 9845 BASIC and other BASIC versions. First of all, 9845 BASIC is considerably comprehensive. There are more extensions to the "standard" than the standard itself provides. 9845 BASIC included 71 statements, 18 operators and 44 functions, which is more than four times the amount compared to the original Dartmouth BASIC. Main areas of extension were string, matrix, file and event processing. It seemed that HP did not care too much about firmware space, and did not get into compromising trade-offs in order to match the requirements of the main target groups of engineers and scientists. Instead, HP adopted many of the later Dartmouth concepts, long before the ANSI Standard for "full" BASIC had been released.

The overall design had been focused towards extensibility. So the already comprehensive built-in BASIC could be easily expanded with additional option ROMs, e.g. for graphics, I/O or mass storage operation. Altogether, 9845 BASIC definitely had been the most powerful BASIC of the time. And even loadable software modules, the so-called binary programs, were part of the concept, which could be used to extend the language even further.

One of the most useful option ROMs, the Structured Programming ROM, provides Pascal-like complete statements for controlling program flow, like IF..THEN..ELSE..END IF, SELECT..CASE..CASE ELSE..END SELECT, WHILE..END WHILE, REPEAT..UNTIL, LOOP..EXIT IF..END LOOP and auto-indent. Unfortunately, programs using these features are no longer usable without change by systems not equipped with the Structured Programming ROM.

A very special extension is the Assembly Development and Execution ROM, which adds support for developing hybrid programs incorporating assembler routines in BASIC programs, using the same interactive editing and debugging environment. Special BASIC statements for loading, storing, assembling and calling assembler routines are added to the standard language set.

9845 BASIC differed slightly between the 9845A and 9845B/C versions. The 9845B could handle more line numbers (32,767 compared to 9999 on the 9845A), and provided screen addressing for the PRINT statement. Also, 9845B BASIC added some few statements (CAT TO, KBD ON, KBD OFF, KBD$).

With the exception of the above differences, BASIC programs of type DATA which are stored as ASCII text files with the SAVE statement can be loaded with GET or LINK on a 9845B/C. The same applies to the other way around. BASIC programs of type PROG which are stored as compact code files with the STORE statement on a 9845A are not loadable on a 9845B/C and vice versa.

Features

There are many features which enhance 9845 BASIC compared to other BASIC implementations of the time. It can be assumed that HP participated in the discussions on standardization of BASIC, however the 9845 systems were much more capable than any of the common home computers of the time. The standard BASIC firmware already spanned 64 kBytes of LPU ROM, not counting all the system and I/O routines (which were part of the PPU ROM). Just for comparison, the Commodore PET firmware had to be squeezed into 18 kBytes, and the Apple II even could use only 12 kBytes, all including the complete interpreter and operating system (!).

Now for the 9845 systems, the need for supporting complex engineering and scientific tasks simply required sophisticated extensions. Another point is that - although the 9845 was targeted towards non-programmers - HP already had lots of experience in software engineering, which again had some influence in providing tools for better programming like labels, long identifiers and indents. Unfortunately the Structured Programming ROM extensions came too late to get part of the standard firmware.

Here are the features which had been already part of the firmware, as shipped with the basic models. More features could be added on demand with the use of option ROMs (actually, the Mass Storage option ROM and the I/O option ROM both were almost always present).

Long identifiers Wheras standard BASIC allows only one single character and one digit for identifiers, 9845 BASIC uses up to 15 letters, digits or underscore, which are all significant. This makes 9845 BASIC programs much better readable.
Typed variables Variables may be declared with type, which gives more flexibility in using less space for certain types like INTEGER and SHORT.
Labels Standard BASIC only allows line numbers as jump targets, 9845 BASIC also support the use of line labels, which again follow the same rules as all identifiers with up to 15 characters. Since line labels can be used everywhere where a line number is used, 9845 BASIC could
Subprograms Standard BASIC only supplies GOSUB and sometimes DEF FN for calling subroutines. One of the main disadvantages is that both statements do not allow separated scopes for variables. So normally all variables in a BASIC program are global, which again makes it harder to handle the available name space, especially with identifiers made of two characters maximum. 9845 BASIC, on the other side, adds real FORTRAN-like subprograms, which have their own execution context, including variables with local scope. Parameter passing can be done either by reference or by value. Global variables can be defined, though, with the COM statement. Subprograms were primarily targeted for setting up tested and debugged re-usable libraries.
Debugging 9845 BASIC provides an extensive list of debugging commands for stepping and tracing the code interactively. Outstanding at the time, no other BASIC provided such perfect debugging aids.
In-line documentation Standard BASIC only allows full comment lines, starting with the REM statement. 9845 BASIC introduces in-line comments which can be appended after each statement, a feature which makes documentation much more effective.
Indent Program listings can be made much better readable with proper indents, e.g. by using indents to visualize the nesting structure of the program. Indents are stored during line input, so they last for later printouts.
Formatted output 9845 BASIC provides complex formatting syntax with PRINT USING statements, which even include details like floating currency, radix character and end-of-line character control. In addition, format codes can be stored and re-used with the IMAGE statement.
Auto numbering and renumbering Whereas most other BASIC dialects had to use special utilities for generating new line numbers, these features are already built into 9845 BASIC.
Arrays 9845 not only allows arrays with multiple dimensions and arbitrary intervals (like indices from -100 up to 100, which makes 201 elements in total), they also can be re-dimensioned in terms of restricting the original number of elements at any time. And 9845 BASIC provides numerous matrix operations, which operate full speed on the whole array. Arrays also can be passed to subroutines both by reference and by value. The standard base of array indices can be set to either 0 or 1 by user preference.
Concurrent processing The 9845 systems all are equipped with two CPUs, one for language processing and another for I/O and other system tasks. 9845 BASIC provides means to enable parallel processing of "logic" and I/O processing, so that e.g. calculating maths and formatting output can be performed simultaneously, thus leveraging the advantages of two processors. Also, during program execution, the 9845 systems still accepts user commands, e.g. for doing some calculations or even for editing the running program code.
Event processing Normally, BASIC execution follows path strictly defined by jump, loop control and decision commands such as GOTO, FOR..NEXT and IF..THEN. Asynchroneous events have to be polled in continuous loops. 9845 BASIC also supports event handlers which - once registered - can be triggered asynchronously, e.g. via error or keyboard events, which makes event processing more efficient and reactive.
Program linking 9845 BASIC programs, which get too large to fit into the available user memory, can be split into several parts, which still can share global variables.
Program protection Files can be protected against deletion, and program lines can be 'secured' from listing & editing. This latter probably originally was intended for protecting intellectual property, however used only to hide the use of binary programs with system impact towards to user.
Memory dumps 9845 BASIC allows to save nearly the complete system state including user memory and screen content into a large image file, which can be re-loaded in order to continue with exactly the same configuration.
Screen addressing Screen output can be freely positioned, so screen masks can be easily created.
Keyboard macros Each of the 16 function keys (plus shift) can be bound to a nearly unlimited number of keystrokes, so even complex inputs can be assigned and recalled with one single key stroke. The definitions can be stored on mass storage for later use.

Restrictions

But there are also restrictions. BASIC originally had been developed with some protection of the system in mind, thus keeping the user off doing something which might influence system reliability or - especially for time-sharing - has an impact to the system core or other concurrent user sessions. This protection was soon reduced by many BASIC dialects with new statements like PEEK, POKE and USR, which provided access to the system memory and means for executing machine language programs. But not with HP's BASIC. In fact, HP did everything to ensure that the user won't even get a chance to access system specific ressources. So there is no PEEK in HP BASIC, and no POKE, and no USR.

Some BASIC dialects provide the question mark "?" as shortcut for the PRINT statement. This is not possible in 9845 BASIC. Also, most BASIC dialects allow combining multiple statements in one single line, seperated by colons ":". This is also not possible with 9845 BASIC. The maximum line length is 160 characters, but each line can hold exactly one statement (actually, this is good practice today for all programming languages).

Another restriction is that 9845 BASIC doesn't include support for integer arithmetics - all calculations are performed with floating point numbers, which is well-fit for engineering and scientific tasks, but sacrifies the option of maximum performance where integer maths are sufficient.

Finally, the size of any array is limited to maximum 32,767 entries. However program size is only limited to the total amount of free user memory.

Software Protection

With 9845 BASIC, HP introduced a feature for preventing individual lines from editing & listing, which was achieved by the use of the SECURE statement. Although computers had been quite new, software piracy already had been an issue. There were many commercial programs which had been developed for the HP 9845 series with high effort, and standard BASIC did not provide any means to protect them from copying and editing.

Since there are not many third-party software artefacts preserved, it is hard to estimate how many vendors made use of this protection feature. But not long after the HP 9845A had been introduced, first solutions were available to 'unsecure' the secured lines, thus effectively eliminating the software protection. Then other utilities were offered, which promised to prevent any unauthorized copying of program by simply adding a certain binary program to the BASIC code, and so on. See the utilities section in the HP Plus vendor program for an overview what had been 'officially' available.

HP itself offered all own programs without protection, except those parts, which implemented direct system access by utilizing certain binary programs, e.g. for performing sector-by-sector copy of storage media. Once line protection had been hacked, the use of those system utilities was obvious and the utilities themselves could then be used to gain complete control of the system. So far towards protecting BASIC systems (quod erat demonstrandum).

Remark:

HP made lots of efforts to protect the OS from the BASIC programmer for several reasons. The most important objective probably was to achieve a certain level of reliability. This had been an issue already with the very first BASIC implementation on the 2100 series computers during the late 60's. Again the background is quite simple, those systems were used in a multi-user time-sharing environment, and it had to be ensured that one user could not disturb the work of another. And in no case kill the core execution environment. With the 9845 series, this still made sense, even in a single-user environment. The user should be freed from any system specific task and most of all - as a beginner - should not be able to do any harm to the system, whether intentionally or non-intentionally.

It showed that for certain tasks, some low level system access was needed, though. So HP offered this additional functionality in binary programs (which could not be 'read' at the time), and used the keywords associated with those functions within BASIC programs. In order not to open a gate for hackers, those low level keywords were protected with the line securing mechanism. Now in fact there were at least three leaks, which could be used to disable the protection:

  • First, the protection mechanism itself had been documented in the original 9845A patent document. Hard to find, but it was specified that a singe bit was set in the line description record for each secured line. Clearing this bit simply made the line readable again.
  • Next, the Assembly Development & Execution ROM could be used to access both system memory and individual sectors on the disk, which made it comparably easy to 'unsecure' secured lines. As long as the Assembly Development & Execution ROM hadn't yet been available, the most useful binary program was the PHYREC binary, which allowed software analysis and manipulation on mass storage level ("PHYREC is your friend").
  • Finally, as it is almost always the case, probably communication leaks in HP enabled third party programmers to get the knowledge about how protection was done and how protection could be disabled.

For keywords implemented in binary programs it was still possible to prevent keyword listing in a BASIC program by simply refusing output in the provided keyword listing routine, even with secure bit cleared. However there is no implementation where this type of protection was used.

At the point when arbitrary, self-developed binary programs could be designed, loaded and executed on the HP 9845, any means of protection of system access in fact became obsolete.

More Information

Since BASIC is the standard programming language for the 9845 series, it is perfectly documented. See the BASIC Programming Manul as primary source (use hpmuseum for download). Implementation details can be found in the arcticle from the McGraw-Hill publication "Computer Structures: Principles and Examples" (look here for download), and additional details can be found in the 9845A patent (available here for download). Note that this information applies primarily to the 9845A, however the software architecture should be basically the same for the 9845B/C.

The source code for the HP BASIC version used in the HP9830A together with a description is included in the U.S. patent 4012725. Some history facts on the early Conversational BASIC can be found in HP Journal, issue 11/1968.