-
Notifications
You must be signed in to change notification settings - Fork 5
/
6502opcodes.htm
720 lines (684 loc) · 25.7 KB
/
6502opcodes.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
<meta http-equiv="Context-Type" content="text/html; charset=iso-8859-1">
<HTML><HEAD><TITLE>www.6502.org: Tutorials and Aids</TITLE>
<META name="description" content="Learn how to do all kinds of things
with the 6502 microprocessor.">
<style><!-- a:{color:#000099} a:vlink{color:#000099}
a:hover{color:#007FFF} --></style></HEAD>
<BODY BACKGROUND="../graph.gif" TEXT="#000000"
LINK="#000099" VLINK="#000099">
<A HREF="../index.htm"><IMG SRC="../www.6502.orgg/mainlink.jpg" align=right
border=0 alt="[Return to Main Page]"></A><BODY>
<FONT size=+1>NMOS 6502 Opcodes</FONT> <FONT SIZE=-1>by John Pickens,
Updated by Bruce Clark</FONT>
<BR><A HREF="index.htm">[Up to Tutorials and Aids]</A>
<BR>
<HR>
<H2>INDEX</H2></A>
<P><TT>
<TABLE border=2 cellPadding=2 width="90%">
<TBODY>
<TR>
<TD align=middle><A
href="#BRA">Branches</A></TD>
<TD align=middle><A
href="#DFLAG">Decimal Mode</A></TD>
<TD align=middle><A
href="#IFLAG">Interrupt Flag</A></TD>
<TD align=middle><A
href="#VFLAG">Overflow Flag</A></TD>
<TD align=middle><A href="#PC">Program
Counter</A></TD>
<TD align=middle><A
href="#STACK">Stack</A></TD>
<TD align=middle><A
href="#TIMES">Times</A></TD>
<TD align=middle><A
href="#WRAP">Wrap-around</A></TD></TR></TBODY></TABLE>
<P>
<TABLE border=2 cellPadding=2 width="90%">
<TBODY>
<TR>
<TD align=middle><A
href="#ADC">ADC</A></TD>
<TD align=middle><A
href="#AND">AND</A></TD>
<TD align=middle><A
href="#ASL">ASL</A></TD>
<TD align=middle><A
href="#BCC">BCC</A></TD>
<TD align=middle><A
href="#BCS">BCS</A></TD>
<TD align=middle><A
href="#BEQ">BEQ</A></TD>
<TD align=middle><A
href="#BIT">BIT</A></TD>
<TD align=middle><A
href="#BMI">BMI</A></TD>
<TD align=middle><A
href="#BNE">BNE</A></TD>
<TD align=middle><A
href="#BPL">BPL</A></TD>
<TD align=middle><A
href="#BRK">BRK</A></TD>
<TD align=middle><A
href="#BVC">BVC</A></TD>
<TD align=middle><A
href="#BVS">BVS</A></TD>
<TD align=middle><A
href="#CLC">CLC</A></TD></TR>
<TR>
<TD align=middle><A
href="#CLD">CLD</A></TD>
<TD align=middle><A
href="#CLI">CLI</A></TD>
<TD align=middle><A
href="#CLV">CLV</A></TD>
<TD align=middle><A
href="#CMP">CMP</A></TD>
<TD align=middle><A
href="#CPX">CPX</A></TD>
<TD align=middle><A
href="#CPY">CPY</A></TD>
<TD align=middle><A
href="#DEC">DEC</A></TD>
<TD align=middle><A
href="#DEX">DEX</A></TD>
<TD align=middle><A
href="#DEY">DEY</A></TD>
<TD align=middle><A
href="#EOR">EOR</A></TD>
<TD align=middle><A
href="#INC">INC</A></TD>
<TD align=middle><A
href="#INX">INX</A></TD>
<TD align=middle><A
href="#INY">INY</A></TD>
<TD align=middle><A
href="#JMP">JMP</A></TD></TR>
<TR>
<TD align=middle><A
href="#JSR">JSR</A></TD>
<TD align=middle><A
href="#LDA">LDA</A></TD>
<TD align=middle><A
href="#LDX">LDX</A></TD>
<TD align=middle><A
href="#LDY">LDY</A></TD>
<TD align=middle><A
href="#LSR">LSR</A></TD>
<TD align=middle><A
href="#NOP">NOP</A></TD>
<TD align=middle><A
href="#ORA">ORA</A></TD>
<TD align=middle><A
href="#PHA">PHA</A></TD>
<TD align=middle><A
href="#PHP">PHP</A></TD>
<TD align=middle><A
href="#PLA">PLA</A></TD>
<TD align=middle><A
href="#PLP">PLP</A></TD>
<TD align=middle><A
href="#ROL">ROL</A></TD>
<TD align=middle><A
href="#ROR">ROR</A></TD>
<TD align=middle><A
href="#RTI">RTI</A></TD></TR>
<TR>
<TD align=middle><A
href="#RTS">RTS</A></TD>
<TD align=middle><A
href="#SBC">SBC</A></TD>
<TD align=middle><A
href="#SEC">SEC</A></TD>
<TD align=middle><A
href="#SED">SED</A></TD>
<TD align=middle><A
href="#SEI">SEI</A></TD>
<TD align=middle><A
href="#STA">STA</A></TD>
<TD align=middle><A
href="#STX">STX</A></TD>
<TD align=middle><A
href="#STY">STY</A></TD>
<TD align=middle><A
href="#TAX">TAX</A></TD>
<TD align=middle><A
href="#TAY">TAY</A></TD>
<TD align=middle><A
href="#TSX">TSX</A></TD>
<TD align=middle><A
href="#TXA">TXA</A></TD>
<TD align=middle><A
href="#TXS">TXS</A></TD>
<TD align=middle><A
href="#TYA">TYA</A></TD></TR></TBODY></TABLE>
</TT>
<P><A name=ADC> </A>
<H2>ADC (ADd with Carry)</H2>
<P>Affects Flags: S V Z C <PRE>MODE SYNTAX HEX LEN TIM
Immediate ADC #$44 $69 2 2
Zero Page ADC $44 $65 2 3
Zero Page,X ADC $44,X $75 2 4
Absolute ADC $4400 $6D 3 4
Absolute,X ADC $4400,X $7D 3 4+
Absolute,Y ADC $4400,Y $79 3 4+
Indirect,X ADC ($44,X) $61 2 6
Indirect,Y ADC ($44),Y $71 2 5+
+ add 1 cycle if page boundary crossed
</PRE>ADC results are dependant on the setting of the <A
href="#DFLAG">decimal flag</A>. In decimal
mode, addition is carried out on the assumption that the values involved are
packed BCD (Binary Coded Decimal).
<P>There is no way to add without carry.
<P><A name=AND> </A>
<H2>AND (bitwise AND with accumulator) </H2>
<P>Affects Flags: S Z <PRE>MODE SYNTAX HEX LEN TIM
Immediate AND #$44 $29 2 2
Zero Page AND $44 $25 2 2
Zero Page,X AND $44,X $35 2 3
Absolute AND $4400 $2D 3 4
Absolute,X AND $4400,X $3D 3 4+
Absolute,Y AND $4400,Y $39 3 4+
Indirect,X AND ($44,X) $21 2 6
Indirect,Y AND ($44),Y $31 2 5+
+ add 1 cycle if page boundary crossed
</PRE>
<P><A name=ASL> </A>
<H2>ASL (Arithmetic Shift Left) </H2>
<P>Affects Flags: S Z C <PRE>MODE SYNTAX HEX LEN TIM
Accumulator ASL A $0A 1 2
Zero Page ASL $44 $06 2 5
Zero Page,X ASL $44,X $16 2 6
Absolute ASL $4400 $0E 3 6
Absolute,X ASL $4400,X $1E 3 7
</PRE>ASL shifts all bits left one position. 0 is shifted into bit 0 and the
original bit 7 is shifted into the Carry.
<P>
<P><A name=BIT> </A>
<H2>BIT (test BITs) </H2>
<P>Affects Flags: N V Z <PRE>MODE SYNTAX HEX LEN TIM
Zero Page BIT $44 $24 2 3
Absolute BIT $4400 $2C 3 4
</PRE>BIT sets the Z flag as though the value in the address tested were ANDed
with the accumulator. The S and V flags are set to match bits 7 and 6
respectively in the value stored at the tested address.
<P>BIT is often used to skip one or two following bytes as in:
<P><PRE>CLOSE1 LDX #$10 If entered here, we
.BYTE $2C effectively perform
CLOSE2 LDX #$20 a BIT test on $20A2,
.BYTE $2C another one on $30A2,
CLOSE3 LDX #$30 and end up with the X
CLOSEX LDA #12 register still at $10
STA ICCOM,X upon arrival here.
</PRE>
<P><A name=BCC> </A> <A name=BCS> </A> <A name=BEQ> </A> <A
name=BNE> </A> <A name=BMI> </A> <A name=BPL> </A> <A
name=BVC> </A> <A name=BVS> </A> <A name=BRA> </A>
<H2>Branch Instructions</H2>
<P>Affect Flags: none
<P>All branches are relative mode and have a length of two bytes. Syntax is "Bxx
Displacement" or (better) "Bxx Label". See the notes on the <A
href="#PC">Program Counter</A> for more on
displacements.
<P>Branches are dependant on the status of the flag bits when the op code is
encountered. A branch not taken requires two machine cycles. Add one if the
branch is taken and add one more if the branch crosses a page boundary. <PRE>MNEMONIC HEX
BPL (Branch on PLus) $10
BMI (Branch on MInus) $30
BVC (Branch on oVerflow Clear) $50
BVS (Branch on oVerflow Set) $70
BCC (Branch on Carry Clear) $90
BCS (Branch on Carry Set) $B0
BNE (Branch on Not Equal) $D0
BEQ (Branch on EQual) $F0
</PRE>There is no BRA (BRanch Always) instruction but it can be easily emulated
by branching on the basis of a known condition. One of the best flags to use for
this purpose is the <A
href="#VFLAG">oVerflow</A> which is unchanged
by all but addition and subtraction operations.
<P>
A page boundary crossing occurs when the branch destination is on a different
page than the instruction AFTER the branch instruction. For example:
<pre>
SEC
BCS LABEL
NOP
</pre>
A page boundary crossing occurs (i.e. the BCS takes 4 cycles) when (the
address of) LABEL and the NOP are on different pages. This means that
<pre>
CLV
BVC LABEL
LABEL NOP
</pre>
the BVC instruction will take 3 cycles no matter what address it is located
at.
<P>
<P><A name=BRK> </A>
<H2>BRK (BReaK) </H2>
<P>Affects Flags: B <PRE>MODE SYNTAX HEX LEN TIM
Implied BRK $00 1 7
</PRE>BRK causes a non-maskable interrupt and increments the program counter by
one. Therefore an <A href="#RTI">RTI</A> will
go to the address of the BRK +2 so that BRK may be used to replace a
two-byte instruction for debugging and the subsequent RTI will be correct.
<P>
<P><A name=CMP> </A>
<H2>CMP (CoMPare accumulator) </H2>
<P>Affects Flags: S Z C <PRE>MODE SYNTAX HEX LEN TIM
Immediate CMP #$44 $C9 2 2
Zero Page CMP $44 $C5 2 3
Zero Page,X CMP $44,X $D5 2 4
Absolute CMP $4400 $CD 3 4
Absolute,X CMP $4400,X $DD 3 4+
Absolute,Y CMP $4400,Y $D9 3 4+
Indirect,X CMP ($44,X) $C1 2 6
Indirect,Y CMP ($44),Y $D1 2 5+
+ add 1 cycle if page boundary crossed
</PRE>Compare sets flags as if a subtraction had been carried out. If the value
in the accumulator is equal or greater than the compared value, the Carry will
be set. The equal (Z) and sign (S) flags will be set based on equality or lack
thereof and the sign (i.e. A>=$80) of the accumulator.
<P>
<P><A name=CPX> </A>
<H2>CPX (ComPare X register) </H2>
<P>Affects Flags: S Z C <PRE>MODE SYNTAX HEX LEN TIM
Immediate CPX #$44 $E0 2 2
Zero Page CPX $44 $E4 2 3
Absolute CPX $4400 $EC 3 4
</PRE>Operation and flag results are identical to equivalent mode accumulator <A
href="#CMP">CMP</A> ops.
<P>
<P><A name=CPY> </A>
<H2>CPY (ComPare Y register) </H2>
<P>Affects Flags: S Z C <PRE>MODE SYNTAX HEX LEN TIM
Immediate CPY #$44 $C0 2 2
Zero Page CPY $44 $C4 2 3
Absolute CPY $4400 $CC 3 4
</PRE>Operation and flag results are identical to equivalent mode accumulator <A
href="#CMP">CMP</A> ops.
<P>
<P><A name=DEC> </A>
<H2>DEC (DECrement memory) </H2>
<P>Affects Flags: S Z <PRE>MODE SYNTAX HEX LEN TIM
Zero Page DEC $44 $C6 2 5
Zero Page,X DEC $44,X $D6 2 6
Absolute DEC $4400 $CE 3 6
Absolute,X DEC $4400,X $DE 3 7
</PRE>
<P><A name=EOR> </A>
<H2>EOR (bitwise Exclusive OR) </H2>
<P>Affects Flags: S Z <PRE>MODE SYNTAX HEX LEN TIM
Immediate EOR #$44 $49 2 2
Zero Page EOR $44 $45 2 3
Zero Page,X EOR $44,X $55 2 4
Absolute EOR $4400 $4D 3 4
Absolute,X EOR $4400,X $5D 3 4+
Absolute,Y EOR $4400,Y $59 3 4+
Indirect,X EOR ($44,X) $41 2 6
Indirect,Y EOR ($44),Y $51 2 5+
+ add 1 cycle if page boundary crossed
</PRE>
<P><A name=CLC> </A> <A name=SEC> </A> <A name=CLD> </A> <A
name=SED> </A> <A name=CLI> </A> <A name=SEI> </A> <A
name=CLV> </A>
<H2>Flag (Processor Status) Instructions</H2>
<P>Affect Flags: as noted
<P>These instructions are implied mode, have a length of one byte and require
two machine cycles. <PRE>MNEMONIC HEX
CLC (CLear Carry) $18
SEC (SEt Carry) $38
CLI (CLear Interrupt) $58
SEI (SEt Interrupt) $78
CLV (CLear oVerflow) $B8
CLD (CLear Decimal) $D8
SED (SEt Decimal) $F8
</PRE>Notes:
<P><A name=IFLAG> </A> The Interrupt flag is used to prevent (SEI) or
enable (CLI) maskable interrupts (aka IRQ's). It does not signal the presence or
absence of an interrupt condition. The 6502 will set this flag automatically in
response to an interrupt and restore it to its prior status on completion of the
interrupt service routine. If you want your interrupt service routine to permit
other maskable interrupts, you must clear the I flag in your code.
<P><A name=DFLAG> </A> The Decimal flag controls how the 6502 adds and
subtracts. If set, arithmetic is carried out in packed binary coded decimal.
This flag is unchanged by interrupts and is unknown on power-up. The implication
is that a CLD should be included in boot or interrupt coding.
<P><A name=VFLAG> </A> The Overflow flag is generally misunderstood and
therefore under-utilised. After an ADC or SBC instruction, the overflow flag
will be set if the twos complement result is less than -128 or greater than
+127, and it will cleared otherwise. In twos complement, $80 through $FF
represents -128 through -1, and $00 through $7F represents 0 through +127.
Thus, after:
<pre>
CLC
LDA #$7F ; +127
ADC #$01 ; + +1
</pre>
the overflow flag is 1 (+127 + +1 = +128), and after:
<pre>
CLC
LDA #$81 ; -127
ADC #$FF ; + -1
</pre>
the overflow flag is 0 (-127 + -1 = -128). The overflow flag is not
affected by increments, decrements, shifts and logical operations i.e. only
ADC, BIT, CLV, PLP, RTI and SBC affect it. There is no op code to set the
overflow but a BIT test on an RTS instruction will do the trick.
<P>
<P>
<P><A name=INC> </A>
<H2>INC (INCrement memory) </H2>
<P>Affects Flags: S Z <PRE>MODE SYNTAX HEX LEN TIM
Zero Page INC $44 $E6 2 5
Zero Page,X INC $44,X $F6 2 6
Absolute INC $4400 $EE 3 6
Absolute,X INC $4400,X $FE 3 7
</PRE>
<P>
<P>
<P><A name=JMP> </A>
<H2>JMP (JuMP) </H2>
<P>Affects Flags: none <PRE>
MODE SYNTAX HEX LEN TIM
Absolute JMP $5597 $4C 3 3
Indirect JMP ($5597) $6C 3 5
</PRE>JMP transfers program execution to the following address (absolute) or to
the location contained in the following address (indirect). Note that there is
no carry associated with the indirect jump so: <PRE><H3>AN INDIRECT JUMP MUST NEVER USE A</H3>
<H3>VECTOR BEGINNING ON THE LAST BYTE</H3>
<H3>OF A PAGE</H3>
</PRE>For example if address $3000 contains $40, $30FF contains $80, and $3100
contains $50, the result of JMP ($30FF) will be a transfer of control to $4080
rather than $5080 as you intended i.e. the 6502 took the low byte of the address
from $30FF and the high byte from $3000.
<P>
<P>
<P><A name=JSR> </A>
<H2>JSR (Jump to SubRoutine) </H2>
<P>Affects Flags: none <PRE>MODE SYNTAX HEX LEN TIM
Absolute JSR $5597 $20 3 6
</PRE>JSR pushes the address-1 of the next operation on to the stack before
transferring program control to the following address. Subroutines are normally
terminated by a <A href="#RTS">RTS</A> op
code.
<P>
<P>
<P><A name=LDA> </A>
<H2>LDA (LoaD Accumulator) </H2>
<P>Affects Flags: S Z <PRE>MODE SYNTAX HEX LEN TIM
Immediate LDA #$44 $A9 2 2
Zero Page LDA $44 $A5 2 3
Zero Page,X LDA $44,X $B5 2 4
Absolute LDA $4400 $AD 3 4
Absolute,X LDA $4400,X $BD 3 4+
Absolute,Y LDA $4400,Y $B9 3 4+
Indirect,X LDA ($44,X) $A1 2 6
Indirect,Y LDA ($44),Y $B1 2 5+
+ add 1 cycle if page boundary crossed
</PRE>
<P>
<P>
<P><A name=LDX> </A>
<H2>LDX (LoaD X register) </H2>
<P>Affects Flags: S Z <PRE>MODE SYNTAX HEX LEN TIM
Immediate LDX #$44 $A2 2 2
Zero Page LDX $44 $A6 2 3
Zero Page,Y LDX $44,Y $B6 2 4
Absolute LDX $4400 $AE 3 4
Absolute,Y LDX $4400,Y $BE 3 4+
+ add 1 cycle if page boundary crossed
</PRE>
<P>
<P>
<P><A name=LDY> </A>
<H2>LDY (LoaD Y register) </H2>
<P>Affects Flags: S Z <PRE>MODE SYNTAX HEX LEN TIM
Immediate LDY #$44 $A0 2 2
Zero Page LDY $44 $A4 2 3
Zero Page,X LDY $44,X $B4 2 4
Absolute LDY $4400 $AC 3 4
Absolute,X LDY $4400,X $BC 3 4+
+ add 1 cycle if page boundary crossed
</PRE>
<P>
<P>
<P><A name=LSR> </A>
<H2>LSR (Logical Shift Right) </H2>
<P>Affects Flags: S Z C <PRE>MODE SYNTAX HEX LEN TIM
Accumulator LSR A $4A 1 2
Zero Page LSR $44 $46 2 5
Zero Page,X LSR $44,X $56 2 6
Absolute LSR $4400 $4E 3 6
Absolute,X LSR $4400,X $5E 3 7
</PRE>LSR shifts all bits right one position. 0 is shifted into bit 7 and the
original bit 0 is shifted into the Carry.
<P>
<P>
<P><A name=WRAP> </A>
<H2>Wrap-Around</H2>
<P>Use caution with indexed zero page operations as they are subject to
wrap-around. For example, if the X register holds $FF and you execute LDA $80,X
you will not access $017F as you might expect; instead you access $7F i.e.
$80-1. This characteristic can be used to advantage but make sure your code is
well commented.
<P>
It is possible, however, to access $017F when X = $FF by using the Absolute,X
addressing mode of LDA $80,X. That is, instead of:
<pre>
LDA $80,X ; ZeroPage,X - the resulting object code is: B5 80
</pre>
which accesses $007F when X=$FF, use:
<pre>
LDA $0080,X ; Absolute,X - the resulting object code is: BD 80 00
</pre>
which accesses $017F when X = $FF (a at cost of one additional byte and one
additional cycle). All of the ZeroPage,X and ZeroPage,Y instructions except
STX ZeroPage,Y and STY ZeroPage,X have a corresponding Absolute,X and
Absolute,Y instruction. Unfortunately, a lot of 6502 assemblers don't have an
easy way to force Absolute addressing, i.e. most will assemble a LDA $0080,X
as B5 80. One way to overcome this is to insert the bytes using the .BYTE
pseudo-op (on some 6502 assemblers this pseudo-op is called DB or DFB,
consult the assembler documentation) as follows:
<pre>
.BYTE $BD,$80,$00 ; LDA $0080,X (absolute,X addressing mode)
</pre>
The comment is optional, but highly recommended for clarity.
<P>In cases where you are writing code that will be relocated you must consider
wrap-around when assigning dummy values for addresses that will be adjusted.
Both zero and the semi-standard $FFFF should be avoided for dummy labels. The
use of zero or zero page values will result in assembled code with zero page
opcodes when you wanted absolute codes. With $FFFF, the problem is in
addresses+1 as you wrap around to page 0.
<P><A name=PC> </A>
<H2>Program Counter</H2>
<P>When the 6502 is ready for the next instruction it increments the program
counter before fetching the instruction. Once it has the op code, it increments
the program counter by the length of the operand, if any. This must be accounted
for when calculating branches or when pushing bytes to create a false return
address (i.e. jump table addresses are made up of addresses-1 when it is
intended to use an RTS rather than a JMP).
<P>The program counter is loaded least signifigant byte first. Therefore the
most signifigant byte must be pushed first when creating a false return address.
<P>When calculating branches a forward branch of 6 skips the following 6 bytes
so, effectively the program counter points to the address that is 8 bytes beyond
the address of the branch opcode; and a backward branch of $FA (256-6) goes to
an address 4 bytes before the branch instruction.
<P><A name=TIMES> </A>
<H2>Execution Times</H2>
<P>Op code execution times are measured in machine cycles; one machine cycle
equals one clock cycle. Many instructions require one extra cycle for
execution if a page boundary is crossed; these are indicated by a + following
the time values shown.
<P>
<P>
<P><A name=NOP> </A>
<H2>NOP (No OPeration) </H2>
<P>Affects Flags: none <PRE>MODE SYNTAX HEX LEN TIM
Implied NOP $EA 1 2
</PRE>NOP is used to reserve space for future modifications or effectively REM
out existing code.
<P>
<P>
<P><A name=ORA> </A>
<H2>ORA (bitwise OR with Accumulator) </H2>
<P>Affects Flags: S Z <PRE>MODE SYNTAX HEX LEN TIM
Immediate ORA #$44 $09 2 2
Zero Page ORA $44 $05 2 2
Zero Page,X ORA $44,X $15 2 3
Absolute ORA $4400 $0D 3 4
Absolute,X ORA $4400,X $1D 3 4+
Absolute,Y ORA $4400,Y $19 3 4+
Indirect,X ORA ($44,X) $01 2 6
Indirect,Y ORA ($44),Y $11 2 5+
+ add 1 cycle if page boundary crossed
</PRE>
<P>
<P>
<P><A name=TAX> </A> <A name=TXA> </A> <A name=TAY> </A> <A
name=TYA> </A> <A name=INX> </A> <A name=DEX> </A> <A
name=INY> </A> <A name=DEY> </A>
<H2>Register Instructions </H2>
<P>Affect Flags: S Z
<P>These instructions are implied mode, have a length of one byte and require
two machine cycles. <PRE>MNEMONIC HEX
TAX (Transfer A to X) $AA
TXA (Transfer X to A) $8A
DEX (DEcrement X) $CA
INX (INcrement X) $E8
TAY (Transfer A to Y) $A8
TYA (Transfer Y to A) $98
DEY (DEcrement Y) $88
INY (INcrement Y) $C8
</PRE>
<P>
<P>
<P><A name=ROL> </A>
<H2>ROL (ROtate Left) </H2>
<P>Affects Flags: S Z C <PRE>MODE SYNTAX HEX LEN TIM
Accumulator ROL A $2A 1 2
Zero Page ROL $44 $26 2 5
Zero Page,X ROL $44,X $36 2 6
Absolute ROL $4400 $2E 3 6
Absolute,X ROL $4400,X $3E 3 7
</PRE>ROL shifts all bits left one position. The Carry is shifted into bit 0 and
the original bit 7 is shifted into the Carry.
<P>
<P>
<P><A name=ROR> </A>
<H2>ROR (ROtate Right) </H2>
<P>Affects Flags: S Z C <PRE>MODE SYNTAX HEX LEN TIM
Accumulator ROR A $6A 1 2
Zero Page ROR $44 $66 2 5
Zero Page,X ROR $44,X $76 2 6
Absolute ROR $4400 $6E 3 6
Absolute,X ROR $4400,X $7E 3 7
</PRE>ROR shifts all bits right one position. The Carry is shifted into bit 7
and the original bit 0 is shifted into the Carry.
<P>
<P>
<P><A name=RTI> </A>
<H2>RTI (ReTurn from Interrupt) </H2>
<P>Affects Flags: all <PRE>MODE SYNTAX HEX LEN TIM
Implied RTI $40 1 6
</PRE>RTI retrieves the Processor Status Word (flags) and the Program Counter
from the stack in that order (interrupts push the PC first and then the PSW).
<P>Note that unlike RTS, the return address on the stack is the actual address
rather than the address-1.
<P>
<P>
<P><A name=RTS> </A>
<H2>RTS (ReTurn from Subroutine) </H2>
<P>Affects Flags: none <PRE>MODE SYNTAX HEX LEN TIM
Implied RTS $60 1 6
</PRE>RTS pulls the top two bytes off the stack (low byte first) and transfers
program control to that address+1. It is used, as expected, to exit a subroutine
invoked via <A href="#JSR">JSR</A> which
pushed the address-1.
<P>RTS is frequently used to implement a jump table where addresses-1 are pushed
onto the stack and accessed via RTS eg. to access the second of four routines: <PRE> LDX #1
JSR EXEC
JMP SOMEWHERE
LOBYTE
.BYTE <ROUTINE0-1,<ROUTINE1-1
.BYTE <ROUTINE2-1,<ROUTINE3-1
HIBYTE
.BYTE >ROUTINE0-1,>ROUTINE1-1
.BYTE >ROUTINE2-1,>ROUTINE3-1
EXEC
LDA HIBYTE,X
PHA
LDA LOBYTE,X
PHA
RTS
</PRE>
<P>
<P><A name=SBC> </A>
<H2>SBC (SuBtract with Carry)</H2>
<P>Affects Flags: S V Z C <PRE>MODE SYNTAX HEX LEN TIM
Immediate SBC #$44 $E9 2 2
Zero Page SBC $44 $E5 2 3
Zero Page,X SBC $44,X $F5 2 4
Absolute SBC $4400 $ED 3 4
Absolute,X SBC $4400,X $FD 3 4+
Absolute,Y SBC $4400,Y $F9 3 4+
Indirect,X SBC ($44,X) $E1 2 6
Indirect,Y SBC ($44),Y $F1 2 5+
+ add 1 cycle if page boundary crossed
</PRE>SBC results are dependant on the setting of the decimal flag. In decimal
mode, subtraction is carried out on the assumption that the values involved are
packed BCD (Binary Coded Decimal).
<P>There is no way to subtract without the carry which works as an inverse
borrow. i.e, to subtract you set the carry before the operation. If the carry is
cleared by the operation, it indicates a borrow occurred.
<P>
<P>
<P><A name=STA> </A>
<H2>STA (STore Accumulator) </H2>
<P>Affects Flags: none <PRE>MODE SYNTAX HEX LEN TIM
Zero Page STA $44 $85 2 3
Zero Page,X STA $44,X $95 2 4
Absolute STA $4400 $8D 3 4
Absolute,X STA $4400,X $9D 3 5
Absolute,Y STA $4400,Y $99 3 5
Indirect,X STA ($44,X) $81 2 6
Indirect,Y STA ($44),Y $91 2 6
</PRE>
<P>
<P><A name=TXS> </A> <A name=TSX> </A> <A name=PHA> </A> <A
name=PLA> </A> <A name=PHP> </A> <A name=PLP> </A> <A
name=STACK> </A>
<H2>Stack Instructions</H2>
<P>These instructions are implied mode, have a length of one byte and require
machine cycles as indicated. The "PuLl" operations are known as "POP" on most
other microprocessors. With the 6502, the stack is always on page one
($100-$1FF) and works top down. <PRE>MNEMONIC HEX TIM
TXS (Transfer X to Stack ptr) $9A 2
TSX (Transfer Stack ptr to X) $BA 2
PHA (PusH Accumulator) $48 3
PLA (PuLl Accumulator) $68 4
PHP (PusH Processor status) $08 3
PLP (PuLl Processor status) $28 4
</PRE>
<P>
<P>
<P><A name=STX> </A>
<H2>STX (STore X register)</H2>
<P>Affects Flags: none <PRE>MODE SYNTAX HEX LEN TIM
Zero Page STX $44 $86 2 3
Zero Page,Y STX $44,Y $96 2 4
Absolute STX $4400 $8E 3 4
</PRE>
<P>
<P>
<P><A name=STY> </A>
<H2>STY (STore Y register)</H2>
<P>Affects Flags: none <PRE>MODE SYNTAX HEX LEN TIM
Zero Page STY $44 $84 2 3
Zero Page,X STY $44,X $94 2 4
Absolute STY $4400 $8C 3 4
</PRE>
<P>
<P><FONT SIZE=-1>Last Updated March 30, 2001.</FONT>
</BODY></HTML>