Gameboy Development Forum

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.

Ads

#1 2008-04-06 16:47:50

Shiny
Member
Registered: 2008-03-17
Posts: 21

Sending and returning data in assembly?

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

 

#2 2008-04-06 18:12:42

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 245

Re: Sending and returning data in assembly?

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


Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

#3 2008-04-06 22:55:10

Shiny
Member
Registered: 2008-03-17
Posts: 21

Re: Sending and returning data in assembly?

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

 

#4 2008-04-07 07:34:30

Lai
New member
Registered: 2008-03-17
Posts: 5

Re: Sending and returning data in assembly?

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

 

#5 2008-04-07 20:29:23

Shiny
Member
Registered: 2008-03-17
Posts: 21

Re: Sending and returning data in assembly?

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

 

#6 2008-04-07 20:38:49

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 245

Re: Sending and returning data in assembly?

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.


Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson