Okay, so I'm working my way through
C64 Machine Language for Beginners by Danny Davis, Melbourne House (1984) as a 6502 primer. On the whole I really like the book. As a rule it's not moving too fast for and it pretty darn through about explaining itself the vast majority of the time.
I'm working through this text in parallel with my efforts to dissassemble a
C64 Cartridge and convert it to working fully commented code. Doing these two things at the same time has been very helpful. As I'm disassembling entirely by hand (no automatic disassembly) I'm only about 10% done. My progress in the book has been quite solid, I'm about 60% done. The only "hiccup" up to this point was 2's compliment arithmetic when performing branching instructions. From what I understand, that confuses everyone the first time around.
Unfortunately, I've hit one of those places where the sample program isn't fully intelligible and the book's explanation... isn't what it could be and I'm just not 100% following. Googling around hasn't helped much. I did check my favorite
6502 Instruction Set Reference site before posting.
In summary: the point of the program is to look for the value
$a9 in memory and (exit?) when it encounters the 4th occurrence. I think it just exits if it wraps around at
$ff. Here is the code, with my perhaps questionable comments after the semi-colon. This is on page 59:
Code:
ldx #$00 ; X is a counter, it increments upon encountering $a9
ldy #$00 ; Y is an index into memory a memory location.
lda #$a9 ; A has the value to be compared.
L40 cmp $f000,y ; Compare somewhere in RAM mapped to KERNAL ROM? Y indexed memory.
beq L90 ; Why am I checking Z and jumping forward? Is this spaghetti code?
L60 iny ; Y++ and Y indexed memory is offset by the new Y value.
bne L40 ; Z!=0? What's setting Z? Is this effectively a greater than statement? If so, why?
stx $0334 ; X is stored here -- as incremented when $a9 is encountered.
rts ; Exit to BASIC?
L90 inx ; X++
cpx #$04 ; X != 4?
bne L60 ; If not, branch to L60 and continue.
stx $0334 ; X is stored here, the same as 5 lines up. Why is this here twice?
rts ; Exit to BASIC?
Any help more completely grasping what I'm seeing would be helpful. I don't want to move on to chapter 8 until I get this.