

EARTH ATTACK

by David Mold

from ZX Computing Oct/Nov.1983



[NOTE: This program requires Issue 2 emulation because of the way]

[it uses IN for scanning the keyboard.                      JimG ]



With just a sprinkling of machine code, David Mold of

Cheshunt has devised an exciting space adventure for your ZX

Spectrum.





Written for the 16K and 48K Spectrum, this listing presents

you with an exciting game which makes great use of machine

code to save vital memory space. This program is divided

into four main sections. These are:



1) Lines 1 to 180 contain all the initialisation and jumps

   to the user-defined graphics and machine code

   initialising routines. Since the amount of initialisation

   which needs to be done in this program is very large,

   there is quite a long pause after RUNning before the

   action starts - however, I shall explain later how this

   problem can be overcome.



2) Lines 200 to 999 contain the main game loop, including its

   servant subroutines (scoring, 'smart bomb' and high score

   routines). This loop calls all the machine code routines,

   and ties together their responses and any other relevant

   BASIC needed for the 'action' of the game.



3) Lines 1000 to 2400 contain the data and POKEing routines

   to set up first the user-defined graphics, then the

   machine code.



4) Lines 3000 to 3250 contain the three pages of instructions

   and game details which the computer cycles through

   between games.



The capital letters in quotes in the following lines should

be entered in the Graphics mode and when the program is RUN

produce the corresponding user-defined graphics to that key:



140, 180, 200, 230, 245, 701, 715, 725, 3140





Variables



The variables used, in order of their appearance, are:



s$	- Initials of the highest scorer.

h$	- The high score total.

s	- Current score.

bo	- INT s/1000 - used in calculating bonus bombs.

n	- Used throughout the program as a control

	  variable in FOR ... NEXT loops.

a$	- Displays the number of ships remaining (initially four).

l	- Number of laser shots.

sb	- The number of smart bombs ready for use.

x,y	- The co-ordinates of the ship.

p	- The screen byte directly in front of the

	  ship (top byte of the character position).

rg	- The random variable used in selecting alien types.

b	- The variable used for POKEing into machine

	  code (address 32477) to denote the height of

	  the next building.

a	- The dummy variable for jumps to machine code.

p$	- Used in the high score routine to accept initials.



There are numerous other variables used in the program, but

they are mostly control or dummy variables which are only

used once and their use is fairly self-explanatory.





Welcome to the machine



There are three main machine code routines used in the

program. The data for them starts at line 2000, and I have

disassembled them for you in Figs. 3a, 4a and 4b.



First is the 'SMART BOMB' routine. This is 26 bytes long and

starts at address 32400 Using the attributes file of the

Spectrum display, it changes the PAPER colour of every

character position in the central third of the display (the

part which the 'action' of this game utilises) to red.

Whilst doing this it also calculates how many of the

character positions previously contained yellow as their INK

colour. This is the number which it returns to the variable

s, when it returns to BASIC, which is then multiplied by 10

to increase your score according to how many aliens your

smart bomb destroyed.



Next comes the 'SIDE SCROLL' and 'BUILD CITY' routines,

which actually run together, and are called by USR 32426.

The height that the skyscrapers are built up to depends upon

the variable b, which is POKEd into location 32477. The

routine operates once again only on the central third of the

display. The reasons for this are:



1) This is in keeping with the character of the game;

2) it speeds up the workings of code which has to operate on

   the thousands of bytes in the Spectrum's D_FILE; and

3) due to what is termed the curious construction of the

   display file, it makes manipulating one third of the screen

   at a time a fairly simple task, but crossing between the

   thirds rather complicated.



The last of these three main routines is the 'CHECK FIRING'

routine. Actually it does rather more than its name

suggests. Its action is as follows:



First the laser fire of the ship is drawn. At each step of

this a check is made to see whether an alien has been hit or

not.



If an alien has been hit, it checks whether this is a saucer

or a monster, and adjusts the value of bc accordingly. It

then creates a two stage explosion.



The routine then erases the laser fire and returns to BASIC.





In short ...



There are two other shorter machine code routines also

included. The routine which starts at address 32350 and

shown in Fig.3b is a servant subroutine for the 'CHECK

FIRING' code. When called from within another routine, it

prints a character to the screen by POKEing the eight bytes

which follow the call instruction to the character position,

the start of which is specified by HL.



The routine starting at address 32300 produces the display

of the city being destroyed at the end of the program, and

is disassembled in Fig.3c.



I mentioned before, the delay caused by the setting up of

the machine code and user-defined graphics. Another problem

with all this data is that it is an incredible waste of

space, since once it has all been POKEd into the relevant

addresses when the program was first run, it justs sits

there cluttering up your machine. And since the program as

it stands uses very nearly all the space in a 16K machine,

this means that, should you be tempted, you would find it

hard to squeeze in many more lines before the Spectrum gives

up the ghost.



The answer is, of course, to SAVE the code and user-defined

graphics in a separate file on tape, and re-LOAD them each

time you LOAD the program from tape. This is a very quick,

efficient method which really makes use of the space saving

advantages which machine code can offer.



So, once your program has been typed in and you have RUN it

once or twice to ensure that there are no errors in your

typed- in version, you can erase every program line between

1000 and 2400 inclusive. Then type in the program lines

shown in Fig.1. Do not worry that some of these lines

