简体   繁体   English

将 memory 地址传递给 C64 汇编中的子程序?

[英]Passing memory address to subroutine in C64 Assembly?

param word 0

function
    ...
    lda param,x


scr1 byte "some text"
     byte 0

So how do I pass "scr1" to function as "param"?那么如何将“scr1”作为“param”传递给 function? I know it's a memory address, so it doesn't fit into 1 byte registers.我知道这是一个 memory 地址,所以它不适合 1 字节寄存器。 What's the best way to do this?最好的方法是什么?

EDIT:编辑:

Thanks for the answers, The solution posted below works great.感谢您的回答,下面发布的解决方案效果很好。 but this uses both X and Y registers?但这同时使用了 X 和 Y 寄存器? What if "function" calls KERNAL routines which rely on X or Y or my own code needs X for something etc?如果“函数”调用依赖于 X 或 Y 或我自己的代码需要 X 的 KERNAL 例程怎么办? In these case this won't work as expected I guess?在这些情况下,我猜这不会按预期工作?

I'm totally new to assembler, so I'm confused about many things.我是汇编程序的新手,所以我对很多事情感到困惑。

Basically I wanted to pass the address, because I wanted to avoid code duplication.基本上我想传递地址,因为我想避免代码重复。 I have a simple function which prints a string to the screen like so:我有一个简单的 function,它像这样在屏幕上打印一个字符串:

        ldx #$00
copy_text
        lda scr1,x
        beq done
        sta screen_start,x
        inx
        bne copy_text
done
        rts

But this works only with scr1.但这仅适用于 scr1。 If wanna print other stuff, I need to duplicate the code which seems to be wasteful.如果想打印其他东西,我需要复制看起来很浪费的代码。

Is that acceptable in assembler?这在汇编程序中可以接受吗? I mean, in C or any other language you would just write a reusable function or method.我的意思是,在 C 或任何其他语言中,您只需编写可重用的 function 或方法。 But in assembler this seems to be very hard to do, because there are only 3 registers and they are used by many things.但是在汇编器中这似乎很难做到,因为只有 3 个寄存器而且它们被很多东西使用。

What's the best way to overcome this limitation?克服此限制的最佳方法是什么?

There are a few ways to do this.有几种方法可以做到这一点。

zpb = $fb

function = *
   stx zpb+0  
   sty zpb+1  ; note after you've stored .X and .Y in zero page they can be re-used for other purposes.
   ...
   ldy #$00
   lda (zpb),y
   ...

caller = *
    ldx #<scr1
    ldy #>scr1
    jsr function

Alternatively you can play with the stack或者你可以玩堆栈

zpb = $fb

function = * 
   pla            ; save current stack pointers for later
   sta temp+0
   pla
   sta temp+1
   pla            ; pull low byte off first
   sta zpb+0
   pla            ; now pull off high byte
   sta zpb+1       
   lda temp+1     ; restore stack pointers so RTS works as expected
   pha
   lda temp+0
   pha
   ...
   ldy #$00
   lda (zpb),y
   ...
temp .byte 0,0

caller = *
    lda #>scr1   ; note we push high byte first since stack is FILO
    pha
    lda #<scr1
    pha
    jsr function

Machine language by its own on the 6502 only has the three registers, so your options are usually to pass values in those registers, or , use those registers to point to a larger set of data that you can then access elsewhere. 6502 上的机器语言本身只有三个寄存器,因此您的选择通常是在这些寄存器中传递值,或者使用这些寄存器指向一组更大的数据,然后您可以在其他地方访问这些数据。 The indirect zero page is your best friend in this regard since you can then store pointers in zero page and then access them indirectly.在这方面,间接零页是您最好的朋友,因为您可以在零页中存储指针,然后间接访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM