Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
I've been using RGBDS for a few days now and have really liked it because of the excellent macro language. I've managed to get sprites on screen and reacting to input, a tiled backdrop and HUD, and plan to make a (very) simple game to test out my newly aquired skills. But I digress... this is my first time writting in assembly and I've been struggling with doing a few things that seem so simple in higher level languages such as C.
My question is that of data flow between subroutines: What is the preferred way to handle input and output between pieces of code?
What I've been doing is simply leaving the data in certain registers, but this seems very primitive and limits me to only a few bytes of information. Then I had the thought to allocate a certain area of memory where all subroutine input and output would be stored but was turned off by the fact that this method can't be used for nested subroutines because later calls might mess up data from previous calls. I'm really not sure what else to say, but I guess I've just been spoiled by C's easy to use 'return' keyword (;
Offline
Here are a couple of ways of passing data:
* Storing data directly in registers. (like you do now)
* Pass data as pointers. BC, DE and HL are perfect for this. Useful for memory operations like clear, copy and text printing routines.
* Push the data to the stack and get it with LD HL,SP+nn. The problem with that I don't know what the syntax for that opcode in RGBDS. :p
Offline
nitro2k01 wrote:
* Storing data directly in registers. (like you do now)
* Pass data as pointers. BC, DE and HL are perfect for this. Useful for memory operations like clear, copy and text printing routines.
Aren't both of those essentially the same? ;P They're both just sending and retrieving data by leaving it in the registers.
nitro2k01 wrote:
* Push the data to the stack and get it with LD HL,SP+nn. The problem with that I don't know what the syntax for that opcode in RGBDS. :p
Doing that is as simple as your average pop rr, unless you're referring to something else. The biggest problem with this for me will be remembering to always get rid of any data that I don't use in the case that I don't actually need the returned data. I could problably create a few macros to simplify things though.
Offline
nitro2k01 wrote:
Here are a couple of ways of passing data:
* Push the data to the stack and get it with LD HL,SP+nn. The problem with that I don't know what the syntax for that opcode in RGBDS. :p
The syntax for that is LD HL,[SP+nn]. A bit unintuitive, yes.
Offline
Lai wrote:
The syntax for that is LD HL,[SP+nn]. A bit unintuitive, yes.
Thank you that confirmation (;
I think that I understand the difference now. Using ld hl, [sp+nn] I could directly copy and retrieve data in the stack at any size that I want because the increment for the stack pointer is variable. Using push and pop I'd be forced to use words to represent larger chunks of data, which would waste cycles from pushing and popping repeatedly.
Last edited by Shiny (2008-04-07 20:29:57)
Offline
Almost. You should use PUSH for pushing function arguments to the stack, and read them with LD HL,[SP+nn]; LD A,[HL+] The reason for this is that you don't have to POP your way through the return address to get the data you want, but you can read it directly. Once you have an initial address in HL, use HL+ type load to read data progressively. LD A,[HL+] has good cycle effiency.
Offline