Jump to content
Sign in to follow this  
kensu

Loop condition in 5200 asm example

Recommended Posts

There's an example of a simple 5200 hello world program here http://www.atarihq.com/danb/files/52hello.txt.

There's a bit that confused me, though. Specifically this loop:

 

Start
       ldx     #$00
       lda     #$00
crloop1    
       sta     $00,x           ;Clear zero page
       sta     $D400,x         ;Clear ANTIC
       sta     $C000,x         ;Clear GTIA
       sta     $E800,x         ;Clear POKEY
       dex
       bne     crloop1

 

The x register is being set to 0, and then decremented. o_O

Now I assume when you decrement a register when it's set at 0, it goes to $FF. But why would someone do it that way? Most of the other examples I see would use ldx #$FF.

This is actually valid code, I've assembled it and it does create a runnable 5200 rom.

Share this post


Link to post
Share on other sites

You are correct, after the first execution of DEX, X will contain $FF. The loop will then continue until DEX yields a 0, which will break the loop. If you loaded $FF to X initially it would only clear addresses $FF, $D4FF, $C0FF, and $E8FF because X would be 0 after the first iteration of the loop, and BNE would not branch. Since you want it to clear $00 - $FF, $D400 - $D4FF, $C000 - $C0FF, and $E800 - $E8FF, it must go through 256 iterations.

 

Because you want to go through 256 iterations, you could actually replace the dex with an inx in this case. Instead of starting at 0 and counting back down to 0 from $FF like the code you posted does, the inx would cause it to start at zero, count up to $FF, and then in the last iteration reset to 0 again.

 

Count down loops are usually used where possible to save a both CPU time and ROM. Here's an example to illustrate this:

 

 

With a count up loop:

 ldx #0
Loop

;code to be looped

 inx
 cpx #5
 bne Loop


 

With a count down loop:

 

 ldx #5
Loop

;code to be looped

 dex
 bne Loop

 

So in that case, counting down will save you 2 bytes of ROM and a total of 10 machine cycles by omitting the CPX instruction. Seems like a trivial difference, but these small optimizations could mean the world in some cases :)

Edited by Wickeycolumbus

Share this post


Link to post
Share on other sites

If you would use ldx #$ff, then you would not clear addresses $0, $c000, $d400 and $e800 because when X hits 0 (after dex instruction) the loop will end. So you would clear only addresses $xx01-$xxff.

Share this post


Link to post
Share on other sites

If you would use ldx #$ff, then you would not clear addresses $0, $c000, $d400 and $e800 because when X hits 0 (after dex instruction) the loop will end. So you would clear only addresses $xx01-$xxff.

 

Yes, you are right. The first part of my post is wrong.

Share this post


Link to post
Share on other sites

Of course, you're doing a lot of unneeded STAs in that loop as well, since there are no more than 16 valid registers to clear on the chips. I'm assuming that this is init code where code size is more important than execution speed.

Share this post


Link to post
Share on other sites

This part

 

 

ldx #$00

lda #$00

Could be replaced with this to save a byte:

 

ldx #$00

tax

Edited by sack-c0s

Share this post


Link to post
Share on other sites

This part

ldx #$00

tax

 

Could be replaced with this to save a byte:

anx #$00 ; code $AB stable with argument #$00

  • Like 1

Share this post


Link to post
Share on other sites

TIP #2 (and this is VERY important): *don't write ANYTHING before your first coffee of the day!*, because if you do you end up making the kind of mistakes I did just then.

 

I meant

LDA #$00

TAX

 

although

LDX #$00

TXA

 

would do the exact same thing. What I wrote will just stick 0 in X then copy A to X, which could be just about anything. D'oh!

Share this post


Link to post
Share on other sites

TIP #2 (and this is VERY important): *don't write ANYTHING before your first coffee of the day!*, because if you do you end up making the kind of mistakes I did just then.

 

I meant

LDA #$00

TAX

 

although

LDX #$00

TXA

 

would do the exact same thing. What I wrote will just stick 0 in X then copy A to X, which could be just about anything. D'oh!

 

Given the way that Windows ``works, I think many coffees may be needed in Redmond,,,

Share this post


Link to post
Share on other sites

Of course, you're doing a lot of unneeded STAs in that loop as well, since there are no more than 16 valid registers to clear on the chips...

 

32. CTIA/GTIA addresses go from xx00 to xx1F. ANTIC and POKEY have 16 addresses.

Share this post


Link to post
Share on other sites

32. CTIA/GTIA addresses go from xx00 to xx1F. ANTIC and POKEY have 16 addresses.

 

Where's the "Oops, that's what I meant to say. Now scurrying off to re-read Mapping the Atari" smiley?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...