overwrite program lines that still exist - this is intended.



Now, find some room on a cassette somewhere, and SAVE the

program and the two code files by typing:



RUN 2000



When re-LOADed, the program will automatically LOAD the two

files and then RUN itself.





Time for fun!



And now, after all this effort, for the fun part - playing

the game Fig.2 shows the keys used to control your rocket

ship as it wings its way over a terrestrial cityscape. To

add amusement there are a whole crop of little alien saucers

out to get you. Although they don't actually shoot at you,

there is a plentiful supply of them, and you can be

destroyed in any of three ways: by crashing into them, by

crashing into the city below you, or by accidentally moving

into 'hyperspace' above. In fact, your little ship is not

very sturdy at all, but you do have one weapon on your side

- your lasers!



To start with you have 20 lasers, and your laser count is

decremented by one each time you use them. You score 10

points for each saucer you hit, and 20 for each alien being

(little yellow 'nasties' who stand on rooftops and are very

hard to hit). Each time you bring the nose of your ship into

contact with a green fuel base, your number of lasers is

incremented by 20. It does not matter if you plough right

through these fuel bases.



You get three smart bombs per 1,000 points scored. While the

little bomb graphics are still showing beneath the city,

each time you press the Space key the sky will turn red, and

all visible aliens will be wiped out, and you will be given

10 points for each. Your ship will then accelerate out of

the dangerous area.



The game ends when all five of your ships have been

destroyed.





= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Fig.1



  30 LOAD "mcode" CODE

  40 LOAD "chars" CODE USR "a"

2000 SAVE "attack" LINE 2050

2010 SAVE "mcode" CODE 32300,300

2020 SAVE "chars" CODE USR "a",6*8

2030 PRINT "SAVEing finished": STOP

2050 RUN





= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Fig.2



         EARTH ATTACK

Use Keys:

         1-5......Up

         Q-T......Down

         6-0......Fire

         SHIFT....Extra Speed

         SPACE....Smart Bombs

PRESS 'S' TO START





= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Fig.3a 'SMART BOMB' disassembled



	ld hl,ATTR_FILE

	ld c,0

	ld b,255

LOOP	inc hl

	ld a,hl

	cp 6

	jr nz,CHANGE

	inc c

	ld a,8

	ld (hl),a

	djnz LOOP

	ret

CHANGE	add a,16

	ld (hl),a

	djnz LOOP

	ret





= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Fig.3b 'CPOKE' disassembled



	ld b,8

LOOP	pop de

	ld a,(de)

	inc de

	push de

	ld (hl),a

	ld de,256

	add hl,de

	djnz LOOP

	ret





= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Fig.3c End of game display disassembled



	ld a,7

LOOP2	ld hl,ATTR_FILE

	ld b,255

LOOP1	ld (hl),a

	inc hl

	djnz LOOP1

	halt

	dec a

	jr nz,LOOP2

	ld b,l

LOOP5	ld hl,D_FILE

LOOP4	ld c,32

	xor a

LOOP3	ld a,(hl)

	srl a

	ld (hl),a

	dec hl

	dec c

	jr nz,LOOP3

	ld a,71

	cp h

	jr nz,LOOP4

	djnz LOOP5

	ret





= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Fig.4a 'SIDE SCROLL' and 'BUILD CITY' disassembled



SIDE SCROLL:

	ld b,64

	ld hl,D_FILE

	ld de,32

	ld a,0

LOOP1	ld (hl),a

	add hl,de

	djnz LOOP1

	ld hl,ATTR_FILE

	ld b,8

	ld de,32

	ld a,7

LOOP2	ld (hl),a

	add hl,de

	djnz LOOP2

	ld bc,2048

	ld de,D_FILE

	ld hl,D_FILE+1

	ldir

	ld bc,256

	ld de,ATTR_FILE

	ld hl,ATTR_FILE+1

	ldir

BUILD CITY:

	ld b,0

	ld c,15

LOOP3	ld a,22

	rst 16

	ld a,c

	rst 16

	ld a,31

	rst 16

	ld  a,16

	rst 16

	ld a,5

	rst 16

	ld a,17

	rst 16

	ld a,0

	rst 16

	ld a,148

	rst 16

	dec c

	djnz LOOP3





= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Fig.4b 'CHECK FIRING' disassembled



	ld c,0

	ld hl,(DFCC)

	ld de,768

	add hl,de

	push hl

	ld b,5

LOOP1	ld a,(hl)

	cp 129

	jr z,X1

	cp 127

	jr z,X2

	ld (hl),255

	inc hl

	djnz LOOP1

	halt

	halt

	halt

	halt

	jr X3

X2	inc c

X1	push bc

	sbc hl,de

	push hl

	call CPOKE

	defb 0,66,36,24,24,36,66,0

	halt

	halt

	pop hl

	push hl

	call CPOKE

	defb 153,90,16,199,227,8,90,153

	halt

	halt

	pop hl

	call CPOKE

	defb 0,0,0,0,0,0,0,0

	pop bc

	inc c

X3	pop hl

	ld a,5

LOOP2	ld (hl),0

	inc hl

	inc b

	cp b

	jr nz,LOOP2

	ld b,0

	ret





--

Another Fine Product transcribed by:

Jim Grimwood (jimg@globalnet.co.uk), Weardale, England

--

