The Game of Life, printed in SYNC 1:2 (pp. 28-30), was written in Basic. This Basic program may be replaced with an assembly subroutine which displays each succeeding generation within a fraction of the time needed by the Basic program. Each generation is constructed and then displayed by using another assembly subroutine for creating a display file based on “A Display File in Machine Code” by Dr. I. S. Logan in the same issue (pp. 13-15).
Program to Enter Assembly Subroutines
Figure 1 is a listing of the program for entering the Hex formatted assembly instructions. Line 100 contains the space which will be occupied by these instructions. As always, once the subroutines have been entered, do not LIST the program because the system will hang up.
The assembly routine is entered one byte at a time. A backspace character, “/”, is provided in case an error is made in entry. This backspace can be used more than once. When the subroutine is completely entered, type in “END” to stop.
Figure 1: Program to Enter Hex Format.
100 REM [247 shift D's]
200 LET V=-1
210 LET V=V+1
220 IF (V/50)*50=V THEN CLS
230 IF (V/10)*10=V THEN PRINT
240 INPUT H$
250 IF H$="" THEN GO TO 240
260 PRINT H$;"#";
270 IF H$="END" THEN STOP
280 IF H$="/" THEN GO TO 310
290 POKE 16427+V,16*CODE(H$)+CO
DE(TL$(H$))-476
300 GO TO 210
310 LET V=V-1
320 GO TO 240
Assembly Subroutines
Figure 2 is a listing of the display file subroutine adapted from Dr. Logan’s article. When the routine is RUN, the display will show a palette of 10×30 locations surrounded by a border.
Figure 3 is a listing of the Game of Life subroutine. Its structure, similar to that of the Basic Game of Life, is shown in flowchart form in Figure 4. The rules for the birth and death of cells are the same as in the Basic version.
Figure 2: Display File Assembly Subroutine.
Decimal Hex Label Mnemonic Hex
Address Address Format Format
16427 402B START LD HL,(D-FILE) 2A 0C 40
E INC HL 23
F CALL EDGER CD 57 40
4032 LD B,10 06 0A
4 LINE PUSH BC C5
5 LD (HL),9 36 09
7 INC HL 23
8 LD B,30 06 1E
A SPACE LD (HL),0 36 00
C INC HL 23
D DJNZ SPACE 10 FB
F LD (HL),9 36 09
4041 INC HL 23
2 CALL DLIM CD 5E 40
5 POP BC C1
6 DJNZ LINE 10 EB
8 CALL EDGER CD 57 40
B END LD (DF-EA),HL 22 0E 40
E LD (DF-END),HL 22 10 40
4051 LD A,11 3E 0B
3 LD (LINE-CTR),A 32 25 40
6 RET C9
7 EDGER LD B,32 06 20
9 EDGE LD (HL),9 36 09
B INC HL 23
C DJNZ EDGE 10 FB
E DLIM LD A,117 3E 75
4060 INC A 3C
1 LD (HL),A 77
2 INC HL 23
3 RET C9
Entering the Program
After entering the program in Figure 1, type in the Hex Format column in the Display File subroutine in Figure 2. Type ‘END” when this has been done. To check to see if this has been correctly entered, type LET A USR(16427). The palette should appear immediately. If it does not, check the program.
Next, in line 290 change 16427 to 16498. Run the program again and enter the Hex Format column in the Game of Life subroutine in Figure 3.
The Basic Portion of the Game of Life program is listed in Figure 5. The first part of this program is also from Dr. Logan’s article. Replace lines 200-320 of the program entered from Figure 1 which are already in memory by entering lines 200-320 in Figure 5.
If possible, save this result. Errors may be corrected either by POKEing or by starting again.
Figure 3: Game of Life Assembly Subroutine. Starting address 16498 decimal.
Hex Hex Label Mnemonic Comment
Address Format Format
4072: DD 21 23 00 LIFE: LD IX,0023H
4076: ED 4B 0C 40 LD BC, (400CH) ;Display File Address
407A: DD 09 ADD IX,BC
407C: DD E5 PUSH IX ;1st PALETTE LOCATION
407E: 0E 0A LD C,0AH ;10 Rows
4080: 06 1E COUNT: LD B,1EH ;30 Rows
4082: 16 00 LOOPX: LD D,00H
4084: DD 7E FF LD A,(IX-01H) ;Begin Search for Neighbors
4087: CD F9 40 CALL TEST ;Call TEST routine
408A: DD 7E 01 LD A,(IX+01H)
408D: CD F9 40 CALL TEST ;Call TEST routine
4090: DD 7E E0 LD A,(IX-20H) ;Call TEST routine
4093: CD F9 40 CALL TEST ;Call TEST routine
4096: DD 7E DF LD A,(IX-21H)
4099: CD F9 40 CALL TEST ;Call TEST routine
409C: DD 7E DE LD A,(IX-22H)
409F: CD F9 40 CALL TEST ;Call TEST routine
40A2: DD 7E 20 LD A,(IX-20H)
40A5: CD F9 40 CALL TEST ;Call TEST Routine
40A8: DD 7E 21 LD A,(IX-21H)
40B7: 82 ADD D
40B8: DD 77 00 LD (IX+00H),A
40BB: DD 23 INC IX
40BD: 10 C3 DJNZ LOOPX ;to LOOPX for next Row
40BF: 0D DEC C ;Last Row?
40C0: 28 08 JR Z,A ;Finished Search, STart
SCORing
40C2: DD 23 INC IX STSCOR
40C4: DD 23 INC IX
40C6: DD 23 INC IX
40C8: 18 B6 JR COUNT
40CA: DD E1 STSCOR: POP IX ;1st PALLETTE LOCATION
40CC: 0E 0A LD C,0AH ;10 Rows
40CE: 06 1E BGNROW: LD B,1EH ;30 Columns
40D0: DD 7E 00 SCORE: LD A,(IX+00H) ;Memory Location
40D3: FE 03 CP 03H ;No cell, 3 neighbors
40D5: 28 0E JR Z,CELL ;To CELL
40D7: FE 82 CP 82H ;Cell, 2 neighbors
40D9: 28 0A JR Z,CELL ;To CELL
40DB: FE 83 CP 83H ;Cell, 3 neighbors
40DD: 28 06 FR Z,CELL ;To CELL
40DF: DD 36 LD (IX+00),00 ;No Cell
40E3: 18 04 JR CELL+1 ;Skip over CELL
40E5: DD 36 CELL: LD (IX+00),00 ;Create Living Cell
40E9: DD 23 INC IX ;Next Location
40EB: 10 E3 DJNZ SCORE ;To SCORE if not end of row
40ED: 0D DEC C ;Row counter
40EE: 28 08 JR Z,SREND ;Last Row?
40F0: DD 23 INC IX
40F2: DD 23 INC IX
40F4: DD 23 INC IX
40F6: 18 D6 JR BGNROW ;Next Row
40F8: C9 SREND: RET ;Back to Basic
40F9: FE 5F TEST: CP 5FH ;Living Cell?
40FB: F8 RET M ;Return to Counting if no Cell
40FC: 14 INC D ;Increment counter
40FD: C9 RET ;Return to Counting
402B: 2A 0C 40 START: LD HL,(400CH) ;Display File address
402E: 23 INC HL
402F: CD 57 40 CALL EDGER ;Call Edge Routine
4032: 06 0A LD B,0AH
4034: C5 LINE: PUSH BC
4035: 36 09 LD (HL),09H
4037: 23 INC HL
4038: 06 1E LD B,1EH
403A: 36 00 SPACE: LD (HL),00
403C: 23 INC HL
403D: 10 FB DJNZ SPACE ;Loop to SPACE
403F: 36 09 LD (HL),09H
4041: 23 INC HL
4042: CD 5E 40 CALL DLIM ;Row Delimiter
4045: C1 POP BC
4046: 10 EB DJNZ LINE ;Line
4048: CD 57 40 CALL EDGER ;Edge Routine
404B: 22 0E 40 END: LD (400EH),HL ;(DF-EA)
404E: 22 10 40 LD (4010H),HL ;(DF-END)
4051: 3E 0B LD A,0BH
4053: 32 25 40 LD (4025),A ;Line Counter
4056: C9 RET
4057: 06 20 EDGER: LD B,20H
4059: 36 09 EDGE: LD (HL),09H
405B: 23 INC HL
405C: 10 FB DJNZ EDGE ;Loop to EDGE
405E: 3E 75 DLIM: LD A,75H ;Row Delimiter
4060: 3C INC A
4061: 77 LD (HL),A
4062: 23 INC HL
4063: C9 RET
Larger Field for Larger Memories
For readers with additional memory a larger area can be created. For example, a 20×30 field can be created by:
1) Load the program.
2) Change line 240 to read “…A>600 …”
3) POKE 1635,20
POKE 16511,20
POKE 16589,20
4) SAVE the result.
Figure 5: Game of Life—Basic Portion.
100 REM, [Assembly Programs]
200 LET A=USR(16427)
210 PRINT "PRESS NO. OR 0"
220 INPUT A
230 IF A=0 THEN GO TO 280
240 IF A<1 OR >A 300 THEN GO TO
220
250 LET A=PEEK(16396)+PEEK(1639
7)*256+34+A+((A-1)/30)*3
260 POKE A,-128*(PEEK(A)=0)
270 GO TO 220
280 PRINT "PRESS NEWLINE"
285 FOR I=1 TO 100
290 LET A=USR(16498)
300 INPUT A$
305 IF A$="S" THEN STOP
310 NEXT I
320 STOP
Running the Program
After “RUN” has been typed, the display palette appears. Enter a starting generation by typing in position numbers as in Dr. Logan’s article. When the starting configuration is complete, type “0” NEWLINE, to display the second generation. Each succeeding generation is displayed by typing NEWLINE. To stop the program, type “S”.
A random starting generation may be entered by using the lines in Figure 6 as replacements in Figure 5.
Figure 6: Line Replacements for Random Start.
210 FOR I=1 TO 100
220 LET A=RND(300)
230
270 NEXT I
[Figure: Figure 4: Flowchart for Game of Life Subroutine]
Some Good Starting Generations
To begin your exploration of the Game of Life, try entering some of the following generations. The reason for my name choices should become evident.
“Migrating L”: 50,79,109,110,111
“E-Lights”: 135,136,137,138,139,165,167,169
“Hawk”: 74,77,104,105,106
“D-Hive”: 100,101,130,132,160,162,190,191
“Melting Snow”: 100,101,102,103,104,130,132,134,160,161,162,163,164
Watch especially the interaction between the different colonies and the borders. Different starting generations will create different succeeding patterns depending upon where they are placed within the palette.
For 8K ROM/16K RAM
This assembly version of the Game of Life may also be implemented on 8K ROM/16K RAM machines. Since there are quite a few changes, the entire revised listings are given below in Figure 7. The program to enter Hex format (Figure 1) may be used with the following line replacements:
220 IF INT(V/50)*50=V THEN CLS
230 IF INT(V/10)*10=V THEN PRINT
290 POKE 16514+V,16*CODE H$+
CODE H$(2 TO 2)-476
Use this revised hex-loading program to enter the assembly subroutines listed in Figure 7. Then type in the Basic program listed in Figure 8. The program does not need to press NEWLINE for each succeeding generation to be displayed, since a pause is taken between each display.
A next-generation version of the Game of Life might make use of pixel graphics, and a non-flashing display. This would effectively quadruple the “world” size, and some very complex patterns could be observed.
Figure 7. Game of Life Assembly Routine (8K ROM; 16K RAM)
Decimal Hex Label Mnemonic Hex
Address Address Format Format
16514 4082 START: LD HL,(D_FILE) 2A 0C 40
E INC HL 23
6 CALL EDGER CD A3 40
9 LD B,20 06 14
B LINE: PUSH BC C5
C LD (HL),8 36 08
E INC HL 23
F LD B,30 06 1E
4091 SPACE: LD (HL),0 36 00
3 INC HL 23
4 DJNZ SPACE 10 FB
6 LD (HL),8 36 08
8 INC HL 23
9 CALL DLIM CD AA 40
C POP BC C1
D DJNZ LINE 10 EB
F CALL EDGER CD A3 40
40A2 RET C9
3 EDGER: LD B,32 06 20
5 EDGE: LD (HL),8 36 08
7 INC HL 23
8 DJNZ EDGE 10 FB
A DLIM: LD A,117 3E 75
C INC A 3C
D LD (HL),A 77
E INC HL 23
F RET C9
40B0 NOP 00
1 NOP 00
16562 2 LIFE: LD IX,35 DD 21 23 00
6 LD BC,(D_FILE) ED 4B 0C 40
A ADD IX,BC DD 09
C PUSH IX DD E5
E LD C,20 0E 14
40C0 ZZZZ: LD B,30 06 1E
2 XXXX: LD D,0 16 00
4 LD A,(IX-1) DD 7E FF
7 CALL TEST CD 39 41
A LD A,(IX+1) DD 7E 01
D CALL TEST CD 39 41
40D0 LD A,(IX-32) DD 7E E0
3 CALL TEST CD 39 41
6 LD A,(IX-33) DD 7E DF
9 CALL TEST CD 39 41
C LD A,(IX-34) DD 7E DE
F CALL TEST CD 39 41
40E2 LD A,(IX+32) DD 7E 20
5 CALL TEST CD 39 41
8 LD A,(IX+33) DD 7E 21
B CALL TEST CD 39 41
E LD A,(IX+34) DD 7E 22
40F1 CALL TEST CD 39 41
4 LD A,(IX) DD 7E 00
7 ADD D 82
8 LD (IX),A DD 77 00
40FB INC IX DD 23
D DJNZ XXXX 10 C3
F DEC C 0D
4100 JR Z,YYYY 28 08
2 INC IX DD 23
4 INC IX DD 23
6 INC IX DD 23
8 JR ZZZZ 18 B6
A YYYY: POP IX DD E1
C LD C,20 0E 14
E TTTT: LD B,30 06 1E
4110 VVVV: LD A,(IX) DD 7E 00
3 CP 3 FE 03
5 JR Z,WWWWW 23 0E
7 CP 130 FE 82
9 JR Z,WWWWW 28 0A
B CP 131 FE 83
D JR Z,WWWWW 28 06
F LD (IX),0 DD 36 00 00
4123 JR UUUU 18 04
5 WWWWW: LD (IX),128 DD 36 00 80
9 UUUU: INC IX DD 23
B DJNZ VVVV 10 E3
D DEC C 0D
E JR Z,SSSS 28 08
4130 INC IX DD 23
2 INC IX DD 23
4 INC IX DD 23
6 JR TTTT 18 D6
8 SSSS: RET C9
9 TEST: CP 127 FE 5F
B RET M F8
C INC D 14
D RET C9
Figure 8. Game of Life Basic Portion (8K ROM)
100 REM ASSEMBLY PROGRAM
200 LET A=USR 16514
210 PRINT "PRESS NO. OR 0"
220 INPUT A
230 IF A=0 THEN GOTO 280
240 IF A<1 OR A>600 THEN GOTO 2
20
250 LET A=PEEK (16396)+PEEK (16
397)*256+34+A+INT ((A-1)/30)*3
260 POKE A,128*(PEEK (A)=0)
270 GOTO 220
280 FOR I=1 TO 1000
290 LET A=USR 16562
300 PAUSE 40
310 NEXT I
320 STOP
Figure 9. Line Replacements for Random Start (8K ROM)
210 FOR I=1 TO 100
220 LET A=RND*300+150
230 [DELETE]
270 NEXT I
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.