kensu Posted April 22, 2013 Share Posted April 22, 2013 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. Quote Link to comment Share on other sites More sharing options...
Wickeycolumbus Posted April 22, 2013 Share Posted April 22, 2013 (edited) 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 April 22, 2013 by Wickeycolumbus Quote Link to comment Share on other sites More sharing options...
MaPa Posted April 22, 2013 Share Posted April 22, 2013 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. Quote Link to comment Share on other sites More sharing options...
Wickeycolumbus Posted April 22, 2013 Share Posted April 22, 2013 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. Quote Link to comment Share on other sites More sharing options...
+David_P Posted April 22, 2013 Share Posted April 22, 2013 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. Quote Link to comment Share on other sites More sharing options...
sack-c0s Posted April 23, 2013 Share Posted April 23, 2013 (edited) This part ldx #$00 lda #$00 Could be replaced with this to save a byte: ldx #$00 tax Edited April 23, 2013 by sack-c0s Quote Link to comment Share on other sites More sharing options...
xxl Posted April 23, 2013 Share Posted April 23, 2013 This part ldx #$00 tax Could be replaced with this to save a byte: anx #$00 ; code $AB stable with argument #$00 1 Quote Link to comment Share on other sites More sharing options...
sack-c0s Posted April 23, 2013 Share Posted April 23, 2013 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! Quote Link to comment Share on other sites More sharing options...
+David_P Posted April 24, 2013 Share Posted April 24, 2013 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,,, Quote Link to comment Share on other sites More sharing options...
kenjennings Posted April 25, 2013 Share Posted April 25, 2013 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. Quote Link to comment Share on other sites More sharing options...
+David_P Posted April 26, 2013 Share Posted April 26, 2013 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? Quote Link to comment Share on other sites More sharing options...
Wickeycolumbus Posted April 26, 2013 Share Posted April 26, 2013 Of course this is clearing the zero page too, which I'd assume they wanted completely cleared. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.