6502opcodes_03.html 26 KB

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