The ZX80 Operating Manual

Sinclair ZX Spectrum
COPING WITH CHARACTERS
You will have noticed that, in addition to the normal typewriter characters there are several other characters not usually found on a typewriter. In addition there are some usable characters not written on the keyboard.

Before we go on to investigate what characters can be printed, meet an interesting function: CHR$(X) where X is a number integer variable or expression.

It is used mainly in conjunction with the PRINT statement. CHR$(X) means "the character whose code is x"

Try this:

10 INPUT X
20 PRINT CHR$(X)
30 GO TO 10

What this program does is to ask for a number and then to print out the character whose code is the number entered. The codes lie between 0 and 255.

A useful program for listing the symbols is given below.

10 PRINT "ENTER CODE VALUE"
20 INPUT X
30 PRINT X; ". . .";CHR$(X)
40 PRINT
50 LET X = X + 1
60 GO TO 30

This lists 11 symbols together with their codes. starting with the code entered at the start of the program.

  Decimal Code     Character                                  Decimal Code     Character  
  0 Space   128 Inverse Space
  1 Null String   129 Inverse "
  2 Graphic   130 Graphic
  3 Graphic   131 Graphic
  4 Graphic   132 Graphic
  5 Graphic   133 Graphic
  6 Graphic   134 Graphic
  7 Graphic   135 Graphic
  8 Graphic   136 Graphic
  9 Graphic   137 Graphic
 10 Graphic   138 Graphic
 11 Graphic   139 Graphic
 12 £   140 Inverse £
 13 $   141 Inverse $
 14 :   142 Inverse :
 15 ?   143 Inverse ?
 16 (   144 Inverse (
 17 )   145 Inverse )
 18 -   146 Inverse -
 19 +   147 Inverse +
 20 *   148 Inverse *
 21 /   149 Inverse /
 22 =   150 Inverse =
 23 >   151 Inverse >
 24 <   152 Inverse <
 25 ;   153 Inverse ;
 26 ,   154 Inverse ,
 27 .   155 Inverse .
 28 0   156 Inverse 0
 29 1   157 Inverse 1
 30 2   158 Inverse 2
 31 3   159 Inverse 3
 32 4   160 Inverse 4
 33 5   161 Inverse 5
 34 6   162 Inverse 6
 35 7   163 Inverse 7
 36 8   164 Inverse 8
 37 9   165 Inverse 9
 38 A   166 Inverse A
 39 B   167 Inverse B
 40 C   168 Inverse C
 41 D   169 Inverse D
 42 E   170 Inverse E
 43 F   171 Inverse F
 44 G   172 Inverse G
 45 H   173 Inverse H
 46 I   174 Inverse I
 47 J   175 Inverse J
 48 K   176 Inverse K
 49 L   177 Inverse L
 50 M   178 Inverse M
 51 N   179 Inverse N
 52 O   180 Inverse O
 53 P   181 Inverse P
 54 Q   182 Inverse Q
 55 R   183 Inverse R
 56 S   184 Inverse S
 57 T   185 Inverse T
 58 U   186 Inverse U
 59 V   187 Inverse V
 60 W   188 Inverse W
 61 X   189 Inverse X
 62 Y   190 Inverse Y
 63 Z   191 Inverse Z
 64 Unused   192 Unused
 65 Unused   193 Unused
 66 Unused   194 Unused
 67 Unused   195 Unused
 68 Unused   196 Unused
 69 Unused   197 Unused
 70 Unused   198 Unused
 71 Unused   199 Unused
 72 Unused   200 Unused
 73 Unused   201 Unused
 74 Unused   202 Unused
 75 Unused   203 Unused
 76 Unused   204 Unused
 77 Unused   205 Unused
 78 Unused   206 Unused
 79 Unused   207 Unused
 80 Unused   208 Unused
 81 Unused   209 Unused
 82 Unused   210 Unused
 83 Unused   211 Unused
 84 Unused   212 "
 85 Unused   213 THEN
 86 Unused   214 TO
 87 Unused   215 ;
 88 Unused   216 ,
 89 Unused   217 )
 90 Unused   218 (
 91 Unused   219 NOT
 92 Unused   220 -
 93 Unused   221 +
 94 Unused   222 *
 95 Unused   223 /
 96 Unused   224 AND
 97 Unused   225 OR
 98 Unused   226 **   The 'raise to a power' operator
 99 Unused   227 =
100 Unused   228 >
101 Unused   229 <
102 Unused   230 LIST
103 Unused   231 RETURN
104 Unused   232 CLS
105 Unused   233 DIM
106 Unused   234 SAVE
107 Unused   235 FOR
108 Unused   236 GO TO
109 Unused   237 POKE
110 Unused   238 INPUT
111 Unused   239 RANDOMISE
112 Unused   240 LET
113 Unused   241 Unused   prints as ?
114 Unused   242 Unused   prints as ?
115 Unused   243 NEXT
116 Unused   244 PRINT
117 Unused   245 Unused   prints as ?
118 Unused   246 NEW
119 Unused   247 RUN
120 Unused   248 STOP
121 Unused   249 CONTINUE
122 Unused   250 IF
123 Unused   251 GO SUB
124 Unused   252 LOAD
125 Unused   253 CLEAR
126 Unused   254 REM
127 Unused   255 Unused   prints as ?

Inverse means that the character appears white on a black background. Inverse space is a black square, for instance.

The graphics characters are shown below: CHR$ (X) allows us to print any character we want.



There is a further group of facilities which enables us to handle characters; These are

(a) TL$ (string)   -   this gives the string minus its first character.

The string can be a literal string inside quotes or a string variable.

10 PRINT TL$ ("ABC")     would give

BC

10 PRINT TL$(G$)     will cut off the first character of string G$

(b) CODE (string)    -     this gives the code corresponding to the first character in a string (either a string variable or a literal string)

10 PRINT CODE("ABC") would print 38 which is the code for A.

An example of the way in which these can be used is the following program which accepts a string and prints it out in inverse video.

10 PRINT "ENTER YOUR STRING"
20 INPUT G$
30 PRINT G$
40 LET X = CODE (G$)
50 LET X = X+128
60 IF G$=CHR$ (1) THEN GO TO 100
70 PRINT CHR$ (X);
80 LET G$ = TL$(G$)
90 GO TO 40
100 STOP

Statement:

40 sets X = code of the first character of G$.

50 adds 128 to the code (this gives Inverse of the letters, digits and graphics).

60 test the string to see if it is a null string, i.e. has no characters in it. If it is a null string either the input string was a null string to begin with or all the characters have been converted to inverse video and printed.

70 prints the inverse video character.

80 chops off the character which has just been printed and the program then jumps back to 40 and the code for the next character is extracted. The program goes on until the string has been shortened to the null string.

There is a further function which can be useful. This is: STR$ (variable or number).

This allows an integer number or variable to be treated as a string variable

10 LET G$=STR$( 1234)

sets G$= "1234"

10 LET G$=STR$(X)

does the same thing for an integer variable — if X = 4852 then G$ = "4852".

So far we've not said very much about the graphics symbols. These have been designed so as to double the effective resolution of the display, which gives 23 lines of 32 characters each.

Here is a program which plots TWO bar charts on the same display.

10 LET X = 0      | Defines
20 PRINT “YZ – AXIS”   | variables
30 PRINT “X =”   | and
40 FOR I = 1 TO 21   | prints
50 LET Y = X   | titles;
60 LET Z = 24-X   | Generates Y
70 PRINT X   | and X
80 FOR J = 1 TO 20      
85 IF J > Y AND J = Z THEN PRINT CHR$ (3);      
90 IF J > Y AND J > Z THEN GO TO 135      
95 IF J = Y AND J > Z THEN PRINT CHR$ (11);      
100 IF J < Y AND J < Z THEN PRINT CHR$ (139);       
105 IF J < Y AND J = Z THEN PRINT CHR$ (139);      
110 IF J < Y AND J > Z THEN PRINT CHR$ (11);      
115 IF J = Y AND J < Z THEN PRINT CHR$ (139);      
120 IF J > Y AND J < Z THEN PRINT CHR$ (3);      
125 IF J = Y AND J = Z THEN PRINT CHR$ (139);      
130 NEXT J      
135 PRINT      
140 LET X=X+1      
150 NEXT I      

The program prints Z as black bars, Y as grey bars. Statements 80 to 130 determine what graphic symbol is to be used; or nothing at all.

This program calculates the graphs of Y = X and Z = 24 — X and plots them in bar chart form. As you will see if you run the program it produces a very clear, unambiguous display. You can use the same sort of trick to achieve greater resolution along the line as well as from line to line.

Lines 85 to 125 decide what character will be printed, depending on the relative size of J, Z and Y.

These examples are only scratching the surface when it comes to character manipulation and graphics. The possibilities are almost literally endless.

Sinclair ZX Spectrum

  Previous Page Back Next Page