home *** CD-ROM | disk | FTP | other *** search
/ UpTime Volume 2 #7 / utv2n7s1.d64 / ml < prev    next >
Encoding:
Text File  |  1988-01-01  |  7.6 KB  |  122 lines

  1. @1
  2.  
  3.  
  4.  Programming With Machine Language
  5.  
  6.       Volume Two, Number Seven
  7.  
  8.  
  9. Copyright 1989 by Robert Rockefeller
  10.         All Rights Reserved
  11.  
  12.             Published by
  13.         Softdisk Publishing
  14.  
  15.  
  16.  
  17.      This month we will continue our discussion of software resources available in the C64 ROMs by beginning coverage of the BASIC 2.0 ROM.  First we will examine the structure of BASIC programs as they are stored in memory.  For demonstration purposes the simple program below was created.  Following the program is a memory dump of the start of BASIC program memory, made with a monitor program.
  18.  
  19.  
  20. {SHIFT-@}c10 rem print                   
  21. {SHIFT-@}c20 print"hello ";: print"there.
  22. {SHIFT-@}c30 data print                  
  23.  
  24. {SHIFT-@}c:0801 0d 08 0a 00 8f 20 MHJ@O  
  25. {SHIFT-@}c:0807 50 52 49 4e 54 00 print@ 
  26. {SHIFT-@}c:080d 26 08 14 00 99 22 &HT@Y" 
  27. {SHIFT-@}c:0813 48 45 4c 4c 4f 20 hello  
  28. {SHIFT-@}c:0819 22 3b 3a 20 99 22 ";: Y" 
  29. {SHIFT-@}c:081f 54 48 45 52 45 2e there. 
  30. {SHIFT-@}c:0825 00 32 08 1e 00 83 @2H{$de}@C 
  31. {SHIFT-@}c:082b 20 50 52 49 4e 54  print 
  32. {SHIFT-@}c:0831 00 00 00 ff ff ff @@@{$de}{$de}{$de} 
  33.  
  34.  
  35.      Line 10 occupies memory from address $0801 to $080c.
  36.      Line 20 occupies $080d to 0825.  Line 30 occupies $0826 to $0831.
  37.  
  38.      Here is the structure of one line of a BASIC program:
  39.  
  40. {SHIFT-@}c{CBM-A}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-R}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-R}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-R}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-S}
  41. {SHIFT-@}c{SHIFT--}link{SHIFT--}line#{SHIFT--} ... text ... {SHIFT--}null{SHIFT--}
  42. {SHIFT-@}c{CBM-Z}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-E}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-E}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-E}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-X}
  43.  
  44.      The link uses two bytes and is a link to the next program line.  For example, the link in line 10 contains $080d, which is the start address of line 20.  The link in line 20 contains $0826, the start address of line 30.  The link in line 30, the last line in the program, contains $0832.  At address $0832 is two 0s or nulls.  When BASIC finds a 0 as the high byte of a line link it interprets this as the end of a program, and executes the END command.  This is why the END command is usually not required in Commodore BASIC programs.  The link is never referenced when BASIC is RUNning a program, except to check for a 0 in the high byte.  Only the LIST command needs the line link.  For this reason the link is sometimes used in simple protection schemes to prevent a program from being listed.
  45.  
  46.      Following the link is two bytes containing the line number, stored in normal 6502 format with the the low byte first.  For example, the line number for line 10 is stored at address $0803 and contains, naturally enough, $000a or 10.  The line number is used by LIST, and also by RUN, GOTO, THEN and GOSUB.
  47.  
  48.      Following the line number comes the BASIC text.  The text contains commands like PRINT or GOTO, functions like TAN() or MID$(), variables like A1 or ZQ$, certain types of punctuation such as commas, semi-colons and colons, string literals like "Abcd" and numeric literals like 123.45.
  49.  
  50.      Commands and functions are 'tokenized.'  This means for example that the PRINT command is not held as the letters P R I N T, but as a single byte of value $99.  This value is known as the 'token' for PRINT.  All commands and functions have their respective tokens.  This both saves memory and allows the interpreter to execute faster.  Tokens are always of value greater than 127, with the 7-bit set.  This allows the interpreter to very quickly and easily determine whether a byte is a token or not.
  51.  
  52.      Examine line 20 in the memory dump and find the PRINT token.  Notice in lines 10 and 30 that keywords such as PRINT are not tokenized if they follow a REM or DATA statememnt.  Keywords which are part of string literals are also not tokenized.  Try to find the REM token in line 10, and the DATA token in line 30.
  53.  
  54.      Numeric literals and variables are composed of normal PETSCII characters of value less than 128.  Variables must begin with an alphabetic character in order to easily distinguish variables from numbers.  The second character of a variable may be alphabetic or numeric.  String literals can contain characters of any value, including graphics and upper case characters which normally have values of $a0 to $bf and $c0 to $df respectively.
  55.  
  56.      The below table illustrates the tokens that BASIC uses to store program keywords.  Notice that * / + - ^ < = > each have their own respective tokens.
  57.  
  58.  
  59.  
  60. {SHIFT-@}c{CBM-A}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-R}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-R}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-S}
  61. {SHIFT-@}c{SHIFT--}80 END    {SHIFT--}99 PRINT  {SHIFT--}b2 =      {SHIFT--}
  62. {SHIFT-@}c{SHIFT--}81 FOR    {SHIFT--}9a CONT   {SHIFT--}b3 <      {SHIFT--}
  63. {SHIFT-@}c{SHIFT--}82 NEXT   {SHIFT--}9b LIST   {SHIFT--}b4 SGN    {SHIFT--}
  64. {SHIFT-@}c{SHIFT--}83 DATA   {SHIFT--}9c CLR    {SHIFT--}b5 INT    {SHIFT--}
  65. {SHIFT-@}c{SHIFT--}84 INPUT# {SHIFT--}9d CMD    {SHIFT--}b6 ABS    {SHIFT--}
  66. {SHIFT-@}c{SHIFT--}85 INPUT  {SHIFT--}9e SYS    {SHIFT--}b7 SIN    {SHIFT--}
  67. {SHIFT-@}c{SHIFT--}86 DIM    {SHIFT--}9f OPEN   {SHIFT--}b8 FRE    {SHIFT--}
  68. {SHIFT-@}c{SHIFT--}87 READ   {SHIFT--}a0 CLOSE  {SHIFT--}b9 POS    {SHIFT--}
  69. {SHIFT-@}c{SHIFT--}88 LET    {SHIFT--}a1 GET    {SHIFT--}ba SQR    {SHIFT--}
  70. {SHIFT-@}c{SHIFT--}89 GOTO   {SHIFT--}a2 NE     {SHIFT--}bb RND    {SHIFT--}
  71. {SHIFT-@}c{SHIFT--}8a RUN    {SHIFT--}a3 TAB(   {SHIFT--}bc LOG    {SHIFT--}
  72. {SHIFT-@}c{SHIFT--}8b IF     {SHIFT--}a4 TO     {SHIFT--}bd EXP    {SHIFT--}
  73. {SHIFT-@}c{SHIFT--}8c RESTORE{SHIFT--}a5 FN     {SHIFT--}be COS    {SHIFT--}
  74. {SHIFT-@}c{SHIFT--}8d GOSUB  {SHIFT--}a6 SPC(   {SHIFT--}bf SIN    {SHIFT--}
  75. {SHIFT-@}c{SHIFT--}8e RETURN {SHIFT--}a7 THEN   {SHIFT--}c0 TAN    {SHIFT--}
  76. {SHIFT-@}c{SHIFT--}8f REM    {SHIFT--}a8 NOT    {SHIFT--}c1 ATN    {SHIFT--}
  77. {SHIFT-@}c{SHIFT--}90 STOP   {SHIFT--}a9 STEP   {SHIFT--}c2 PEEK   {SHIFT--}
  78. {SHIFT-@}c{SHIFT--}91 ON     {SHIFT--}aa +      {SHIFT--}c3 LEN    {SHIFT--}
  79. {SHIFT-@}c{SHIFT--}92 WAIT   {SHIFT--}ab -      {SHIFT--}c4 STR$   {SHIFT--}
  80. {SHIFT-@}c{SHIFT--}93 LOAD   {SHIFT--}ac *      {SHIFT--}c5 VAL    {SHIFT--}
  81. {SHIFT-@}c{SHIFT--}94 SAVE   {SHIFT--}ad /      {SHIFT--}c6 ASC    {SHIFT--}
  82. {SHIFT-@}c{SHIFT--}95 VERIFY {SHIFT--}ae ^      {SHIFT--}c7 CHR$   {SHIFT--}
  83. {SHIFT-@}c{SHIFT--}96 DEF    {SHIFT--}af AND    {SHIFT--}c8 LEFT$  {SHIFT--}
  84. {SHIFT-@}c{SHIFT--}97 POKE   {SHIFT--}b0 OR     {SHIFT--}c9 RIGHT$ {SHIFT--}
  85. {SHIFT-@}c{SHIFT--}98 PRINT# {SHIFT--}b1 >      {SHIFT--}ca MID$   {SHIFT--}
  86. {SHIFT-@}c{CBM-Z}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-E}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-W}cb GO     {SHIFT--}
  87. {SHIFT-@}c                      {CBM-Z}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{SHIFT-*}{CBM-X}
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.    BASIC makes use of several important zero-page storage locations, including the below ones:
  99.  
  100. {SHIFT-@}cTXTTAB = $2b
  101. {SHIFT-@}cVARTAB = $2d
  102. {SHIFT-@}cARYTAB = $2f
  103. {SHIFT-@}cSTREND = $31
  104. {SHIFT-@}cFRETOP = $33
  105. {SHIFT-@}cMEMSIZ = $37
  106.  
  107.      These variables define the memory usage of the BASIC program area.  Txttab contains the address of the start of the BASIC program area.  Interestingly, address (TXTTAB)-1 must contain a zero or RUN and NEW will give SYNTAX ERRORs.  Normally TXTTAB contains $0801.  Therefore address $0800 must contain a 0.  Try POKEing a non-zero value there and try to use RUN or NEW.  [Editor's note: POKE a 0 back to $0800 to restore RUN and NEW's normal operation.]
  108.  
  109.      Variables in BASIC 2.0 are stored immediately after the BASIC program text.  VARTAB contains the address of the start of the BASIC variable storage area.  In our example program given above, BASIC text runs from address $0801 to $0833.  In that case VARTAB would contain the value $0834.
  110.  
  111.      Following simple variables in memory is storage for array variables.  ARYTAB contains the address of the start of the array storage area.  STREND contains the address of the end of the array storage area plus one.  For example if the last byte of memory used for array storage was at address $0932, then STREND would contain the value $0933.
  112.  
  113.      MEMSIZ defines the highest byte plus one which is usable by BASIC.  For example, if the value $7000 was POKEd into MEMSIZ the highest address usable by BASIC would be $6fff.  The memory from FRETOP to MEMSIZ is used for storage of strings, not string variables but string data.  For example, if we execute this piece of BASIC:
  114.  
  115. {SHIFT-@}cA$ = "SOME " + "TEXT"
  116.  
  117. a string variable named A$ would be created and stored in the simple variable storage area.  Any existing arrays would be moved up higher in memory to make room for the new variable, and the values in ARYTAB and STREND would be adjusted.
  118.  
  119.      The string assigned to A$, namely "SOME TEXT" would be stored at the top of memory, growing downwards toward the array storage area.  FRETOP is adjusted downwards as more strings are put into the string storage area.  If enough strings are created, eventually the string storage area and the array storage area collide.  BASIC will try to recover unused memory in the string storage area by performing the infamous garbage collection.  BASIC 2.0 garbage collection is infamous because it takes a very long time if large string arrays are being used.  If garbage collection cannot recover enough memory then an 'Out Of Memory Error' occurs.
  120.  
  121.      Next month we will continue our discussion of BASIC internals.
  122.