Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Using an index register as an assembly optimisation (Sonic 1)

Reply to topic
Author Message
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14690
  • Location: London
Reply with quote
Using an index register as an assembly optimisation (Sonic 1)
Post Posted: Tue Jan 18, 2022 11:36 am
Last edited by Maxim on Tue Jan 18, 2022 12:18 pm; edited 2 times in total
While working on the Sonic 1 editor, and reading Kroc's excellent (if incomplete) disassembly, there's a characteristic of the code that I haven't seen elsewhere: it uses the iy register to point at the main "game memory" instead of using absolute addresses.

For example, instead of this:
ld a,(SOME_STATE) ; SOME_STATE is a RAM address like $c401

it will do this:
; iy is always $c400
ld a,(iy+offset_of_SOME_STATE) ; offset_of_SOME_STATE is an offset from $c400 for the variable in question


This has certain advantages:

* You can load to/from memory to any of the registers, not just a, relieving register pressure
* You can use the bit opcodes (bit, set, res) on memory directly
* You can load values to memory directly, e.g.
ld (iy+n), $ff

* You can add/subtract/compare values from memory directly, e.g.
add a,(iy+n)


And some disadvantages:

* It's a bit slower in general, although this is offset by the reduced need to juggle registers
* iy is not useable for anything else
* The assembly format for determining the offset to iy is a bit ugly; WLA DX structs come in handy but it's still ugly
* You can only access 256 bytes this way

I suspect it might also be a little bit like using the zero page for variables on processors like the 6502, although I don't have much experience there.

What do you think? Are there other advantages/disadvantages I'm missing?
  View user's profile Send private message Visit poster's website
  • Joined: 05 Sep 2013
  • Posts: 3763
  • Location: Stockholm, Sweden
Reply with quote
Post Posted: Tue Jan 18, 2022 11:40 am
it's interesting indeed... I would add the disadvantage that you can't address more than 256 different bytes so your 'game memory' can't be bigger than that
  View user's profile Send private message Visit poster's website
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14690
  • Location: London
Reply with quote
Post Posted: Tue Jan 18, 2022 12:17 pm
That's certainly true :) although practically that is unlikely to matter much - any "arrays" or "objects" are likely to be managed some other way. This is more about the set of "globals" in Sonic.
  View user's profile Send private message Visit poster's website
  • Joined: 23 Aug 2009
  • Posts: 213
  • Location: Seattle, WA
Reply with quote
Post Posted: Tue Jan 18, 2022 4:45 pm
I don't think the syntax is all that ugly. It's just (iy + StructName.FieldName). Yeah it gets a bit verbose but it's explicit. You could probably shorten it with some macros.

It's like using the index register as a this pointer.
  View user's profile Send private message
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14690
  • Location: London
Reply with quote
Post Posted: Tue Jan 18, 2022 8:40 pm
Certainly it makes sense to use the index registers for structured data, there’s just usually more than one instance of a structure.
  View user's profile Send private message Visit poster's website
  • Joined: 23 Aug 2009
  • Posts: 213
  • Location: Seattle, WA
Reply with quote
Post Posted: Tue Jan 18, 2022 9:30 pm
As in there are arrays of structures, or you have memory being re-used and can't always anticipate which structure is at a given location?
  View user's profile Send private message
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14690
  • Location: London
Reply with quote
Post Posted: Tue Jan 18, 2022 10:11 pm
More the former - where the index acts more like an object reference and the code acts relative to a changeable base.
  View user's profile Send private message Visit poster's website
Reply to topic



Back to the top of this page

Back to SMS Power!