ASC

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: ASC
Abbreviation: A, Shift+S
Type: Function
Token code: 198/$C6
Handling routine
in BASIC ROM:
46987–47002
$B78B–B79A
List of all BASIC keywords


Remark: This article describes the BASIC command ASC in BASIC V2 at the Commodore 64.

Type: Numerical Function 
General Programming-Syntax: ASC(<String>)

The ASC keyword takes the first char of a string and maps it to the numeric index of the Commodore ASCII-table. This value can be converted back to the character by using the CHR$ function.

Evaluation of an empty string ("") results in the BASIC-error ?ILLEGAL QUANTITY ERROR. Since BASIC 7.0 this has been fixed and ASC("") returns the value 0. See Examples section for common usage.
If the operand doesn't evaluate to a string the BASIC error ?TYPE MISMATCH ERROR appears. If no operand is provided the error message ?SYNTAX ERROR shows up.

Examples[edit | edit source]

Parameter[edit | edit source]

PRINT ASC("A")
 65

PRINT ASC("ABC")
 65

PRINT ASC("")
?ILLEGAL QUANTITY  ERROR

PRINT ASC
?SYNTAX  ERROR

PRINT ASC(1)
?TYPE MISMATCH  ERROR

BASIC 7.0[edit | edit source]

PRINT ASC("")
 0

No error condition anymore!

Common usage[edit | edit source]

In a program the efficient way to use the ASC function is (in regard to cope with the special case "empty string") is

10 Z$=CHR$(0)
...
1000 A=ASC(A$+Z$)

The variable definition speeds the thing up (avoiding the evaluation of the CHR$ function and its parameter). The definition of Z$ should be placed at the very beginning to ensure a fast access to this variable.
Other variants like

A=ASC(A$+CHR$(.))

or

IF A$="" THEN A$=CHR$(.)
A=ASC(A$)

and so on are either impractical or take to much code space. However they are always slower.

Read a sequential file bytewise[edit | edit source]

 10 Z$=CHR$(0)
 100 OPEN 2,8,3,"FILENAME,S,R"
 110 FOR S=0 TO 1
 120 GET#2,A$
 130 A=ASC(A$+Z$)
 140 S=ABS(ST)
 150 PRINT A;
 160 NEXT S
 170 CLOSE 2
 ...

The command GET# needs special treatment. If a CHR$(0) is read the result is an empty string, which can be fixed as follows:

  • if A$ is empty, concatenate string Z$ (contains CHR$(0)) which results in the final string containing CHR$(0).
  • if A$ is not empty the concatenation doesn't change anything because ASC() takes only the first character.

The recommendation is to use a variable for CHR$(0), see above Common Usage.
The FOR-NEXT loop has its advantage to speed things up and terminates as soon the status (ST system-variable) return a value not equal 0 (this happens on end-of-file or error condition) which is achieved by manipulating the FOR-NEXT variable S.


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT