nesdev.com
http://forums.nesdev.com/

Any way to avoid getting stuck in code?
http://forums.nesdev.com/viewtopic.php?f=2&t=15730
Page 2 of 2

Author:  nicklausw [ Mon Apr 24, 2017 6:39 pm ]
Post subject:  Re: Any way to avoid getting stuck in code?

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:
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.

Page 2 of 2 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/