ZX Classroom - CODE, CHR$, POKE, and PEEK in ZX Spectrum BASIC

ZX Revija #03

ZX Classroom: BASIC / Machine Code

CODE

Used to translate characters into their corresponding codes.

General form of the function:

CODE string

where:

  • string — any string of characters.

Note:

As is well known, there are 256 different characters, each with a unique code (a number between 0 and 255).

CHR$

Used to translate (convert) a number into the corresponding character.

General form of the function:

CHR$ number
CHR$ numeric variable
CHR$ (numeric expression)

The CHR$ function makes it possible to obtain the character whose code is defined as the rounded integer value of the element specified in the function.

POKE

Used to store a specified numeric value directly at a specified memory location.

General form of the statement:

POKE m,n

where:

  • m — the numeric value defining the memory location at which the numeric value n is to be stored. The range is 0<=m<=65535.
  • n — the numeric value to be stored at the memory location defined by m. The range is -255<=n<=255.

PEEK

Used to obtain the contents of a specified memory location.

General form of the function:

PEEK location

where:

  • location — the numeric value defining the memory location from which the contents are to be read.

USR

Has two different purposes. When used with a string argument, it is used to define user graphics (or new symbols). When used with a numeric argument, it is used to link a program written in BASIC with a program written in machine code.

USR string

where:

  • string — alphanumeric/numeric.

BIN

Used for the binary representation of numbers.

General form of the function:

BIN string

where:

  • string — a numeric string consisting of some combination of 8 consecutive zeros and ones.

Examples of the preceding functions and statements:

10 PRINT CODE

20 PRINT CODE

10 FOR n=32 TO 255
20 PRINT CHR$ n;
30 NEXT n

5 POKE 23756,0
6 POKE 23609,50

10 PRINT PEEK 23756

10 PRINT USR

10 LET a=BIN 10110
20 PRINT a

PLOT

Used to draw a point on the screen.

General form of the statement:

PLOT m,n

where:

  • m — the first coordinate (abscissa) of the point to be drawn. The range is 0<=m<=255.
  • n — the second coordinate (ordinate) of the point to be drawn. The range is 0<=n<=175.

The point is drawn by darkening the pixel whose coordinates are m and n.

Note:

One of the functions PAPER, INK, FLASH, INVERSE and OVER may be included within the PLOT statement in order to change the characteristics of the point to be drawn.

PLOT

Used to draw a straight line (i.e. a line segment), or part of a circle (i.e. an arc).

General form of the statement:

DRAW x,y     (a)
DRAW x,y,z   (b)

where:

  • x,y — the coordinates of the endpoint of the line segment/arc to be drawn.
  • z — defines the angle, expressed in radians. When z>0, an arc is drawn to the left; when z<0, an arc is drawn to the right.

Notes:

  1. Form (a) is used to draw a line segment, and form (b) to draw an arc.
  2. Coordinates x and y may also be negative; the applicable limits depend on the position of the starting point of the line segment to be drawn.
  3. One of the functions PAPER, INK, FLASH, INVERSE and OVER may be included within the DRAW statement in order to change the characteristics of the point on the line/arc to be drawn.

CIRCLE

Used to draw a circle.

General form of the statement:

CIRCLE x,y,z

where:

  • x,y — the coordinates of the centre of the circle, respectively the abscissa and ordinate.
  • z — the radius of the circle.

Note:

One of the functions PAPER, INK, FLASH, BRIGHT, INVERSE and OVER may be included within the CIRCLE statement in order to change the characteristics of a point on the circle.

POINT

Used to determine the characteristics of a particular point.

ZX Classroom: BASIC / Machine Code

The general form of the function is:

POINT (x,y)

where:

  • x,y are the coordinates of a particular point, with 0<=x<=255 and 0<=y<=175.

The POINT function returns a value of 0 or 1. If the point with coordinates (x,y) is the color of the INK, the result is 1; if the point is the color of the PAPER, the result is 0.

Examples of the preceding functions and commands:

10 FOR n=0 TO 100
20 PLOT n,n*n/100
30 NEXT n
10 PLOT 100,20
20 DRAW 150,50:PAUSE 0
30 DRAW -10,100
40 DRAW -140,-60
10 PLOT 10,30
20 DRAW 100,80,PI
30 DRAW 100,50,PI
40 DRAW -150,-60,-PI
50 DRAW -40,30,PI/2
10 FOR r=20 TO 80 STEP 8
20 CIRCLE r,85,r
30 CIRCLE 254-r,85,r
40 NEXT r
10 PLOT 60,160
20 PRINT POINT (60,160)
30 POINT (60,160)

PAUSE

Provides a temporary pause in the program.

The general form of the command is:

PAUSE n

where:

  • n defines the length of time for which the program is to be paused.

Time is measured in units of one-fiftieth of a second, with 0<=n<=65535.

If n=0, the pause is unlimited.

The PAUSE command can also be interrupted before time n has elapsed by pressing any key.

10 FOR a=0 TO 30
20 PRINT AT 5,a;
30 PAUSE 2:NEXT a
40 PRINT AT 5,31; INKEY$

INKEY$

Serves to obtain a character in response to a key being pressed at that moment. If no key is pressed at the appropriate moment, a null string is generated, i.e. a string of length 0.

The general form of the function is:

INKEY$

This function is important because it allows values to be entered while the program is running, without needing to use the ENTER key.

It is especially useful for video-game programs.

10 CLS
20 PRINT INKEY$:GO TO 10

SCREEN$

Serves to obtain the character at a particular screen position.

The general form of the function is:

SCREEN$ (x,y)

where:

  • x is the number defining the row in which the required character is located;
  • y is the number defining the column in which the required character is located.

If the character at the specified position cannot be recognized, the result is zero, i.e. a string whose length is zero.

