It is currently Sat Nov 18, 2017 9:02 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Dec 31, 2016 8:16 am 
Offline

Joined: Wed Sep 09, 2015 6:00 pm
Posts: 19
I want to check if the number or not in an array how i can done?

Code:
ColumnMap:
    LDA ColumnData, x
    STA #$00, x
    INX
    BNE ColumnMapDone
ColumnMapDone:

ColumnData:
    .db $01,$02,$03,$04,$05,$06,$07,$08,$09,$10,$0A,$0B,$0C,$0D,$0E,$0F
    .db $02,$03,$04,$05,$06,$07,$08,$09,$10,$11,$1A,$1B,$1C,$1D,$1E,$1F


Top
 Profile  
 
PostPosted: Sat Dec 31, 2016 9:05 am 
Offline
User avatar

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 3192
Location: Mountain View, CA, USA
sta #$00,x makes no sense and will not assemble. You probably meant sta $00,x. Immediate addressing (#) is not the same thing as indexed addressing $blah,x.

Your loop is broken and the code doesn't even really make sense. :-)

First, you're saying inx / bne. In English this means: increment the X register (which affects the zero flag); next, if the zero flag IS NOT set (i.e. if the value in X is non-zero), then branch to ColumnMapDone (which is the label right after the bne). What you probably meant was bne ColumnMap, to keep the loop iterating until X had wrapped to zero -- but this really isn't what you want either, because you'll end up copying data past where ColumnData ends (there's only 32 bytes of data there).

Second, in the code you showed, you don't define what X contains at before ColumnMap is reached. It could be $20, it could be $00, who knows. So your "starting offset" (into the ColumnData buffer) is going to vary, as well as the starting location in ZP where the values are being copied to. But, funny enough, that doesn't really matter because you have your branch conditional inverted (IMO), or the label wrong (my guess is this) as I describe above.

I can't tell exactly what you're wanting to do with this routine. Are you wanting to copy 16 bytes into ZP $00-$0F? 32 bytes into ZP $00-1F? Do you want some kind of support for an offset into the ColumnData table (say, starting at byte 5 (value $05) and thus copy only 11 bytes)? If so, do you want that copied into $00-0B or $05-0F? All this matters!

As for a literal answer to your initial question: it's easy: you use a compare instruction (cmp for accumulator, cpx for X, cpy for Y) and then do your branch instruction after that. For data copying routines, you have two approaches: 1) you pre-calculate the length of the data you're copying and iterate in a loop that number of times, or b) you use a special byte/value that signifies "end of buffer" (think NULL string termination in C).

Edit: Sorry about the first example. My brain was off in some weird state where I was thinking about dealing with multiple loops (for >256 byte copies) and didn't write the routine even remotely correctly. The simple version:

Code:
  ldx #0
ColumnLoop:
  lda ColumnData,x
  sta $00,x
  inx
  cpx #32
  bne ColumnLoop

  ...

ColumnData:
    .db $01,$02,$03,$04,$05,$06,$07,$08,$09,$10,$0A,$0B,$0C,$0D,$0E,$0F
    .db $02,$03,$04,$05,$06,$07,$08,$09,$10,$11,$1A,$1B,$1C,$1D,$1E,$1F

For the latter, if you know ColumnData will never contain the value $FF, then you could simply compare for that using cmp #$ff like so. (This routine will break badly if you try to copy more than 256 bytes, but it should be obvious why):

Code:
  ldx #0
ColumnLoop:
  lda ColumnData,x
  cmp #$ff
  beq ColumnDone
  sta $00,x
  inx
  jmp ColumnLoop
ColumnDone:
  ...

ColumnData:
    .db $01,$02,$03,$04,$05,$06,$07,$08,$09,$10,$0A,$0B,$0C,$0D,$0E,$0F
    .db $02,$03,$04,$05,$06,$07,$08,$09,$10,$11,$1A,$1B,$1C,$1D,$1E,$1F
    .db $FF ; End of buffer


Last edited by koitsu on Sat Dec 31, 2016 10:32 am, edited 2 times in total.

Top
 Profile  
 
PostPosted: Sat Dec 31, 2016 10:10 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1823
Location: DIGDUG
koitsu, please check your example code again, the one that starts ldy #0, ldx #0.

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Sat Dec 31, 2016 10:29 am 
Offline
User avatar

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 3192
Location: Mountain View, CA, USA
Yup thanks -- edited with an explanation to boot.


Top
 Profile  
 
PostPosted: Sat Dec 31, 2016 1:16 pm 
Offline

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 93
Location: Southern California
Quote:
I want to check if the number or not in an array how i can done?

This sentence does not really make sense. It sounds like you want to see if a particular number is in the array; but your code looks like you just want to copy the array. To just copy it, assuming it's ok to go in the reverse order to save an instruction, you can do:

Code:
        LDX  #$1F
loop:   LDA  ColumnData,X
        STA  0,X
        DEX
        BPL  loop


But if you want to see if a particular number is in the array, do something like the following, starting with your comparison number in A:

Code:
        LDX  #$1F
loop:   CMP  ColumnData,X
        BEQ  end
        DEX
        BPL  loop
end:


If the Z flag is set at the end, you found a match. If you finished the loop without finding the number you were looking for, Z will be clear.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


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

All times are UTC - 7 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 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:  
Powered by phpBB® Forum Software © phpBB Group