Jump to content
Sign in to follow this  
Heaven/TQA

coding questions re: loops

Recommended Posts

stupid question but i need a solution for this...

 

normally i would do a loop like this:

 

ldx #100

loop lda tab,x

sta destination,x

dex

bpl loop

 

but what if x>=128?

 

f.e. i need above routine for 160 bytes... my question seems little bit stupid but i want to avoid a inx, cpx... maybe bvs etc?

 

any ideas?

Share this post


Link to post
Share on other sites
oh... i forgot to mention... of course it should perform the x=0 as well...

 

Well, this is ugly, but I think it will work...

   ldx #100

Loop 

  lda tab,X

  sta destination,X

  txa

  beq Done

  dex

  bne Loop

  beq Loop

Done 

Heh.

Share this post


Link to post
Share on other sites

I'd say just use BNE and add an extra STA destination,x after the loop. Only uses 3 extra bytes and no extra cycles.

 

Although I always just use BNE and STA destination-1,x. I can't think of many situations where that won't work except maybe where you have some tricky page alignment issues.

 

-paul

Share this post


Link to post
Share on other sites

correction, LDA/STA after the loop. 6 extra bytes (or less if zero page) Still not a significant cost unless you have a ton of these (which I'm guessing is not the case).

Share this post


Link to post
Share on other sites

thanks guys...

 

thomas... i came up with one of yours suggestions in meantime... ;) sometimes small things seems more complicated than they are...

 

thanks...

 

(btw.... it's for boinxx and it's screenwrapper scroll routine...)

Share this post


Link to post
Share on other sites

Here's an extra ugly variant, if you want to avoid offset-1 :

 

      lda #159; will be 160 bytes including index #0

loop: tax

     lda src,x

     sta dest,x

     txa

     sec

     sbc #$01

     bcs loop

done:

 

No, I'm not really serious that this is a good alternative. The CPX solution is two bytes shorter (and the offset-1 BNE is four bytes shorter). And that is without counting all the cycles wasted in each turn of the loop.

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.

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