很抱歉,如果问题似乎过于基本'。我是68K ASM编码器,但有一位朋友让我看一眼6502代码。 我们有一个指向数据字符串的指针:
my_ptr ds 2
使用以下代码设置此指针:
ldx sound_channel_busy
bne .abc_set_score1 ; at bottom of code
sta my_ptr ; fill the pointer
使用
读取数据lda (my_ptr),y ; my_ptr + offset
但正如我在6502 doc中看到的那样,y是一个字节。因此,使用超过255个字节的数据字符串是不可能的(我们希望读取10.000字节或更多的字符串)。 我建议我的朋友这样做:
1)将一个指针设置为" base"还有一个临时的,我们会在阅读时加入
my_ptr ds 2
my_ptr_tmp ds 2
2)用以下内容初始化它们:
ldx sound_channel_busy
bne .abc_set_score1
sta my_ptr
sta my_ptr_tmp ; Duplicate
3)然后阅读:
lda (my_ptr_tmp) ; Read to acumulator
inc my_ptr_tmp ; One more on adress pointer
但它不起作用,因为我的朋友是C开发者,我们没有调试器......不容易。 在68K这似乎是合乎逻辑的,但在6502?
非常感谢你的帮助
答案 0 :(得分:6)
6502非常有限。
只有两种间接模式:(zp),y
和(zp,x)
其中zp
是零页面地址。前者将地址计算为
contentOf(zp + (zp + 1) << 8) + y
并使用其中的字节作为操作数。后者将地址计算为
contentOf( zp + x + (zp + x + 1) << 8)
y表单用于访问由零页面指针指向的数组元素,x表单用于访问零页面内存中的向量表。
设置指针:
lda #<pointer ; The low byte of the 16 bit address pointer is loaded into A
sta my_ptr
lda #>pointer ; the high byte of the pointer
sta my_ptr+1
ldy #0 ; zero the y register
访问指针
loopStart:
lda (my_ptr),y
假设C样式字符串带有空终止符
beq loopExit ; previous LDA sets the S and Z flags.
增加指针
iny ; Increment y
bne loopStart
inc my_ptr+1
jmp loopStart
您也可以将Y保持为0并增加低字节和两个零页面位置,但是INC my_ptr
比INY
慢了五个周期而不是两个周期。
修改强>
如果你有一个长度,而不是一个空终止的字符串,你需要稍微修改它。一种方法是计算你已完成的字节数并与长度进行比较。利用上述算法,如果长度<1,则Y是计数。 256,所以我们可以做的是将计数的高字节存储在
中; first set up my_ptr, same as before.
;
lda #<pointer ; The low byte of the 16 bit address pointer is loaded into A
sta my_ptr
lda #>pointer ; the high byte of the pointer
sta my_ptr+1
;
; Set up the counter
;
ldx #0 ; set up x for the count
ldy #0 ; Set up y for the count/loop
;
; A common trick with compiling while loops is to put the test at the end of the loop and jump to it immediately.
; This means you don't have to reverse the logic of the loop condition.
;
jmp loopTest ; Omit this if you definitely need to go round the loop at least once
loopStart:
lda (my_ptr),y ; Get the byte
;
; Do what you need to do here
;
; Increment the counter
;
iny ; Increment y
bne loopTest
inx
inc my_ptr+1
loopTest:
cpy length ; Compare the low byte of length to the count
bne loopStart
cpx length+1 ; Compare the high byte of length to the count
bne loopStart
答案 1 :(得分:1)
6502是8位数据,16位地址,所以你的指针需要是2个字节,通常在第0页。
lda #<addr ;low byte
sta my_ptr
sta my_ptr_tmp
lda #>addr ;high byte
sta my_ptr+1
sta my_ptr_tmp+1
该公司也需要是16位:
inc my_ptr_tmp
bne :skip
inc my_ptr_tmp+1 ;only inc high byte if low byte is zero
:skip
另请注意,没有X或Y的lda(zp)仅适用于65C02。