10 PRINT ekran :PAUSE 0
20 PRINT SCREEN$ (0,3)

COMMANDS FOR WORKING WITH THE TAPE RECORDER

The first commands you learned on your Spectrum—what more can be said about them?

It would probably be superfluous to say anything, so let us simply mention that they are: SAVE, VERIFY, LOAD and MERGE.

COMMANDS FOR WORKING WITH THE PRINTER

The commands that support printer operation are:

LPRINT, LLIST and COPY.

COPY

Serves to copy the screen contents to the printer, copying the first 22 screen lines.

The general form of the command is:

COPY

There are also programs for making posters on some higher-quality printers (e.g. EPSON).

LLIST

Serves to print a program listing on the printer.

The general form of the command is:

LLIST
10 REM LLIST-command
20 PRINT AT 10,10; ZX REVIJA
30 LLIST
40 STOP

When this program is started, line 20 prints ZX REVIJA at a specified position on the screen, and line 30 prints the listing from line 10 to line 40—but only if the printer was switched on before the program was started.

LPRINT

Serves to print text on the printer.

The general form of the command is:

LPRINT
10 REM LPRINT-command
20 LPRINT This is an example of the LPRINT-command
30 STOP

When this program is started, the text in quotation marks on line 20 will be printed.

ZX Classroom: BASIC / Machine Code

Fragment 3/4.

Commands for Working with the ZX Microdrive

The ZX Microdrive is a special type of disk drive used for storing larger quantities of data. Its commands are: CAT, CLOSE#, ERASE, FORMAT, MOVE and OPEN#. We will not discuss these commands now, but when we cover the ZX Microdrive and how to use it, we will go over them in detail.

With this, we have finished our short course on BASIC. Anyone who wants to learn more about BASIC can order the BASIC-VIDEO-KURS V 2,00, priced at 40,00 din. on domestic cassettes, or 70,00 din. on TDK or SONY cassettes. Starting with this issue, we will cover MACHINE CODE in this column.

Machine Code

Let us assume that you have no idea what machine code is, how it is entered, and so on—in other words, that you know nothing about machine code. First, we will learn from BASIC. A BASIC program is made up of program lines. Each line has its own number, which determines its position in the program.

10 PRINT 10
20 NEW

This program shows that the number 10 will be printed first, and only then will the NEW command be executed. Of course, this happens very quickly, so you will barely notice the execution of the command on line 10. But if you put PAUSE 0 on line 15, the program will not be erased from memory until you press a key.

This means that the computer proceeds from the lower number to the higher one, unless there are GO TO or GOSUB commands.

A machine-code program has the same structure. The program is made up of machine instructions, each of which is stored in a specific way in a separate memory location.

Since the Spectrum’s central processing unit executes 686 different machine instructions, and because of the large number of codes, a symbolic machine language was introduced, entered using an ASSEMBLER.

This machine language is called mnemonic machine language, and its form looks roughly like this:

10 LD a,10

Here, 10 is the program line number, while LD a,10 corresponds to the command LET a=10. However, the instruction LD a,4D means PRINT. It all depends on the channel we open, that is, on the famous CALL, which determines the other functions in the program.

A machine-code program can also be entered using the DATA and POKE commands; that is, these commands are used to place instructions in memory. It is much easier to put codes into memory with the POKE command, but it is also dangerous, because the program may crash—that is, the computer may freeze—and everything will have to be repeated. All the effort up to that point will have been wasted, and if the program is long, it takes a lot of time; you can no longer correct the error.

Using the DATA command is much easier, because you can check what you have put in the DATA command again and again—even after the RUN command, if it reports an error in the DATA command.

But this method is also tedious if you are writing a longer machine-code program, which is why mnemonic, or symbolic, language was created. It is the fastest way to write and understand what you have written, and it is closest to the actual machine code that the computer understands.

To make machine-code programming easier to understand, in upcoming issues of ZX Revija we will include several tables to help you write machine-code programs.

First of all, you must understand how your computer works, as well as its capacity, memory locations, and so on. The legendary Z80A IC is responsible for program execution speed. It runs at 3,5 MHz, which is not particularly fast, but is quite sufficient for the fast operation of most programs.

Besides the ASSEMBLER mentioned above, you can also enter a machine-code program using a loader, DEC-loader, M.C.M, EZ-CODE and HEXLOAD. We will discuss each of them separately in upcoming issues. Now let us return to the ASSEMBLER: it translates mnemonic language into the Spectrum’s machine language, that is, into 0s and 1s.

The ASSEMBLER will assemble the program only after you have entered the entire program and pressed the appropriate key.

ZX Classroom: BASIC / Machine Code

If you make a mistake, you have to type everything in again, but you can avoid this by using better software. That is a brief introduction to the ASSEMBLER. Now, a little about the DISASSEMBLER. It reads what has been written in machine code. Since you can learn from it too, you absolutely must get one. And now, here are a few programs written in BASIC-MACHINE CODE:

ZVUK $UMA

10 DATA 243,33,0,0,17,255 251,201:FOR A=40001 TO 40031:READ N:POKE A,N:
NEXT A:FOR N=1TO100:
RANDOMIZE USR 40001:
NEXT N

ZVUK BUDILICE

10 DATA 6,16,17,2,0,33,16,245,201:FOR A=40001 TO 40020:READ N:POKE A,N:NEXT A:FOR N=1 TO 200:RANDOMIZE USR 40001:NEXT N
10 FOR N=16384 TO 22527
20 POKE N,INT(RNDx255)+1
30 NEXT N
10 FOR N=22528 TO 23295
20 POKE N,INT(RNDx100)+1
30 PAUSE 10
40 NEXT N

Press A to repeat
Press B to continue
LOADING ZX REVIJA

Share your thoughts about the article