Any way to avoid getting stuck in code?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
nicklausw
Posts: 376
Joined: Sat Jan 03, 2015 5:58 pm
Location: ...
Contact:

Re: Any way to avoid getting stuck in code?

Post by nicklausw »

psycopathicteen wrote:That's either Billy from Power Rangers finding where the Zeo Crystals are, or Glen Beck finding the Illuminati headquarters.
Bit much hair for Billy iirc (even in Zeo).

My two pieces of advice for neat code mostly apply to assembly programs: don't be afraid to comment plentifully, and if you ever find yourself wanting to use multiple branches of anonymous labels (like --,+++,) don't. Just don't. Why? Because I have no idea what this routine that I wrote does.

Code: Select all

calcrange:


    ld c,a
    ld a,b
    sbc a,13
    jr c,++
    ld b,a
    ld a,c


    cp b
    jr z,++
    jr nc,+
    jp calcno

    +: ld c,a
    ld a,b
    adc a,35
    jr c,++
    ld b,a
    ld a,c
    cp b
    jr z,++
    jr c,++
    jp calcno
    ++: ld a,2
    ld [calc],a
    ret


calcno:
    ld a,1
    ld [calc],a
    ret
    djnz -

    ++: dec hl
    ld [board_loc],hl
    ret
By that I don't mean that I can't figure out that the code calculates the range of something. I mean that what it's calculating the range of is unknown, and how it's doing so is even more unknown. What the anonymous labels point to is pretty much completely unknown. See the redundant wording? Yeah, everything is unknown.

Of course, that little snippet's outdated, and I have added tons of comments since, so sweet, problem solved. It probably took me an hour to add them, though, because by the time I got around to it, I really couldn't remember what the code was doing or how. Not a fun situation.

Oh, and something I didn't care about back when I wrote that: don't put whitespace before label names, at least not as much as you would for opcodes. It's ugly.
Post Reply