在Commodore 64上使用汇编语言编写磁盘文件

8
我正在尝试学习如何使用内核例程编写磁盘文件,遵循这个Codebase64教程的步骤。
下面是我用Acme交叉汇编器编写的程序。它无法打开文件,并显示错误消息:"FILE NOT OPENED"。
; Definitions
SETNAM = $FFBD
SETFLS = $FFBA
OPEN   = $FFC0
CHKOUT = $FFC9
READST = $FFB7
CLOSE  = $FFC3
CLRCHN = $FFCC
CHROUT = $ffd2      

;Basic Start
    * = $0801                               ; BASIC start address (#2049)
    !byte $0d,$08,$dc,$07,$9e,$20,$34,$39   ; BASIC loader to start at     $c000...
    !byte $31,$35,$32,$00,$00,$00           ; puts BASIC line 2012 SYS 49152

;Program Code
    * = $c000                               ; Can be executed by writing sys 49152

    ldx #<message0         
    ldy #>message0   
    jsr printMessage    


save2file:      
    ; call SETNAM   
    lda #fname_end-fname    ; file name size
    ldx #<fname             ; file name vector
    ldy #>fname             ; file name vector
    jsr SETNAM              ; call SETNAM

    ; call SETFLS
    lda #$00
    ldx $BA                 ; last used device number
    bne +
        ldx #$08            ; default to device 8
+   ldy #$00
    jsr SETFLS              ; call SETLFS

    ;call OPEN
    jsr OPEN                ; call OPEN
    bcs .error1             ; if carry set, the file could not be opened

    ; call CHKOUT
    ldx #$02                ; filenumber=2
    jsr CHKOUT              ; file 2 now used as output

    ; Copy border color to the file
    jsr READST              ; call READST (read status byte)
    bne .error2             ; write error
    lda $d020               ; get byte from memory
    jsr CHROUT              ; write to file

    ldx #<message1         
    ldy #>message1     
    jsr printMessage

.close
    lda #$02      ; filenumber 2
    jsr CLOSE     ; call CLOSE
    jsr CLRCHN    ; call CLRCHN
    rts

.error1
    ldx #<errorMsg1         
    ldy #>errorMsg1   
    jsr printMessage
    jmp .close

.error2
    ldx #<errorMsg2         
    ldy #>errorMsg2   
    jsr printMessage    
    jmp .close        

fname:  !tx "DATA,S,W"
fname_end:

message0:   !by 141 : !scr"SAVING" : !by 0
message1:   !by 141 : !scr"COLORS SAVED" : !by 0
errorMsg1:  !by 141 : !scr"FILE NOT OPENED" : !by 0
errorMsg2:  !by 17 : !scr"WRITE ERROR" : !by 0

;==========================================================================
; printMessage
;   Prints null terminated string to the memory
;   Input: x,y adress vector of text string 
;==========================================================================
temp     = $fb          ;zero page pointer

printMessage:   
    stx temp            ;save string pointer LSB
    sty temp+1          ;save string pointer MSB
    ldy #0              ;starting string index

-   lda (temp),y        ;get a character
    beq +               ;end of string
        jsr CHROUT      ;print character
        iny             ;next
        bne -
    inc temp+1             
    bne -       
+ rts               

我使用C64程序员参考手册准备了下面列出的基本例程。它在同样的环境中按预期工作。

10 OPEN 3,8,3, "O:DATA FILE,S,W"
20 PRINT#3, "SENT TO DISK"
30 CLOSE 3      

为什么我的汇编程序无法正常工作?

我正在测试Vice 2.4 上的程序。


BASIC例程与汇编语言非常不同,但说一个语言有效而另一个无效并没有什么用!你调试过代码了吗? - t0mm13b
2
你的BASIC命令正在数据通道3(3,8,3)上打开文件3,但是你的ASM尝试访问数据通道0(ldy #$00)上的文件零(lda #$00),这是无效的,因为设备8(磁盘)的次要地址号码不是0。 - J...
1
@J...我已经更改了数字,现在它可以工作了。 - wizofwor
2个回答

6

显然问题出在逻辑号码和次要地址中,正如J所指示的那样...

我已通过更改部件来解决它,即:

    ; call SETFLS
    lda #$03
    ldx $BA                 ; last used device number
    bne +
        ldx #$08            ; default to device 8
+   ldy #$03
    jsr SETFLS              ; call SETLFS

...

    ; call CHKOUT
    ldx #$03                ; filenumber=3
    jsr CHKOUT             ; file 2 now used as output

...

.close
    lda #$03      ; filenumber 3
    jsr CLOSE     ; call CLOSE
    jsr CLRCHN    ; call CLRCHN
    rts

还有其他问题,比如“颜色已保存”消息被发送到文件而不是屏幕,但这些问题可以轻松解决。


0

我知道这是一个旧的线程,但只需从每个错误例程中调用jsr close然后rts就可以将错误打印到屏幕上,对吗?这样你就可以在输出文本之前关闭文件/等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接