Re: [stella] 6502 question

Subject: Re: [stella] 6502 question
From: Nick S Bensema <nickb@xxxxxxxxxxxx>
Date: Fri, 4 Apr 1997 14:07:04 -0700 (MST)
>
>>It's surprising how often in 6502 programming, it is better to keep data in
>>memory than in registers.  That is good since there are so few registers.
>
>Yeah, but it's completely opposite from 80x86 programming, where you've got
>at least six 16-bit registers, four of which you can access the high and
>low bytes separately, and more if you know what you're doing (up to seven
>32-bit registers, a couple 16-bit regs, and some floating-point storage on
>the 386+), and the #1 rule is keep everything in the registers.  Compare
>this to three 8-bit registers plus the stack register if you know how to
>use it.
>
>I miss having MUL and DIV instructions =)  Also, exactly which instructions
>set and clear the carry?  I know the shifts and rotates do, and ADC is
>obvious, but what exactly do SBC and the compares do to the carry?

The general rule is SBC does the exact same thing as ADC but the operand
is exclusive-or'ed with $FF first.  That is why you always see people SET
the carry bit before subtracting: suppose for some dumb reason you wanted
to subtract 0 from 0.  SEC, SBC #0 is the same as SEC, ADC #$FF, and since 
the carry bit is set, $00 + $FF + 1 == $100.  The accumulator is still zero,
but the carry bit is still set because $100 won't fit in eight bits.

Trying this again, but this time subtracting 1 from 0.  SEC, SBC #1 is the
same as SEC, ADC #$FE, so $00 + $FE + 1 = $FF, which in twos-complement
is -1.  But $FF is an eight-bit number, so now the carry bit is actually
clear!  So if you subtract a value that is greater than the accumulator,
the carry bit is CLEAR.

Now let's try 3-2 just for kicks.  change the SBC #2 to ADC #$FD, and
$03 + $FD + 1 = $101.  The accumulator contains 1, which is the answer,
and the carry bit is set.  So if you subtract a value that is less than
or equal to the accumulator, the carry bit is SET.

CMP works like SEC+SBC, without changing any registers other than P.
Therefore, if everything I said above isn't a total load of bull...

      LDA #5
      CMP X
      BCC X_IS_MORE_THAN_FIVE
      BEQ X_IS_EQUAL_TO_FIVE
      BCS X_IS_LESS_THAN_OR_EQUAL_TO_FIVE.

Sometimes you will see BPL or BMI.  This branches if the answer has bit
7 set, i.e. is >= #$80.  Numbers with the most significant bit set are
considered to be negative numbers in twos-complement.

An interesting side note is that in the same way $FF == -1 in assembly
language, 99 == -1 in "BASIC Programming".  Try it!  You'll see why
the second keypad is laid out ingeniously, the two buttons to the left and
right of the "0" represent 99 and 1 to any program running, allowing the
user to gain horizontal control of something.

>>There is no Zero Page,Y although there is an Absolute,Y so you could
>>conceivably do LDA $0000,Y and only take one more byte and no more cycles.
>>I think the assembler will make this adjustment for you in most cases.
>
>No ZP,Y?  My reference says there is and I've used it a number of times
>already.. I guess DASM already adjusted it to Absolute,Y for me.

My reference says there isn't.  You can find out by marking that instruction
with a label and finding out where that label wound up on the assembly,
perhaps like this:

INDEXYTEST  LDA $xx,Y
	...
	...
	...
	*= $FFFC
	.word Start
	.word INDEXYTEST

Go in there with a hex browser after assembling it.  The last two bytes of
the file will tell you where it is.  Go to that byte, and if the bytes you
find there is B9 xx 00, then the assembler is converting it for you.

According to my source, the old C64 book, only LDX has an explicit Zero Page,Y
mode.


--
Archives available at http://www.biglist.com/lists/stella/archives/
E-mail UNSUBSCRIBE in the body to stella-request@xxxxxxxxxxx to be removed.

Current Thread