Programmare in BASIC Andrea Mantoni Ultima modifica: %%mtime(%d %B %Y) = Introduzione = Il BASIC (= Beginner's All-purpose Symbolic Instruction Code) è una famiglia di linguaggi: - con sintassi semplice, per principianti - general-purpose - high-level - unstructured - interpreted or compiled ... Utilizzi: - come OS per PC (diffussissimo negli anni '80) - per scopi didattici, insieme a Pascal (molto usato fino alla diffusione di Python, fine 200x) Esistono 2 standard ANSI di riferimento per il BASIC: - Minimal BASIC X3.60-1978 (a 1978 standard for minimal features) - X3.113-1987 (the full BASIC standard, rarely implemented fully) In questi appunti faremo riferimento solo a questi standard, in appendice si possono trovare le caratteristiche dei vari dialetti. = Sintassi = commenti: REM commento istruzioni: [LINE NUMBER] COMMAND ARG OSS.: "LINE NUMBER" può essere omesso dichiarazione variabili: (non necessaria) inizializzazione variabili: ANUMBER% = 10 ASTRING$ = "Hello World" == Operatori == + somma - differenza * moltiplicazione / divisione ((, )) parentesi quadre [, ] (((, ))) parentesi graffe {, } ; concatenamento stringhe == Controllo di flusso == GOTO 200 " salta alla linea 200 GOSUB 200 " salta alla subroutine alla linea 200 (salva l'indirizzo corrente per il RETURN) CALL GuessWords(s$) ' invoca la funzione "GuessWords" con il parametro "s$" END " fine programma SLEEP " makes the program wait for the user to press a key === Strutture di controllo === IF...THEN...ELSE: IF a > b THEN b = b*2 sum = sum + a ELSE a = a+1 sum = sum + b END IF SELECT...CASE: SELECT CASE value CASE 1 CALL do_something CASE 2,3,4 CALL do_otherthing(value) CASE IS > 4 counter = counter+1 value = value/2 CASE ELSE PRINT "Incorrect value" END SELECT DO...LOOP: DO PRINT question$ INPUT answer$ IF answer = "quit" THEN EXIT DO CALL check_answer(answer$) LOOP WHILE time_left FOR...NEXT: FOR row = 1 TO maxrows FOR column = 1 TO maxcols STEP 8 PRINT TAB(column);"*"; NEXT column PRINT NEXT row === SUBs and FUNCTIONs === FUNCTION returns a value, SUB does not. Both of them can be either internal or external. In internal procedure, all the variables are global within the program unit. In external procedure, all variables are local. esempi: SUB check_answer(answer$) IF answer$ = correct$ THEN PRINT "Correct!" score = score+1 CALL NextQuestion ELSE PRINT "Wrong, try again." ENDIF END SUB = I/O = CLS " clear the scree PRINT "Hello world" ' stampa sullo schermo PRINT VAR PRINT 5+6 INPUT NAME$ ' legge l'input dell'utente nella variabile stringa "NAME$" PRINT "Hello, "; NAME$ ;"! I am your computer, nice to meet you." == Strutture di controllo == = Appendice: dialetti = Nella sua lunga storia, il BASIC è stato arricchito di molti comandi e funzionalità che ne hanno prodotto vari "dialetti", alcuni molto popolari: - Dartmouth BASIC (1964) (compiler only) (prima implementazione del linguaggio, di pubblico dominio) - Microsoft BASIC (1975) (interpreter only) (licenziato a vari produttori di PC come OS di base) - Altair BASIC (1975) - Commodore BASIC (1977) (a.k.a. PET BASIC) (bundled with Commodore home computers) - Applesoft BASIC (1977) (superseded Integer BASIC) - Color BASIC (1980) (included in the ROM of the Tandy/Radio Shack TRS-80 Color Computers) - N88-BASIC (1981) (bundled with NEC PC8801/9801) (Japan's most popular BASIC based on Microsoft's one) - IBM BASIC (1981) (bundled with IBM PC DOS) (support for the graphics and sound hardware of the IBM PC line) - Cassette BASIC - IBM Disk BASIC (BASIC.COM) - IBM BASICA = BASIC Advanced (BASICA.COM) - GW-BASIC (1983) (bundled with MS-DOS <5.0, BASICA-compatible) - QuickBASIC (1985) (interpreter+compiler for professional use) - QBASIC (1991) (stripped version of QuickBASIC, interpreter only) (bundled with MS-DOS >=5.0) - FreeBASIC (2004) (open source interpreter+compiler, QuickBASIC-compatible) - Oric Extended Basic (1983) (shipped with Oric 8-bit family computers) - MSX BASIC (1984) (support for the hardware of the MSX home computers) (bundled with MSX computers) - Integer BASIC (1977) (interpreter-only) (written by Steve Wozniak) (included in ROM on the original Apple II computer at release, later replaced with Applesoft BASIC by Microsoft) - TRS-80 Level I BASIC (1977) (based on Tiny BASIC (PD)) (shipped with early Tandy Corporation's desktop microcomputer model line) - Sinclair BASIC (1979) (shipped with Sinclair 8-bit home computers: ZX80, ZX81, ZX Spectrum) - Atari BASIC (1979) (interpreter-only) (developed by Shepardson Microsystems) (shipped with Atari 8-bit family computers) - BBC BASIC (1981) (interpreter-only) (shipped with early BBC Micros) ... == QuickBASIC == == Commodore BASIC == sources: http://www.geocities.com/musuruan/emufaqen.html http://mess.toseciso.org/sysinfo:c64 http://www.pagetable.com/?p=273 === Cassettes Tapes handling (.wav, .prg, .tap) === Once you're at the BASIC prompt you can load a tape typing: LOAD or LOAD"" or LOAD"",1 You can use LOAD"",1,1 to load programs which requires a special address. === Floppy Disks handling (.d64, .nib) === Once you're at the BASIC prompt you have various options: * to list the content of the floppy: LOAD"$",8 LIST * to load a BASIC program from the disk: LOAD"filename",8 * to load a machine language program at its address: LOAD"filename",8,1 * to load the first program from the disk (useful if you're not sure of which is the correct one to load): LOAD"*",8 Once the program is loaded, a “READY” message will be prompted and you can run your program by simply typing: RUN or the appropriate SYS call. === Cartridges (.prg, .crt, .80, .90, .a0, .b0, .e0, .f0) handling === They start up automatically :) == MSX BASIC == sources: Sony A Guide To MSX-BASIC Version 2.0 http://www.faq.msxnet.org http://mess.toseciso.org/sysinfo:msx An MSX can execute: - MSX BASIC source code files (.bas|ldr) - intermediate language code files (.asc) - compiled? binary files (.???) === Cassettes Tapes (.cas, .tap, .wav, .tzx) === for MSX-BASIC files saved with CSAVE: CLOAD RUN For MSX-BASIC files saved with SAVE: RUN"CAS:" For MSX-BASIC files saved with SAVE (alternative): LOAD"CAS:",R For binary files saved with BSAVE: BLOAD"CAS:",R === Floppy Disks (.dsk, .msx, .ddi, .td0, .cp2, .img) === list the files on a floppydisk: FILES for BASIC and intermediate language code files: RUN"A:FILENAME.BAS|ASC|LDR" for binary files: BLOAD"A:FILENAME.BIN",R NOTE: "A:" is the 1st floppy disk drive, "B:" is the 2nd, etc. NOTE: if omitted, "A:" is assumed === Cartridges (.rom, .mx1, .mx2) === Insert the cartridge when the system is off, then turn it on (it will autostart). == N88-BASIC == sources: http://www.atarimagazines.com/creative/v9n11/28_NEC_PC8800_personal_comp.php ... === Floppy Disks (.d88) === ... == Sinclair BASIC == Il Sinclair BASIC era fornito nella ROM dei computer della Sinclair Research Ltd (UK): ZX80, ZX81 e ZX Spectrum. BASIC commands were not entered by typing them out but were instead selected somewhat similarly to a scientific calculator - each key had a few different functions selected by both context and modes as well as with the shift key. = Fonti = - http://en.wikibooks.org/wiki/Programming:BASIC - http://en.wikipedia.org/wiki/User:PauliKL/ANSI_BASIC