base:writing_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:writing_a_file_byte-by-byte [2015-04-17 04:34] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Writing to a file byte-by-byte ====== | ||
+ | BASIC code: | ||
+ | < | ||
+ | 10 FS=8192: | ||
+ | 20 OPEN 2, | ||
+ | 30 IF ST<>0 THEN GOTO 70 | ||
+ | 40 A=PEEK(FS): | ||
+ | 50 PRINT# | ||
+ | 60 IF FE>FS THEN GOTO 30 | ||
+ | 70 CLOSE 2 | ||
+ | </ | ||
+ | Assembler code: | ||
+ | < | ||
+ | file_start = $2000 ; example addresses | ||
+ | file_end | ||
+ | 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 EXISTS error etc. | ||
+ | |||
+ | LDX #$02 ; filenumber 2 | ||
+ | JSR $FFC9 ; call CHKOUT (file 2 now used as output) | ||
+ | |||
+ | LDA #< | ||
+ | STA $AE | ||
+ | LDA #> | ||
+ | STA $AF | ||
+ | |||
+ | LDY #$00 | ||
+ | .loop JSR $FFB7 ; call READST (read status byte) | ||
+ | BNE .werror | ||
+ | LDA ($AE), | ||
+ | JSR $FFD2 ; call CHROUT (write byte to file) | ||
+ | INC $AE | ||
+ | BNE .skip | ||
+ | INC $AF | ||
+ | .skip | ||
+ | LDA $AE | ||
+ | CMP #< | ||
+ | LDA $AF | ||
+ | SBC #> | ||
+ | BCC .loop ; next byte | ||
+ | .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 | ||
+ | .werror | ||
+ | ; for further information, | ||
+ | |||
+ | ... error handling for write errors ... | ||
+ | JMP .close | ||
+ | |||
+ | fname: | ||
+ | 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/writing_a_file_byte-by-byte.txt · Last modified: 2015-04-17 04:34 by 127.0.0.1