!lm10
!rm76
Field Input Routine for Applesoft....................Bob Potts

Inputting strings to an Applesoft program is normally a simple task.  What could be easier than "INPUT A$"?  But, that method will not allow commas or colons.  

Another easy way is to use GET C$ for each character, and append them to a string using A$=A$+C$.  But, by the time you add the testing for each input character to find the end of input and other possible control characters, the routine can be terribly slow.  Furthermore, it eats up string space like crazy; eventually Applesoft garbage collection starts, and the program dies for a while.  Here is the kind of loop I am talking about:

!lm15
10 A$=""
20 GET C$
30 <perform various tests on C$>
40 A$=A$+C$:PRINT C$;
50 GO TO 20
!lm10

As the string increases in length, the speed decreases dramatically.  In fact, some characters may be lost if you are a fast typist.

One way to correct this is to use a machine language routine to input each keystroke, test it, and build a string for the Applesoft program.  Such a routine was printed in "Apple Assembly Line" issue #7 (April, 1981), pages 6-8.  But that routine used the monitor's RDLINE subroutine to input the string.  I needed a routine more adapted to inputting a series of fields, using the screen in a "fill-in-the-blanks" mode.
!np
The following program was designed for use in the various branches of the Bank of Louisville.  The Apple is used to calculate loans, print the installment notes, and to enter loan applications.  A loan application involves filling in the blanks on several screens full of prompts.

To use the input routine, you first position the cursor to the start of field using VTAB and HTAB; then set a field length using the SCALE= statement, and a field code using the ROT= statement.  The actual call to the input routine is done with "&INPUT" and the name of your string.  Here is an example for inputting a 5-character field starting in column 10 of line 7:

!lm15
10 VTAB 7 : HTAB 10 : SCALE=5 : ROT = 0 : &INPUT A$
!lm10

The input routine allows skipping from field to field, either forward or backward through a form.  Backspace and copy (right arrow) are supported.  Filling up a field, or hitting RETURN within a field, finish that field and return the value to Applesoft.  An EXIT CODE tells the Applesoft program whether a value was returned in the string or some other exit was chosen.  You access the exit code with a PEEK(224).  Here are the four exit codes and their meanings:
!lm15

EXIT CODE = 0     Field was filled or RETURN typed.
          = 1     ESCAPE was typed at beginning of field.
          = 2     CTRL-F was typed at beginning of field.
          = 3     Left Arrow (backspace) was typed
                  at beginning of field.

!lm10
If the exit code is zero, then the field data you typed is in your string.  Otherwise, the string's value is not changed.  Finishing a field by either filling it up or hitting RETURN puts the field data into your string, and I then advance to the next field on the form.  I use an exit code of 3 (backspace at beginning of field) to mean that the Applesoft program should go back to the previous field on the current form.

How you use the exit codes of 1 and 2 is up to you.  You might use an ESCAPE (exit code = 1) to abort the form-filling and return to a main menu.  The ESCAPE is now only recognized if you are at the beginning of the field and the field code is non-zero.  Of course, you could change that.  You might use the control-F to mean you are finished with the current form.
!np
How Does It Work?

Line 1110 sets the origin to $0300.  If you already have something else in page 3, you can change the origin to whatever suits your fancy.  Just remember to set the correct values for HIMEM and LOMEM to protect it from Applesoft, and vice versa.

Lines 1380-1440 install the ampersand vector.  If you BRUN the program, this code is executed.  If you BLOAD it, then CALL 768 will execute it.  You only have to execute this once in your program.  Once done, any occurrence of an ampersand statement in your program will branch to INPUT.FIELD, at line 1460.

Lines 1460-1500 check for the keyword "INPUT", and a string variable name.  The three routines (and others used in this program) starting with "AS." are in the Applesoft ROMs.  AS.SYNCHR compares the current character with what is in the A-register; if different you get SYNTAX ERROR, and if the same the character pointer is advanced.  AS.PTRGET scans a variable name and finds its descriptor in memory.  AS.CHKSTR makes sure that the variable is a string (if not you get TYPE MISMATCH).  At this point the address of the string descriptor is in $83,84.  The address in $83,84 points to 3 bytes which tell the length and address of the string's contents.

Lines 1520-1690 test the input character and branch accordingly.  I use MON.RDKEY to read the character, which means that the data could come from any I/O slot as well as the normal Apple Keyboard.  You could add more tests here, or remove some.  If it is a printing character, we fall into lines 1730-1810 to store the character in the input buffer and on the screen.  If the filed is now full, line 1810 jumps to the routine which passes the data to Applesoft.  Note that characters stored in the input buffer have the high-bit equal to zero (Applesoft likes them that way).  Characters written on the screen have the high-bit set to one, so that they print in NORMAL video.

Lines 1920-1990 handle the backspace character.  If you are at the beginning of a field, the routine will return with an exit code of 3.  Otherwise, the current character will be replace on the screen with an underline character, and the cursor will be backed up.

Lines 2030-2050 handle the right arrow.  Normally this just copies over a character on the screen.  Characters are picked up from the screen image, and the treated just as though they came from the keyboard.  Note that the right arrow will not advance over an underline character.

Lines 2090-2140 handle ESCAPE.  As I mentioned earlier, ESCAPE is ignored unless it is typed when the cursor is at the beginning of the field, and the field code is non-zero.  This is the only use for the field code in the input routine presented here, but you might think of many more uses and make your own modifications.
!np
Lines 2180-2190 make Applesoft allocate some space for the string in the normal string data space.  Then lines 2200-2270 set up the string variable's descriptor to point to this space.  Lines 2280-2310 move the string data from the input buffer up to the new place.  This code was copied from the "Fast String Input Routine" in AAL #7.

The input routine is presented here in a very simple form; I leave it up to you to modify it to suit your most demanding applications.
!np
Here is a brief sample showing how you might use the input routine to fill in five fields:

<sample program>
