6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Wed Nov 13, 2024 6:58 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Jan 13, 2003 9:17 am 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
I was perusing the various news items, and found a nice tutorial on CMP, CPX, and CPY. I also found a nasty bug. The code as presented, in the very last tutorial, is as follows:

Code:
        LDX  #00     ;Index = 0
NXTBYT  LDA  $20,X   ;Load next byte
        STA  $0320,X ;Store next byte
        INX          ;Increment index
        CPX  $1F     ;All bytes moved?
        BNE  NXTBYT  ;If not, move next byte


Oh, it'll make good on the tutorial's promise of moving up to 256 bytes of data. However, it'll move it from zero page on the 6502, and part of direct page and into the stack on the 65816. This code, therefore, is not portable, and is quite likely not what the author intended.

I think the intention of the code, as I understand it, is best expressed with the following code:

Code:
        LDY  #00     ;Index = 0
NXTBYT  LDA  ($20),Y ;Load next byte
        STA  $0320,Y ;Store next byte
        INY          ;Increment index
        CPY  $1F     ;All bytes moved?
        BNE  NXTBYT  ;If not, move next byte


This way, we set up a pointer to the data to be moved at location $20, so that if the user really does want to move from zero page and part of the stack space, then he can do so. Besides, it gives the Y register some of the limelight that X always seems to get. :)

On the other hand, it's also 1:18 AM for me here, so maybe I'm just getting too sleepy...


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jan 16, 2003 1:05 pm 
Offline

Joined: Thu Jan 16, 2003 12:55 pm
Posts: 64
Location: Indianapolis
I haven't seen the tutorial you mentioned, but that code could be improved. This is smaller and faster, but it is limited to addressing 128 bytes.

Code:
          LDX $1F
NXTBYT:   LDA $20,X
          STA $0320,X
          DEX
          BPL NXTBYT


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jan 16, 2003 6:17 pm 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
I actually very rarely use that style of indexing. I usually load the 2's compliment into the index register and increment, like this:

Code:
    lda #multiplicand1
    sta m1
    stz m1+1
    lda #multiplicand2
    sta m2+1   ; This is not a typo.
    stz m2
    ldx #-8
.multiply8x8bit
    jsr mulStep
    inx
    bne .multiply8x8bit


Your method works, of course. I've seen it used many times, but most of my applications for this type of thing is in moving overlapping regions of memory, where X *must* increase, not decrease, to prevent overwriting uncopied memory. That's where I first came up with the 2's compliment technique above, and it's been more or less a habit ever since. :)

Code:
    ; To FILL memory, SrcAddr < DstAddr.  To MOVE memory, the opposite.

    ldx #-value
    lda SrcAddr+value,x
    sta DstAddr+value,x
    inx
    bne *-8


Top
 Profile  
Reply with quote  
 Post subject: Re: CMP Tutorial
PostPosted: Fri Jan 24, 2003 11:48 pm 
Offline
Site Admin
User avatar

Joined: Fri Aug 30, 2002 1:08 am
Posts: 281
Location: Northern California
Hi Guys,

Thanks for your input (and code). I will update the CMP tutorial during the next major update of 6502.org.

Best Regards,
Mike

_________________
- Mike Naberezny (mike@naberezny.com) http://6502.org


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 24 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: