views:

187

answers:

3

I wrote an answer yesterday to this: http://stackoverflow.com/questions/192479/whats-the-coolest-hack-youve-seen-or-done and I was trying really hard to remember my 6502 assembly, and I couldn't for the life of me remember how to branch if less than...

  :1
  lda $C010
  cmp #$80
  bcc :1  ; branch if less than? I forget how to do that.
  lda $C000
  jsr $FDF0   ;output the accumulator value to the screen

Anybody know what the instruction is? BNE and BEQ are equals, BCC was for carry, and a CMP is basically an SBC and that affects the carry, but I'm not sure if it works in that case.

+1  A: 

First Google hit: http://en.wikibooks.org/wiki/6502_Assembly#Branch

Hans Passant
cheater. :-) I was looking for some lively 6502 discussion from old schoolers like me with better memories. :-)
stu
+3  A: 

The cmp instruction simulates a subtraction. The carry bit is set if the result would have been negative. You need "bcs" if you want to branch if the value is less than $80; bcc in this case is greater than or equal.

Mark Ransom
Just to make it totally clear, BCC is Branch on Carry Clear; BCS is Branch on Carry Set. The carry is cleared if the subtraction would result in a value less than zero (treating both operands as unsigned).
Bob Montgomery
A: 

Mark's answer is wrong on 2 counts:

(1) The carry is cleared if the result would have been negative. (You SEC before SBC on the 6502.)
(2) BCC is branch if less than; BCS is branch if greater than. There's a nice tutorial here.

Additionally stu's code can be written more concisely without CMP:

BIT $C010     ;clear the keyboard strobe
:1
LDA $C000     ;check for a keypress
BPL :1        ;taken if no keypress
JSR $FDFO     ;print the key
Nick Westgate