You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Zero Page Indirect Addressing的6502内存循环程序无法正常运行,请求故障排查

Fixing Your 6502 Memory Fill Program

Let's walk through the issues in your code and get it working correctly:

Key Issues in Your Original Code

1. Unclear Carry Flag During 16-Bit Addition

This is the critical bug breaking your program. When you perform the 16-bit increment of your zero-page pointer:

LDA $00 
ADC #1 
STA $00 
LDA $01 
ADC #0 
STA $01 

you only clear the carry flag once at the start of the program. If the low byte ($00) ever rolls over from $FF to $00, the ADC #1 will set the carry flag to 1. This carry stays set for the next iteration of the loop, so the next ADC #1 will add 1 plus the carry to $00, making your pointer jump two bytes instead of one. Over time, this causes your writes to skip addresses and fill memory incorrectly.

2. Overcomplicated Pointer Management

You’re manually incrementing a 16-bit pointer when you don’t need to. Since you’re filling a contiguous 128-byte block, you can use the Y register as an index—its 8-bit range (0-127) perfectly fits your needs. This simplifies the code and eliminates the risk of 16-bit addition errors.

3. Missing BASIC Loader (For Commodore 64)

If you’re running this on a Commodore 64 (the most common system for $8001-originated code), starting at *=$8001 without a BASIC header will cause the interpreter to treat your machine code as invalid BASIC tokens. You need a small header to let BASIC call your program.

Fixed Code (Simplified Version)

This version uses the Y register for indexing, which is cleaner and more reliable:

*=$8001
; BASIC loader: 10 SYS 2064 (2064 = $810)
.byte $0C,$08,$0A,$00,$9E,$20,$32,$30,$36,$34,$00,$00,$00

*=$810 ; Actual program start
; Initialize zero-page pointer to $0400
LDA #$00
STA $00
LDA #$04
STA $01

CLD ; Disable decimal mode (critical for binary arithmetic)

LDX #128 ; Loop counter: 128 iterations
LDY #0   ; Index starts at 0
loop:
LDA #$01 ; Value to store
STA ($00),Y ; Store at pointer + Y index

INY ; Increment index for next address

DEX ; Decrement loop counter
BNE loop ; Repeat until counter hits 0

RTS ; Return to caller

Fixed Code (Original Pointer Approach)

If you prefer to keep the manual 16-bit pointer increment, just add a CLC before each addition to clear the carry flag:

*=$8001
; BASIC loader: 10 SYS 2064
.byte $0C,$08,$0A,$00,$9E,$20,$32,$30,$36,$34,$00,$00,$00

*=$810
LDA #$00
STA $00
LDA #$04
STA $01

CLD ; Disable decimal mode

LDX #128
LDY #0
loop:
LDA #$01
STA ($00),Y

; Fix: Clear carry before 16-bit addition
CLC
LDA $00
ADC #1
STA $00
LDA $01
ADC #0
STA $01

DEX
BNE loop

RTS

How to Test

If you’re using a C64 emulator like VICE:

  1. Load the code into memory
  2. Type RUN (or SYS 2064) to execute the program
  3. Check memory from $0400 to $047F—you’ll see all bytes set to $01

内容的提问来源于stack exchange,提问作者M. Gianota

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 22:57:34