Jump to content
IGNORED

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.

Link to comment
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
Link to comment
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!

Link to comment
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,,,

Link to comment
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...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...