### ZX Spectrum BASIC String Slicing Examples Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap08.md Demonstrates the use of string slicing in ZX Spectrum BASIC to extract substrings. It shows how to specify start and end points, use default values, and handle cases where start is greater than finish. ```basic 10 LET a$="abcdef" 20 FOR n=1 TO 6 30 PRINT a$(n TO 6) 40 NEXT n 50 STOP ``` ```basic 10 LET a$="ABLE WAS I" 20 FOR n=1 TO 10 30 PRINT a$(n TO 10),a$((10-n) TO 10) 40 NEXT n 50 STOP ``` ```basic "abcdef"(2 TO 5)="bcde" ``` ```basic "abcdef"( TO 5)="abcdef"(1 TO 5)="abcde" ``` ```basic "abcdef"(2 TO )="abcdef"(2 TO 6)="bcdef" ``` ```basic "abcdef"( TO )="abcdef"(1 TO 6)="abcdef" ``` ```basic "abcdef"() ``` ```basic "abcdef"(3)="abcdef"(3 TO 3)="c" ``` ```basic "abcdef"(8 TO 7)="" ``` ```basic "abcdef"(1 TO 0)="" ``` -------------------------------- ### ZX Spectrum BASIC RUN Command Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap02.md Demonstrates how to run a specific part of a BASIC program by specifying a starting line number with the RUN command. 'RUN 100' starts execution from line 100. ```basic RUN 100 ``` -------------------------------- ### ZX Spectrum BASIC: Subroutines with GO SUB and RETURN Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Illustrates modular programming using subroutines. GO SUB calls a block of code starting at a specified line number, and RETURN sends execution back to the statement following the GO SUB. This example is part of a guessing game. ```basic 10 REM Subroutine example 20 INPUT a: CLS 30 INPUT "Guess the number ",b 40 IF a=b THEN PRINT "Correct": STOP 50 IF ab THEN GO SUB 100 70 GO TO 30 100 PRINT "Try again" 110 RETURN ``` -------------------------------- ### ZX Printer: LPRINT and LLIST Example Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap21.md Demonstrates the use of LPRINT and LLIST to send output to the ZX printer, including printing the character set. This code requires a ZX printer to be attached. ```basic 10 LPRINT "This program".' 20 LLIST 30 LPRINT '"prints out the character set." 40 FOR n=32 TO 255 50 LPRINT CHR$ n 60 NEXT n ``` -------------------------------- ### ZX Spectrum BASIC INPUT and GO TO Loop Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap02.md Demonstrates using the INPUT command to get user input into a variable (F) and the GO TO command to create an infinite loop, continuously prompting for input. ```basic 40 INPUT F 60 GO TO 40 ``` -------------------------------- ### Listing Program (LIST) in ZX Spectrum BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appc2.md Lists the program to the screen. LIST without arguments starts from the beginning. LIST n lists starting from line number n, making n the current line. This command is intended for screen display. ```zx-spectrum-basic LIST ``` ```zx-spectrum-basic LIST n ``` -------------------------------- ### Simple Plot and Draw Commands - ZX Spectrum BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap17.md Demonstrates basic usage of PLOT and DRAW commands. The first PLOT sets the starting point, and DRAW then draws a line from that point to a relative position. The second PLOT sets a new starting point for another DRAW command. ```basic PLOT 0,100: DRAW 80,-35 PLOT 90,150: DRAW 80,-35 ``` -------------------------------- ### BASIC READ, DATA, PRINT Example Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap06.md Demonstrates the basic usage of READ, DATA, and PRINT to assign and display values. The READ statement fetches values from the DATA statement, and PRINT displays them. The order of variables in PRINT can be changed to show flexibility. ```basic 10 READ a,b,c 20 PRINT a,b,c 30 DATA 10,20,30 40 STOP ``` ```basic 10 READ a,b,c 20 PRINT b,c,a 30 DATA 10,20,30 40 STOP ``` -------------------------------- ### ZX Spectrum BASIC: Program Structure and Line Numbers Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Demonstrates the fundamental structure of a ZX Spectrum BASIC program, highlighting the use of line numbers for execution sequence and statement labeling. It includes a simple temperature conversion example. ```basic 10 REM Temperature conversion program 20 PRINT "deg F", "deg C" 30 PRINT 40 INPUT "Enter deg F", F 50 PRINT F,(F-32)*5/9 60 GO TO 40 ``` -------------------------------- ### ZX Spectrum BASIC: Unconditional Branching with GO TO Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Shows how to alter the program flow using unconditional branching with the GO TO statement, directing execution to a specified line number. This example demonstrates skipping a line of code. ```basic 10 PRINT "Start" 20 GO TO 40 30 PRINT "This will be skipped" 40 PRINT "End" ``` -------------------------------- ### Read and Write to I/O Ports (BASIC) Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Basic code examples for reading from and writing to hardware I/O ports on the ZX Spectrum. Demonstrates reading keyboard input and writing values to specific ports, with a caution for output operations. ```basic 10 REM Read keyboard port 20 LET value=IN 254 30 PRINT value ``` ```basic 10 REM Write to port (use with caution) 20 OUT 254,value ``` ```basic 10 REM Detect key presses without waiting 20 LET k$=INKEY$ 30 IF k$="" THEN PRINT "No key pressed" 40 IF k$<>"" THEN PRINT "Key: ";k$ ``` -------------------------------- ### BASIC READ, RESTORE, DATA Example Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap06.md Demonstrates the use of the RESTORE statement to reset the data pointer. The first READ statement assigns values, then RESTORE resets it, allowing subsequent READ statements to fetch data again from the beginning or a specified line. ```basic 10 READ a,b 20 PRINT a,b 30 RESTORE 10 40 READ x,y,z 50 PRINT x,y,z 60 DATA 1,2,3 70 STOP ``` -------------------------------- ### ZX Spectrum BASIC: Line Drawing Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Shows how to draw lines on the ZX Spectrum screen using the DRAW command. Lines can be drawn relative to the last PLOT position using specified X and Y offsets. The first example draws two lines from arbitrary starting points. The second example draws a series of lines with random endpoints and varying ink colors, creating a dynamic visual effect. ```basic 10 PLOT 0,100: DRAW 80,-35 20 PLOT 90,150: DRAW 80,-35 10 BORDER 0: PAPER 0: INK 7: CLS 20 LET x1=0: LET y1=0 30 LET c=1 40 LET x2=INT (RND*256): LET y2=INT (RND*176) 50 DRAW INK c;x2-x1,y2-y1 60 LET x1=x2: LET y1=y2 70 LET c=c+1: IF c=8 THEN LET c=1 80 GO TO 40 ``` -------------------------------- ### ZX Spectrum BASIC Set Paper Color Example Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap16.md Sets the paper (background) color for subsequent printed text on the ZX Spectrum. This example demonstrates changing the paper color to cyan (code 5) using the `PAPER` command. ```basic PAPER 5 ``` -------------------------------- ### BASIC: Convert Date to Day of Week Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appd.md This program takes a day, month, and year (20th century) as input and calculates the corresponding day of the week. It handles leap years and validates the input date. The core logic involves converting the date into a sequential number of days since the start of the century and then using modulo arithmetic to determine the day of the week. ```basic 10 REM convert date to day 20 DIM d$(7,6): REM days of week 30 FOR n=1 TO 7: READ d$(n): NEXT n 40 DIM m(12): REM lengths of months 50 FOR n=1 TO 12: READ m(n): NEXT n 100 REM input date 110 INPUT "day?";day 120 INPUT "month?";month 130 INPUT "year (20th century only)?";year 140 IF year<1901 THEN PRINT "20th century starts at 1901": GO TO 100 150 IF YEAR>2000 THEN PRINT "20th century ends at 2000": GO TO 100 160 IF month<1 THEN GO TO 210 170 IF MONTH>12 THEN GO TO 210 180 IF year/4-INT(year/4)=0 THEN LET m(2)=29: REM leap year 190 IF day>m(month) THEN PRINT "This month has only "; m(month); " days.": GO TO 500 200 IF day>0 THEN GO TO 300 210 PRINT "Stuff and nonsense. Give me a real date." 220 GO TO 500 300 REM convert date to number of days since start of century 310 LET y=year-1901 320 LET b=365*y+INT (y/4): REM number of days to start of year 330 FOR n=1 TO month-1: REM add on previous months 340 LET b=b+m(n): NEXT n 350 LET b=b+day 400 REM convert to day of week 410 LET b=b-7*INT (b/7)+1 420 PRINT day;"/";month;"/";year 430 FOR n=6 TO 3 STEP -1: REM remove trailing spaces 440 IF d$(b,n) <> " " THEN GO TO 460 450 NEXT n 460 LET e$=d$(b, TO n) 470 PRINT" is a "; e$; "day" 500 LET m(2)=28: REM restore February 510 INPUT "again?", a$ 520 IF a$="n" THEN GO TO 540 530 IF a$ <> "N" THEN GO TO 100 1000 REM days of week 1010 DATA "Mon", "Tues", "Wednes" 1020 DATA "Thurs", "Fri", "Satur", "Sun" 1100 REM lengths of months 1110 DATA 31, 28, 31, 30, 31, 30 1120 DATA 31, 31, 30, 31, 30, 31 ``` -------------------------------- ### Z80 Assembly to Machine Code Example Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap26.md Demonstrates a simple Z80 assembly routine and its translation into machine code bytes. This is the raw data that needs to be loaded into memory. ```assembly ld bc, 99 ret ``` ```machinecode 1,99,0,201 ``` -------------------------------- ### Listing Program to Printer (LLIST) in ZX Spectrum BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appc2.md Similar to LIST, but outputs the program listing to the printer. LLIST without arguments lists the entire program. LLIST n lists starting from line number n. ```zx-spectrum-basic LLIST ``` ```zx-spectrum-basic LLIST n ``` -------------------------------- ### ZX Spectrum BASIC Contrast Ink Example Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap16.md Demonstrates the 'contrast' attribute value (9) for `INK` and `PAPER` in ZX Spectrum BASIC. Using `INK 9` makes the ink color contrast with the paper color, becoming white on dark backgrounds and black on light backgrounds. This example shows `INK 9` with varying `PAPER` colors. ```basic INK 9: FOR c=0 TO 7: PAPER c: PRINT c: NEXT c ``` -------------------------------- ### BASIC: Convert Yards, Feet, Inches Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appd.md This program handles conversions between yards, feet, and inches. It includes subroutines to adjust the values into a standard form and to print them. The main subroutine normalizes the input by converting everything to inches, then recalculating yards, feet, and inches. ```basic 10 INPUT "yards?",yd,"feet?",ft,"inches?",in 40 GO SUB 2000: REM print the values 50 PRINT " = "; 70 GO SUB 1000: REM the adjustment 80 GO SUB 2000: REM print the adjusted values 90 PRINT 100 GOTO 10 1000 REM subroutine to adjust yd, ft, in to the normal form for yards, feet and inches 1010 LET in=36*yd+12*ft+in: REM now everything is in inches 1030 LET s=SGN in: LET in=ABS in: REM we work with in positive, holding its sign in s 1060 LET ft=INT (in/12): LET in=(in-12*ft)*s: REM now in is ok 1080 LET yd=INT (ft/3)*s: LET ft=ft*s-3*yd: RETURN 2000 REM subroutine to print yd, ft and in 2010 PRINT yd;"yd";ft;"ft";in;"in";: RETURN ``` -------------------------------- ### ZX Spectrum BASIC LIST Command Examples Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap02.md Demonstrates the behavior of the LIST command in ZX Spectrum BASIC, including how it interacts with the current and top lines of the program. It shows variations in output based on whether LIST is used alone, with a line number, or in conjunction with user input for scrolling. ```ZX Spectrum BASIC LIST LIST 22 ``` -------------------------------- ### ZX Spectrum BASIC: Scroll prevention with adjusted FOR loop Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap04.md This example illustrates how changing the starting value of a FOR loop (from 10 to 100) can affect screen output. It introduces the 'scroll?' prompt, allowing users to pause program execution and view text before it scrolls off-screen, and demonstrates how to stop the program using BREAK keys. ```basic REM Change 10 to 100 in line 10 ``` -------------------------------- ### ZX Spectrum BASIC: Loop Structures with FOR-NEXT Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Demonstrates iterative programming using FOR-NEXT loops. Programs can specify a range and an optional STEP increment for the loop counter. Examples include a graphical plot and a countdown. ```basic 10 FOR n=0 TO 255 20 PLOT n,88+80*SIN (n/128*PI) 30 NEXT n 10 FOR i=10 TO 1 STEP -1 20 PRINT i;' " " 30 NEXT i 40 PRINT "Liftoff!" ``` -------------------------------- ### ZX Spectrum BASIC: Scientific Notation Examples Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap07.md Illustrates the use of scientific notation in ZX Spectrum BASIC for representing numbers, including positive and negative exponents. It also shows how the computer may switch to scientific notation for large numbers. ```basic PRINT 2.34e0 PRINT 2.34e1 PRINT 2.34e2 PRINT 2.34e15 PRINT 2.34e-1 PRINT 2.34e-2 ``` -------------------------------- ### ZX Spectrum BASIC: Arithmetic Expression Example Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap07.md Demonstrates a basic arithmetic expression in ZX Spectrum BASIC, calculating tax based on a 'sum' variable. This showcases combining operations and variable lookup within an expression. ```basic LET tax=sum*15/100 ``` -------------------------------- ### ZX Spectrum BASIC: Conditional Execution with IF-THEN Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Explains conditional logic using the IF-THEN statement. It allows program execution to branch based on comparison operators (=, <, >, <=, >=, <>). This example features a number guessing game. ```basic 10 REM Guess the number 20 INPUT a: CLS 30 INPUT "Guess the number", b 40 IF b=a THEN PRINT "That is correct": STOP 50 IF ba THEN PRINT "That is too big, try again" 70 GO TO 30 ``` -------------------------------- ### Load Data from Tape (BASIC) Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap20.md Loads data from cassette tape with a specified name. The 'CODE' keyword indicates that raw bytes are being loaded. Optional 'start' and 'length' parameters specify the memory address and maximum number of bytes to load, respectively. If omitted, 'start' defaults to the original save address, and 'length' is ignored (all bytes are read). ```basic LOAD "picture" CODE LOAD name CODE start,length LOAD "picture" SCREEN$ ``` -------------------------------- ### ZX Spectrum BASIC: Array Declaration and Usage Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Demonstrates how to declare and utilize arrays for storing numeric and string data. Includes examples of one-dimensional numeric arrays and two-dimensional string arrays, along with their initialization and access. ```basic 10 DIM numbers(10) 20 FOR i=1 TO 10 30 LET numbers(i)=i*i 40 NEXT i 50 PRINT numbers(5) 10 DIM days$(7,6) 20 FOR n=1 TO 7: READ days$(n): NEXT n 30 PRINT days$(1) 100 DATA "Mon","Tues","Wednes","Thurs","Fri","Satur","Sun" ``` -------------------------------- ### ZX Spectrum BASIC: Draw Character with Arms and Legs Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appd.md This snippet demonstrates drawing a character using PLOT and DRAW commands in ZX Spectrum BASIC. It defines routines for drawing arms, legs, and a face, utilizing the OVER command for color control. The code also includes a loop for user input to determine if the drawing should be repeated. ```zx spectrum basic 655 REM arms 660 PLOT 255,117: DRAW -15,-15: DRAW -15,15 670 OVER 0 680 PLOT 236,81: DRAW 4,21: DRAW 4, 21 690 OVER 1: REM legs 700 PLOT 255,66: DRAW -15,15: DRAW -15,-15 710 OVER 0 720 PLOT 236,60: DRAW 4,21: DRAW 4,-21 730 PLOT 237,127: DRAW 6,0,-PI/2: REM frown 740 PRINT AT 19,0;w$ 800 INPUT "again? ";a$ 810 IF a$="" THEN GO TO 850 820 LET a$=a$(1) 830 IF a$="n" THEN STOP 840 IF a$(1)="N" THEN STOP 850 RESTORE: GO TO 5 ``` -------------------------------- ### BASIC: I Ching Coin Toss Simulator Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appd.md This program simulates coin tosses to generate hexagrams for the I Ching. It performs six throws, each involving three coins, and displays the resulting hexagram patterns. The program uses random number generation to determine coin outcomes and includes a feature to erase itself after use. ```basic 5 RANDOMIZE 10 FOR m=1 TO 6: REM for 6 throws 20 LET c=0: REM initialize coin total to 0 30 FOR n=1 TO 3: REM for 3 coins 40 LET c=c+2+INT (2*RND) 50 NEXT n 60: PRINT " "; 70 FOR n=1 TO 2: REM 1st for the thrown hexagram, 2nd for the changes 80 PRINT "---"; 90 IF c=7 THEN PRINT "-"; 100 IF c=8 THEN PRINT " "; 110 IF c=6 THEN PRINT "X";: LET c=7 120 IF c=9 THEN PRINT "0";: LET c=8 130 PRINT "--- "; 140 NEXT n 150 PRINT 160 INPUT a$ 170 NEXT m: NEW ``` -------------------------------- ### Initialize and Use Month Name String Array in BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap12.md This example demonstrates initializing a string array `m$` with the names of the twelve months using READ and DATA statements. It also shows how to access and print specific month names using subscripts and how to concatenate strings. ```basic DIM m$(12,9) READ m$(1),m$(2),m$(3),m$(4),m$(5),m$(6),m$(7),m$(8),m$(9),m$(10),m$(11),m$(12) DATA "JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER" PRINT "now is the month of ";m$(5);"ing";" when merry lads are playing" ``` -------------------------------- ### ZX Spectrum BASIC: Setting Colors and Brightness via CHR$ Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap16.md These code examples demonstrate how to use CHR$ codes to control screen attributes like ink color, paper color, and brightness in ZX Spectrum BASIC. These are typically entered after a line number or within PRINT statements. ```BASIC REM CHR$ 17 for PAPER, CHR$ 6 for yellow REM CAPS SHIFT + digit for INK PRINT CHR$ 17; CHR$ 6; REM CHR$ 19 for brightness, CHR$ 0 for normal PRINT CHR$ 19; CHR$ 0; REM CHR$ 18 for flashing, CHR$ 1 for flashing PRINT CHR$ 18; CHR$ 1; REM CHR$ 20 for normal chars, CHR$ 0 for normal PRINT CHR$ 20; CHR$ 0; REM CHR$ 20 for inverse chars, CHR$ 1 for inverse PRINT CHR$ 20; CHR$ 1; ``` -------------------------------- ### ZX Spectrum BASIC: Using Control Characters for Color Manipulation Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap16.md This example demonstrates the use of CHR$ control characters to directly manipulate screen colors for INK, PAPER, FLASH, BRIGHT, INVERSE, and OVER. Each control character is followed by a color code character. ```basic PRINT CHR$ 16+CHR$ 9; . . . ``` -------------------------------- ### ZX Spectrum BASIC: Floating-Point Precision Demonstration Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap07.md Shows examples of floating-point arithmetic inaccuracies in ZX Spectrum BASIC. These snippets highlight how large numbers or specific operations can lead to unexpected results due to limited precision and rounding. ```basic PRINT 4294967295, 4294967295-429e7 ``` ```basic PRINT 1e10+1-1e10,1e10-1e10+1 ``` ```basic PRINT 5e9+1-5e9 ``` -------------------------------- ### ZX Spectrum BASIC: Saving Programs to Tape Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Provides examples of saving various types of data to cassette tape in ZX Spectrum BASIC using the SAVE command. This includes saving the current BASIC program, saving with an auto-run line, saving blocks of memory (CODE), and saving the screen contents. ```bash SAVE "myprogram" # Save current program SAVE "myprogram" LINE 100 # Auto-run from line 100 SAVE "data" CODE 30000,1000 # Save memory block SAVE "screen$" SCREEN$ # Save screen contents ``` -------------------------------- ### ZX Spectrum BASIC: Cursor Positioning Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Illustrates positioning text output on the ZX Spectrum screen using the PRINT AT command. The command takes the line (0-21) and column (0-31) as arguments, followed by the text to display. Examples show centering text, printing at the top-left and bottom-left corners, and printing text diagonally across the screen. ```basic 10 CLS 20 PRINT AT 10,15;"CENTER" 30 PRINT AT 0,0;"Top left" 40 PRINT AT 21,0;"Bottom left" 10 CLS 20 FOR row=0 TO 21 30 PRINT AT row,row;"*" 40 NEXT row ``` -------------------------------- ### ZX Spectrum BASIC: Accessing User Graphics Memory with USR Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap14.md This example demonstrates how to access the memory location for a user-defined character pattern using the USR function in ZX Spectrum BASIC. USR is used here to get the starting memory address associated with a specific user-defined character, allowing for its pattern to be read or modified. ```basic USR "P" USR "P"+1 USR "P"+7 ``` -------------------------------- ### ZX Spectrum BASIC: BEEP Command for Sound Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Shows how to generate simple tones using the BEEP command in ZX Spectrum BASIC. The command takes duration (in seconds) and pitch (semitones from middle C) as arguments. Examples include a single beep, a beep an octave higher, a short beep an octave lower, a musical scale, and a siren-like effect by rapidly increasing pitch. ```basic 10 BEEP 1,0 REM 1 second, middle C 20 BEEP 0.5,12 REM Half second, C one octave up 30 BEEP 0.2,-12 REM Short beep, C one octave down 10 REM Musical scale 20 FOR n=0 TO 12 30 BEEP 0.5,n 40 NEXT n 10 REM Siren effect 20 FOR n=1 TO 100 30 BEEP 0.05,n 40 NEXT n ``` -------------------------------- ### ZX Spectrum BASIC: Screen Formatting Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Demonstrates text formatting and layout control in ZX Spectrum BASIC using TAB, LINE, and other separators. The TAB command moves the cursor to a specified column, useful for creating aligned columns of text. The examples show simple tab usage for alignment and printing data in formatted columns. ```basic 10 PRINT TAB 10;"Hello" 20 PRINT "Col1";TAB 15;"Col2";TAB 30;"Col3" 30 PRINT AT 10,0;"New position" 10 PRINT "Name";TAB 15;"Age";TAB 25;"City" 20 PRINT "John";TAB 15;25;TAB 25;"London" ``` -------------------------------- ### ZX Spectrum BASIC: String Slicing and Substring Access Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Explains how to manipulate strings in ZX Spectrum BASIC, including accessing substrings using slicing notation (e.g., `a$(start TO end)`). It also shows how to extract single characters. ```basic 10 LET a$="Sinclair ZX Spectrum" 20 PRINT a$(1 TO 8) REM "Sinclair" 30 PRINT a$(10 TO) REM "ZX Spectrum" 40 PRINT a$(TO 8) REM "Sinclair" 50 LET name$="BASIC" 60 PRINT LEN name$ REM 5 70 PRINT name$(3) REM "S" ``` -------------------------------- ### BASIC ABS Function Example Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap09.md Demonstrates the usage of the ABS function in BASIC to get the absolute value of a number. It shows that ABS converts both positive and negative numbers to their positive equivalents. ```basic ABS -3.2 = ABS 3.2 = 3.2 ``` -------------------------------- ### Handle Two-Byte Variables in ZX Spectrum BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap25.md Demonstrates how to poke a value into a two-byte system variable and how to peek its value. This is crucial for modifying or reading multi-byte system data. The examples use BASIC's PEEK and POKE commands. ```basic REM Poking a value 'v' to a two-byte variable at address 'n' POKE n, v-256*INT (v/256) POKE n+1, INT (v/256) REM Peeking the value of a two-byte variable at address 'n' PEEK n+256*PEEK (n+1) ``` -------------------------------- ### Loading Files (LOAD) in ZX Spectrum BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appc2.md Loads program and variables from a file 'f'. Supports loading numeric arrays, string arrays ($), and raw machine code. Machine code loading can specify a starting address 'm' and a byte count 'n'. Loading SCREEN$ is a specific case for screen memory. ```zx-spectrum-basic LOAD f ``` ```zx-spectrum-basic LOAD f DATA () ``` ```zx-spectrum-basic LOAD f DATA $() ``` ```zx-spectrum-basic LOAD f CODE m,n ``` ```zx-spectrum-basic LOAD f CODE m ``` ```zx-spectrum-basic LOAD f CODE ``` ```zx-spectrum-basic LOAD f SCREEN$ ``` -------------------------------- ### BASIC: Pangolins Animal Guessing Game Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appd.md This is a 'Pangolins' game where the computer tries to guess an animal the user is thinking of by asking yes/no questions. If the computer doesn't know the animal, it learns a new question to distinguish it for future guesses. The game uses arrays to store questions and corresponding branching logic. ```basic 5 REM pangolins 10 LET nq=100: REM number of questions and animals 15 DIM q$(nq,50): DIM a(nq,2): DIM r$(1) 20 LET qf=8 30 FOR n=1 TO qf/2-1 40 READ q$(n): READ a(n,1): READ a(n,2) 50 NEXT n 60 FOR n=n TO qf-1 70 READ q$(n): NEXT n 100 REM start playing 110 PRINT "Think of an animal.","Press any key to continue." 120 PAUSE 0 130 LET c=1: REM start with 1st question 140 IF a(c,1)=0 THEN GO TO 300 150 LET p$=q$(c): GO SUB 910 160 PRINT "?": GO SUB 1000 170 LET in=1: IF r$="y" THEN GO TO 210 180 IF r$="Y" THEN GO TO 210 190 LET in=2: IF r$="n" THEN GO TO 210 200 IF r$"<>"N" THEN GO TO 150 210 LET c=a(c,in): GO TO 140 300 REM animal 310 PRINT "Are you thinking of" 320 LET P$=q$(c): GO SUB 900: PRINT "?" 330 GO SUB 1000 340 IF r$="y" THEN GO TO 400 350 IF r$="Y" THEN GO TO 400 360 IF r$="n" THEN GO TO 500 370 IF r$="N" THEN GO TO 500 380 PRINT "Answer me properly when I'm","talking to you.": GO TO 300 400 REM guessed it ``` -------------------------------- ### ZX Spectrum BASIC: USR for User-Defined Graphics Setup Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap14.md This program demonstrates how to use the USR function in conjunction with POKE to define custom characters (user-defined graphics) on the ZX Spectrum. It prompts the user to input eight binary numbers for each character, allowing for custom visual elements. ```basic 10 FOR n=0 TO 7 20 INPUT row: POKE USR "P"+n,row 30 NEXT n ``` -------------------------------- ### ZX Spectrum BASIC: Character Code Manipulation Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Demonstrates converting between characters and their ASCII numeric codes using CODE and CHR$ functions in ZX Spectrum BASIC. CODE returns the numeric code of a character, while CHR$ converts a numeric code back to its corresponding character. Examples include getting the code for 'A', converting code 65 to 'A', printing all printable ASCII characters, and iterating through a string to get character codes. ```basic 10 PRINT CODE "A" REM 65 20 PRINT CHR$ 65 REM "A" 30 FOR n=32 TO 127 40 PRINT CHR$ n; 50 NEXT n 10 LET a$="Hello" 20 FOR i=1 TO LEN a$ 30 PRINT CODE a$(i);" "; 40 NEXT i ``` -------------------------------- ### ZX Spectrum BASIC: Circle Drawing Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Demonstrates drawing circles on the ZX Spectrum screen using the CIRCLE command. The command takes the X and Y coordinates of the center and the radius as parameters. The examples show drawing multiple circles with different sizes and positions, and also drawing a series of concentric circles by varying the radius in a loop. ```basic 10 CLS 20 CIRCLE 128,87,50 30 CIRCLE 64,44,30 40 CIRCLE 192,131,40 10 FOR r=10 TO 80 STEP 10 20 CIRCLE 128,87,r 30 NEXT r ``` -------------------------------- ### ZX Spectrum BASIC: Conditional Expression Logic Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap13.md Illustrates the behavior of AND and OR operators in ZX Spectrum BASIC conditional expressions. It shows how AND can be used for concatenation or selection based on a non-zero condition, and how OR can be used in numeric contexts. The provided examples highlight the differences in default values (0 for AND, 1 for OR) affecting the outcome. ```basic LET total price=price less tax*(1.15 OR v$="zero rated") x$ AND y has the value {x$ if y is non-zero { "" if y is zero ``` -------------------------------- ### ZX Spectrum BASIC: Temporary Color Changes in PRINT Statements Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap16.md This example shows how to temporarily change colors within a PRINT statement using INK, PAPER, and other color-related commands. The color effect only lasts until the end of the current PRINT statement. ```basic PRINT PAPER 6;"x";: PRINT "y" ``` -------------------------------- ### Drawing a Semicircle Arc - ZX Spectrum BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap17.md This example demonstrates drawing a semicircle using the DRAW command with an angle parameter. It starts plotting at a specified coordinate and then draws an arc with a length defined by x and y, turned by PI radians (a semicircle). ```basic 10 PLOT 100,100: DRAW 50,50, PI ``` -------------------------------- ### ZX Spectrum BASIC: Program Control Flow with RUN and GO TO Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap02.md Explains the functionality of RUN and GO TO commands for controlling program execution. It differentiates RUN's behavior (clears variables and screen) from GO TO's (no clearing) and illustrates their use with line numbers and without. The STOP command for halting program execution during input is also covered. ```basic RUN 100 ``` ```basic GO TO 100 ``` ```basic RUN ``` ```basic STOP ``` -------------------------------- ### ZX Spectrum BASIC: Examining NEXT loop behavior with PRINT c Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap04.md This example investigates the state of the control variable 'c' after a FOR...NEXT loop has completed. It highlights how the control variable's value is updated one last time before the loop terminates, which can lead to unexpected results if not understood. ```basic PRINT c ``` -------------------------------- ### ZX Spectrum BASIC: Draw Man with Provided X Coordinate Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appd.md This ZX Spectrum BASIC routine draws a stick figure man at a specified X-coordinate. It includes subroutines for drawing the head, body, legs, and arms using CIRCLE and PLOT commands. The RETURN statement indicates the end of this drawing procedure, allowing it to be called from other parts of the program. ```zx spectrum basic 1000 REM draw man at column x 1010 REM head 1020 CIRCLE x,132,8 1030 PLOT x+4,134: PLOT x-4,134: PLOT x,131 1040 REM body 1050 PLOT x,123: DRAW 0,-20 1055 PLOT x,101: DRAW 0,-19 1060 REM legs 1070 PLOT x-15,66: DRAW 15,15: DRAW 15,-15 1080 REM arms 1090 PLOT x-15,117: DRAW 15,-15: DRAW 15,15 1100 RETURN ``` -------------------------------- ### Seed RND Sequence with RANDOMIZE in BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap11.md This example shows how to use the RANDOMIZE statement with a specific number to reset the RND sequence to a known starting point. This is useful for debugging or ensuring reproducible random number generation. The program prints five RND values after seeding with '1'. ```basic 10 RANDOMIZE 1 20 FOR n=1 TO 5: PRINT RND ,: NEXT n 30 PRINT: GO TO 10 ``` -------------------------------- ### ZX Spectrum BASIC GO TO Non-existent Line Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap02.md Shows how a GO TO command referencing a non-existent line number results in execution jumping to the next available line. ```basic GO TO 31 ``` -------------------------------- ### ZX Spectrum BASIC Animal Guessing Game Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/appendices/appd.md Implements a classic 'Animal Guessing Game' where the program learns new animals and questions. It uses DATA statements for initial animals and questions, and handles user input to expand its knowledge base. The game involves string manipulation and conditional logic. ```basic 410 PRINT "I thought as much.": GO TO 800 500 REM new animal 510 IF qf>nq-1 THEN PRINT "I'm sure your animal is very", "interesting, but I don't have","room for it just now.": GO TO 800 520 LET q$(qf)=q$(c): REM move old animal 530 PRINT "What is it, then?": INPUT q$(qf+1) 540 PRINT "Tell me a question which dist-","inguishes between " 550 LET p$=q$(qf): GO SUB 900: PRINT " and" 560 LET p$=q$(qf+1): GO SUB 900: PRINT " " 570 INPUT s$: LET b=LEN s$ 580 IF s$(b)="?" THEN LET b=b-1 590 LET q$(c)=s$(TO b): REM insert question 600 PRINT "What is the answer for" 610 LET p$=q$(qf+1): GO SUB 900: PRINT "?" 620 GO SUB 1000 630 LET in=1: LET io=2: REM answers for new and old animals 640 IF r$="y" THEN GO TO 700 650 IF r$="Y" THEN GO TO 700 660 LET in=2: LET io=1 670 IF r$="n" THEN GO TO 700 680 IF r$="N" THEN GO TO 700 690 PRINT "That's no good. ": GO TO 600 700 REM update answers 710 LET a(c,in)=qf+1: LET a(c,io)=qf 720 LET qf=qf+2: REM next free animal space 730 PRINT "That fooled me." 800 REM again? 810 PRINT "Do you want another go?": GO SUB 1000 820 IF r$="y" THEN GO TO 100 830 IF r$="Y" THEN GO TO 100 840 STOP 900 REM print without trailing spaces 905 PRINT " "; 910 FOR n=50 TO 1 STEP -1 920 IF p$(n)<>" " THEN GO TO 940 930 NEXT n 940 PRINT p$(TO n);: RETURN 1000 REM get reply 1010 INPUT r$: IF r$="" THEN RETURN 1020 LET r$=r$(1): RETURN 2000 REM initial animals 2010 DATA "Does it live in the sea",4,2 2020 DATA "Is it scaly",3,5 2030 DATA "Does it eat ants",6,7 2040 DATA "a whale", "a blancmange", "a pangolin", "an ant" ``` -------------------------------- ### ZX Spectrum BASIC: Console Input and Output Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Shows how to interact with the user through the console using INPUT for reading user data and PRINT for displaying output. Demonstrates different output formatting with commas and semicolons. ```basic 10 INPUT "Enter your name", name$ 20 INPUT "Enter your age", age 30 PRINT "Hello ";name$;", you are ";age;" years old" 40 PRINT name$,age REM comma tabs to middle of screen 50 PRINT name$;age REM semicolon prints immediately adjacent ``` -------------------------------- ### ZX Spectrum BASIC: Binary Number Representation Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Explains how to work with binary numbers in ZX Spectrum BASIC using the BIN keyword. The BIN keyword can be used to represent binary literals, which are then treated as their decimal equivalents by the BASIC interpreter. Examples show printing decimal values of binary literals and assigning them to variables. ```basic 10 PRINT BIN 10101010 REM 170 in decimal 20 PRINT BIN 11111111 REM 255 30 LET x=BIN 1100 40 PRINT x REM 12 ``` -------------------------------- ### Verify Data on Tape (BASIC) Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap20.md Verifies data on cassette tape against data in memory. The 'CODE' keyword is used for byte verification, with optional 'start' and 'length' parameters to specify the memory region to compare against. If 'length' is omitted, all bytes from the tape are checked. 'SCREEN$' is a shortcut for verifying the display file. ```basic VERIFY name CODE start,length VERIFY name CODE start VERIFY name CODE VERIFY name SCREEN$ ``` -------------------------------- ### ZX Spectrum BASIC: User-Defined Functions Source: https://context7.com/0x00cl/zx-spectrum-basic-programming/llms.txt Illustrates how to create and use custom functions in ZX Spectrum BASIC with the DEF FN statement. This allows for code modularity and reusability by defining specific calculations that can be called with different parameters. The examples show defining functions for squaring a number and calculating the hypotenuse of a right triangle. ```basic 10 DEF FN s(x)=x*x 20 PRINT FN s(5) REM 25 30 DEF FN h(a,b)=SQR (a*a+b*b) 40 PRINT FN h(3,4) REM 5 ``` -------------------------------- ### ZX Spectrum BASIC Commands Overview Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap02.md This section outlines common ZX Spectrum BASIC commands and their basic usage. It highlights that most commands can be used as direct commands or within program lines. Commands like RUN, LIST, CONTINUE, and NEW are mentioned as typically used directly but can function within programs. ```ZX Spectrum BASIC PRINT LET INPUT RUN LIST GO TO CONTINUE NEW REM ``` -------------------------------- ### ZX Spectrum BASIC: Calling a Subroutine Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap05.md Demonstrates the basic syntax for calling a subroutine using the GO SUB statement in ZX Spectrum BASIC. The computer remembers the return address to resume execution after the subroutine completes. ```basic GO SUB n ``` -------------------------------- ### Drawing Lines with Random Colors - ZX Spectrum BASIC Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap17.md This program draws a series of lines with changing colors. It starts from the bottom-left corner and draws lines to random endpoints, updating the starting point for each new line. Colors cycle from 1 to 7. ```basic 10 BORDER 0: PAPER 0: INK 7: CLS: REM black oue screen 20 LET x1=0: LET y1=0: REM start of line 30 LET c=1: REM for ink colour, starting blue 40 LET x2=INT (RND*256): LET y2=INT (RND*176): REM random finish of line 50 DRAW INK c;x2-x1,y2-y1 60 LET x1=x2: LET y1=y2: REM next line starts where last one finished 70 LET c=c+1: IF c=8 THEN LET c=1: REM new colour 80 GO TO 40 ``` -------------------------------- ### ZX Printer: LPRINT with AT and TAB Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap21.md Illustrates the difference in output buffering between PRINT and LPRINT when using AT and TAB commands. LPRINT buffers output and handles line breaks differently, especially with AT which ignores line numbers. ```basic 10 FOR n=31 TO 0 STEP-1 20 PRINT AT 31-n,n; CHR$ (CODE "0"+n); 30 NEXT n ``` ```basic 10 FOR n=31 TO 0 STEP-1 20 PRINT TAB n; CHR$ (CODE "0"+n); 30 NEXT n ``` ```basic 10 FOR n=31 TO 0 STEP-1 20 LPRINT AT 31-n,n; CHR$ (CODE "0"+n); 30 NEXT n ``` -------------------------------- ### ZX Spectrum BASIC Color and Brightness Demo Source: https://github.com/0x00cl/zx-spectrum-basic-programming/blob/main/chapters/chap16.md Demonstrates the eight available colors and two brightness levels on the ZX Spectrum. It iterates through color and brightness settings to display colored spaces and numbers, showcasing the `BRIGHT`, `PAPER`, and `INK` commands. ```basic 10 FOR m=0 TO 1: BRIGHT m 20 FOR n=1 TO 10 30 FOR c=0 TO 7 40 PAPER c: PRINT " ": REM 4 coloured spaces 50 NEXT c: NEXT n: NEXT m 60 FOR m=0 TO 1: BRIGHT m: PAPER 7 70 FOR c=0 TO 3 80 INK c: PRINT c;" " 90 NEXT c: PAPER 0 100 FOR c=4 T0 7 110 INK c: PRINT c;" " 120 NEXT c: NEXT m 130 PAPER 7: INK 0: BRIGHT 0 ```