base:reading_a_file_byte-by-byte
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | base:reading_a_file_byte-by-byte [2015-04-17 04:33] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Reading from a file byte-by-byte ====== | ||
+ | BASIC code: | ||
+ | < | ||
+ | 10 LA=8192 | ||
+ | 20 OPEN 2, | ||
+ | 30 IF ST<>0 THEN GOTO 60 | ||
+ | 40 GET#2,A$:IF A$="" | ||
+ | 50 POKE LA, | ||
+ | 60 CLOSE 2 | ||
+ | </ | ||
+ | Assembler code: | ||
+ | < | ||
+ | load_address = $2000 ; just an example | ||
+ | LDA # | ||
+ | LDX #<fname | ||
+ | LDY #>fname | ||
+ | JSR $FFBD ; call SETNAM | ||
+ | |||
+ | LDA #$02 ; file number 2 | ||
+ | LDX $BA ; last used device number | ||
+ | BNE .skip | ||
+ | LDX #$08 ; default to device 8 | ||
+ | .skip LDY #$02 ; secondary address 2 | ||
+ | JSR $FFBA ; call SETLFS | ||
+ | |||
+ | JSR $FFC0 ; call OPEN | ||
+ | BCS .error | ||
+ | |||
+ | ; check drive error channel here to test for | ||
+ | ; FILE NOT FOUND error etc. | ||
+ | |||
+ | LDX #$02 ; filenumber 2 | ||
+ | JSR $FFC6 ; call CHKIN (file 2 now used as input) | ||
+ | |||
+ | LDA #< | ||
+ | STA $AE | ||
+ | LDA #> | ||
+ | STA $AF | ||
+ | |||
+ | LDY #$00 | ||
+ | .loop JSR $FFB7 ; call READST (read status byte) | ||
+ | BNE .eof ; either EOF or read error | ||
+ | JSR $FFCF ; call CHRIN (get a byte from file) | ||
+ | STA ($AE), | ||
+ | INC $AE | ||
+ | BNE .skip2 | ||
+ | INC $AF | ||
+ | .skip2 | ||
+ | |||
+ | .eof | ||
+ | AND #$40 ; end of file? | ||
+ | BEQ .readerror | ||
+ | .close | ||
+ | LDA #$02 ; filenumber 2 | ||
+ | JSR $FFC3 ; call CLOSE | ||
+ | |||
+ | JSR $FFCC ; call CLRCHN | ||
+ | RTS | ||
+ | .error | ||
+ | ; Akkumulator contains BASIC error code | ||
+ | |||
+ | ; most likely errors: | ||
+ | ; A = $05 (DEVICE NOT PRESENT) | ||
+ | |||
+ | ;... error handling for open errors ... | ||
+ | JMP .close | ||
+ | .readerror | ||
+ | ; for further information, | ||
+ | |||
+ | ;... error handling for read errors ... | ||
+ | JMP .close | ||
+ | |||
+ | fname: | ||
+ | !tx "JUST A FILENAME" | ||
+ | fname_end: | ||
+ | </ | ||
+ | |||
+ | You may open more than one file if you use different file numbers and secondary addresses for them. File numbers and secondary addresses should be in the range of 2 to 14. It's usually a good idea to use the same number for both to keep confusion low. |
base/reading_a_file_byte-by-byte.txt · Last modified: 2015-04-17 04:33 by 127.0.0.1