首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取内存和inc (6502)

读取内存和inc (6502)
EN

Stack Overflow用户
提问于 2017-10-11 02:43:35
回答 2查看 1.2K关注 0票数 1

如果这个问题看起来“太基本”,我很抱歉。我是一个68K的ASM编码器,但是一个朋友让我看一眼6502的代码。我们有一个指向数据字符串的指针:

代码语言:javascript
复制
 my_ptr ds 2

这个指针是用以下代码设置的:

代码语言:javascript
复制
ldx sound_channel_busy
bne .abc_set_score1   ; at bottom of code
sta my_ptr   ; fill the pointer

读取数据的操作已完成

代码语言:javascript
复制
lda (my_ptr),y    ; my_ptr + offset

但正如我在6502文档中看到的,y是一个字节。因此,使用超过255字节的数据字符串是不可能的(我们希望读取10.000字节或更多的字符串。我建议我的朋友这样做:

1)将一个指针设置为"base“,并设置一个临时指针,我们将在读取时将其合并

代码语言:javascript
复制
 my_ptr ds 2
 my_ptr_tmp ds 2

2)初始化:

代码语言:javascript
复制
ldx sound_channel_busy
bne .abc_set_score1
sta my_ptr
sta my_ptr_tmp  ; Duplicate

3)然后使用:

代码语言:javascript
复制
  lda (my_ptr_tmp)   ; Read to acumulator
  inc my_ptr_tmp     ; One more on adress pointer

但是它不工作,因为我的朋友是一个C开发人员,我们没有调试器……这可不容易。在68K中,这似乎是合理的,但在6502中呢?

非常感谢你的帮助

EN

回答 2

Stack Overflow用户

发布于 2017-10-11 20:52:57

6502是相当有限的。

  • 所有对内存的读取和写入都是8位
  • 所有算术都是8位
  • 只有两种间接模式:(zp),y(zp,x),其中zp是一个零页地址。前者将地址计算为

contentOf(zp + (zp + 1) << 8) +y

并使用其中的字节作为操作数。后者将地址计算为

contentOf ( zp +x+( zp +x+ 1) << 8) (zp+x+(zp+x+1)zp+x+1)

Y形式用于访问零页指针所指向的数组的元素,x形式用于访问零页存储器中的矢量表。

要设置指针,请执行以下操作:

代码语言:javascript
复制
    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

访问指针

代码语言:javascript
复制
loopStart:
    lda (my_ptr),y

假设C样式的字符串带有空终止符

代码语言:javascript
复制
    beq loopExit  ; previous LDA sets the S and Z flags.

递增指针

代码语言:javascript
复制
    iny           ; Increment y
    bne loopStart
    inc my_ptr+1
    jmp loopStart

您也可以保持Y为0并递增低字节和两个零页位置,但是INC my_ptrINY慢得多,需要5个周期而不是2个周期。

编辑

如果你有一个长度,而不是一个以null结尾的字符串,你需要稍微修改一下。一种方法是计算你做了多少字节,并与长度进行比较。对于上面的算法,Y是计数,如果长度< 256,那么我们可以做的是将计数的高字节存储在

代码语言:javascript
复制
; 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
票数 6
EN

Stack Overflow用户

发布于 2017-10-11 08:52:08

6502是8位数据,16位地址,所以你的指针需要是2个字节,通常在第0页。

代码语言:javascript
复制
lda #<addr ;low byte
sta my_ptr
sta my_ptr_tmp
lda #>addr ;high byte
sta my_ptr+1
sta my_ptr_tmp+1

Inc.还需要为16位:

代码语言:javascript
复制
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上可用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46673778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档