[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help with confirmation on a small bit of assembly?



Tristan Mumford <xtristan.xmumford@xgmail.xcom> wrote:

> David Empson wrote:
> 
> > If it was SEC instead of CLC before the ADC then it would be adding one
> > more. ADC adds the operand plus the carry flag to the accumulator.
> 
> I see now. You have also made me think that I have to check for dropped
> flags in other parts of my code. That could cause some nasty fencepost
> errors.

Yes, you have to watch out for the carry flag, and the lack of
add/subtract without carry.

Some other flag-related traps for young players (especially if familiar
with other processors):

Load operations modify the N and Z flags, but store operations don't
affect any flags.

The 6502's carry flag is "backwards" for a subtract: CLC, SBC will
subtract one more, while SEC, SBC will not. This also affects the logic
of compare-and-branch sequences: BCC is "branch if less than" (unsigned)
on a 6502, but it is "branch if greater than or equal" on other
processors.

The 6502 is missing complex branch instructions (anything involving more
than one flag bit) so you have to synthesise "branch if less than or
equal", "branch if greater than" or any signed branches, by doing the
appropriate combinations of BCC, BCS, BVS, BVC, BPL, BMI, BEQ and/or
BNE.

On the 6502, if you use decimal mode, it doesn't implement some of the
flags (if I remember right C is OK, but N, Z and V don't get modified by
decimal ADC/SBC). This behaviour was amended on the 65C02 (and is the
most reliable way of detecting a 6502 vs 65C02).

> Sorry. 6502 assembly isn't one of my strongest languages. It's even worse
> when reading other peoples code.

6502 was my first assembly language (and about my third programming
language, after Apple Pascal and Applesoft BASIC). I later moved on to
more flexible assembly languages at work (I do embedded systems
programming) and mostly use C these days, but I still remember enough
6502 machine code to be able to rattle off rubbish like this and know
what it means:

A9 C1 20 ED FD 18 69 01 C9 DB D0 F6 60

(Assuming I've remembered the correct opcodes for ADC and CMP
immediate.)

Don't ask me to write anything more complex off the cuff. ;-)

Sigh. All those formative teenage years spent typing in programs from
Nibble magazines. I'll never forget LDA immediate. What a waste of brain
cells.

> You've pointed out yet another idiotic mistake I've made of late but
> couldn't see. Somehow during my breakdown of it 0xF = 16. Last time I
> checked 16 != 15.

I'm sure someone can come up with a proof that 16 = 15, based on the 1 =
0 concept. :-)

Anyway, good to know it was a simple mistake.

> Thankyou yet again.

Glad to be of service.

-- 
David Empson
dempson@actrix.gen.nz