This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembly-6502.html
355 lines (352 loc) · 16 KB
/
assembly-6502.html
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
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="description" content="Assembly is an extremely low-level human-readable language that has a strong relation between the code and the corresponding machine code."><!-- Bing --><meta name="msvalidate.01" content="45CBBE1BD8265A2217DFDA630EB8F84A" /><title>Tiny Brain Fans - Assembly (6502)</title><link rel="stylesheet" href="tinystyle.css"></head><body>
<main id="main"><article id="content"><h1 id="title">Assembly (6502)</h1><p>Assembly is an extremely low-level human-readable language that has a strong relation between the code and the corresponding <a href="machine-code.html">machine code</a>.</p>
<p>The Motorola 6502 is a microcontroller based on their 6800 series, but simplified, less expensive, and faster.</p>
<!--
## Setup
Insert emulation/compilation instructions here
-->
<h2>Memory Locations</h2>
<h3><code>$0000 - $00ff</code> - Zero Page</h3>
<p>This stores commonly used game variables as they can be accessed with only two digit addresses (see <a href="#addressing-modes" target="_blank">addressing modes</a>).</p>
<h3><code>$0200 - $05ff</code> - Display</h3>
<p>This stores values to be rendered on the display. Each row of the screen is represented by <code>$20</code> values stored inline, rendering left to right. e.g. the first row of the screen is held in <code>$0200</code> - <code>$021f</code>; the second row is held in <code>$0220</code> - <code>$023f</code>.</p>
<h2>Syntax</h2>
<h3>Literals</h3>
<table>
<thead>
<tr>
<th>Type</th>
<th>Syntax</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Binary</td>
<td><code>%nnnnnnnn</code></td>
<td><code>n</code> is a binary digit. Trailing numbers can extend</td>
</tr>
<tr>
<td>Hexadecimal</td>
<td><code>$xy</code></td>
<td><code>xy</code> are hex digits. Trailing numbers can extend</td>
</tr>
<tr>
<td>Decimal</td>
<td><code>xy</code></td>
<td><code>xy</code> are decimal digits. Trailing numbers can extend</td>
</tr>
</tbody></table><h3>Flags</h3>
<table>
<thead>
<tr>
<th>Flag</th>
<th>Name</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>C</td>
<td>Carry Flag</td>
<td>The carry flag is set if the last operation caused an overflow from bit 7 of the result or an underflow from bit 0. This condition is set during arithmetic, comparison and during logical shifts (or explicitly).</td>
</tr>
<tr>
<td>Z</td>
<td>Zero Flag</td>
<td>The zero flag is set if the result of the last operation as was zero.</td>
</tr>
<tr>
<td>I</td>
<td>Interrupt Disable</td>
<td>The interrupt disable flag is set if the program has executed a <code>SEI</code> instruction. While this flag is set the processor will not respond to interrupts from devices until it is cleared by a <code>CLI</code> instruction.</td>
</tr>
<tr>
<td>D</td>
<td>Decimal Mode Flag</td>
<td>While the decimal mode flag is set the processor will obey the rules of Binary Coded Decimal (BCD) arithmetic during addition and subtraction. The flag can be explicitly set using <code>SED</code> and cleared with <code>CLD</code>.</td>
</tr>
<tr>
<td>B</td>
<td>Break Command</td>
<td>The break command bit is set when a <code>BRK</code> instruction has been executed and an interrupt has been generated to process it.</td>
</tr>
<tr>
<td>V</td>
<td>Overflow Flag</td>
<td>The overflow flag is set during arithmetic operations if the result has yielded an invalid 2's complement result (e.g. adding to positive numbers and ending up with a negative result: 64 + 64 => -128). It is determined by looking at the carry between bits 6 and 7 and between bit 7 and the carry flag.</td>
</tr>
<tr>
<td>N</td>
<td>Negative Flag</td>
<td>The negative flag is set if the result of the last operation had bit 7 set to a one.</td>
</tr>
</tbody></table><h3>Addressing Modes</h3>
<p>These will all use <code>STA</code> but can be used with any command that uses an address.</p>
<table>
<thead>
<tr>
<th>Type</th>
<th>Syntax</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td>Absolute</td>
<td><code>LDA $c000</code></td>
<td>Load the value at this full memory location</td>
</tr>
<tr>
<td>Zero page</td>
<td><code>LDA $c0</code></td>
<td>Load the value at <code>$00c0</code>, <code>c0</code> within the zero page</td>
</tr>
<tr>
<td>Absolute, X</td>
<td><code>LDA $c000,X</code></td>
<td>Load the value at <code>$c000</code> + the value in register X (e.g. if X is <code>$01</code>, load value at memory location <code>$c001</code>)</td>
</tr>
<tr>
<td>Zero page, X</td>
<td><code>LDA $c0,X</code></td>
<td>Load the value at <code>$00c0</code> + the value in register X (e.g. if X is <code>$01</code>, load value at memory location <code>$c001</code>)</td>
</tr>
<tr>
<td>Immediate</td>
<td><code>LDA #$c0</code></td>
<td>Load value <code>$c0</code></td>
</tr>
<tr>
<td>Relative</td>
<td><code>$c0</code> or <code>label</code></td>
<td>Go <code>$c0</code> bytes forward/backward (to get to label position). Used with branching and jump commands (see below)</td>
</tr>
<tr>
<td>Implicit</td>
<td><code>INX</code></td>
<td>Do what instruction implies (no args)</td>
</tr>
<tr>
<td>Indirect</td>
<td><code>LDA ($c000)</code></td>
<td>Load value at memory address <code>$c000</code> and the following byte, with the first address being the <em>least</em> significant byte, and the following address the <em>most</em> significant byte (e.g. in this example, if <code>$00c0</code> holds <code>01</code> and <code>$00c1</code> holds <code>0f</code>, it would dereference to <code>$0f01</code>).</td>
</tr>
<tr>
<td>Indexed Indirect</td>
<td><code>LDA ($c0,X)</code></td>
<td>Load value at the address (<code>$00c0</code> + <code>X</code>), with the resulting first address being the <em>least</em> significant byte, and the following address the <em>most</em> significant byte (e.g. in this example, if <code>X</code> was <code>01</code>, we would look at the address <code>$00c1</code>. If <code>$00c1</code> holds <code>01</code> and <code>$00c2</code> holds <code>0f</code>, it would dereference to <code>$0f01</code>). <em>NOTE: Can only be used with X</em></td>
</tr>
<tr>
<td>Indirect Indexed</td>
<td><code>LDA ($c0),Y</code></td>
<td>Load value at the address found at <code>$00c0</code> plus the <em>value</em> of <code>Y</code>, with the resulting first address being the <em>least</em> significant byte, and the following address the <em>most</em> significant byte (e.g. in this example, lets say <code>Y</code> was <code>01</code>. We would look at the address <code>$00c0</code>. If <code>$00c0</code> holds <code>01</code> and <code>$00c1</code> holds <code>0f</code>, it would dereference to <code>$0f01</code> <em>plus</em> the value of <code>Y</code>, resulting in <code>$0f02</code>).</td>
</tr>
</tbody></table><h3>Commands</h3>
<p>A <strong>value</strong> refers to a number prefaced by a <code>#</code>, whereas an <strong>address/location</strong> refers to a hexadecimal address.</p>
<table>
<thead>
<tr>
<th>Command</th>
<th>Argument(s)</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>BRK</code></td>
<td></td>
<td>Break out of the program</td>
</tr>
<tr>
<td><code>org $n</code></td>
<td><code>$n</code>: Number</td>
<td>Set program origin to <code>$n</code></td>
</tr>
<tr>
<td><code>LDA n</code></td>
<td><code>n</code>: value or memory address/location where value is stored</td>
<td>Load <code>n</code> into register A</td>
</tr>
<tr>
<td><code>STA n</code></td>
<td><code>n</code>: memory address/location</td>
<td>Store value in register A at <code>n</code></td>
</tr>
<tr>
<td><code>TAX</code></td>
<td></td>
<td>Transfer value in A to X</td>
</tr>
<tr>
<td><code>INX</code> / <code>DEX</code></td>
<td></td>
<td>Increment/Decrement value in X by 1</td>
</tr>
<tr>
<td><code>ADC n</code></td>
<td><code>n</code>: value or memory address/location where value is stored</td>
<td>Add <code>n</code> with carry to the A register</td>
</tr>
</tbody></table><h4>Flag Commands</h4>
<p>These commands are used solely for the purpose of changing and setting flags.</p>
<table>
<thead>
<tr>
<th>Command</th>
<th>Argument(s)</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>CMP n</code> / <code>CPX n</code> / <code>CPY n</code></td>
<td>n: value or memory address/location where value is stored</td>
<td>Compare value at A/X/Y to n. If equal, set <code>Z</code> flag to 1; else, set <code>Z</code> flag to 0. <em>Note: behind the scenes, <a href="http://www.6502.org/tutorials/compare_beyond.html#2.1" target="_blank">these instructions are doing subtraction</a></em>.</td>
</tr>
<tr>
<td><code>SEI</code> / <code>CLI</code></td>
<td></td>
<td>Set/Clear Interrupt Disable</td>
</tr>
</tbody></table><h4>Branching</h4>
<p><strong>label</strong>: A label that must be within 256 bytes of invocation</p>
<p>Any label can be substituted for a direct memory address.</p>
<table>
<thead>
<tr>
<th>Command</th>
<th>Name</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>BEQ label</code></td>
<td>Branch on EQual</td>
<td>If <code>Z</code> flag is 1 (comparison was equal), go to label</td>
</tr>
<tr>
<td><code>BNE label</code></td>
<td>Branch on Not Equal</td>
<td>If <code>Z</code> flag is 0 (comparison was not equal), go to label</td>
</tr>
<tr>
<td><code>BCS label</code></td>
<td>Branch on Carry Set</td>
<td>If carry flag is 1 (set), go to label</td>
</tr>
<tr>
<td><code>BCC label</code></td>
<td>Branch on Carry Clear</td>
<td>If carry flag is 0 (not set), go to label</td>
</tr>
<tr>
<td><code>JMP label</code></td>
<td>Unconditional Jump</td>
<td>Jump directly to label</td>
</tr>
<tr>
<td><code>JSR label</code> / <code>RTS</code></td>
<td>Jump to SubRoutine / ReTurn from Subroutine</td>
<td>Jump directly to label and push current address - 1 to the <a href="stack.html">stack</a>. This will be retrieved via the return statement.</td>
</tr>
<tr>
<td><code>BVS label</code></td>
<td>Branch on oVerflow Set</td>
<td>If <code>V</code> flag is set, or bit 6 is set when using <code>BIT</code>, go to label</td>
</tr>
<tr>
<td><code>BVC label</code></td>
<td>Branch on oVerflow Clear</td>
<td>If <code>V</code> flag is not set, or bit 6 is not set when using <code>BIT</code>, go to label</td>
</tr>
<tr>
<td><code>BMI label</code></td>
<td>Branch on MInus</td>
<td>If <code>N</code> flag is set, or bit 7 is set when using <code>BIT</code>, go to label</td>
</tr>
<tr>
<td><code>BPL label</code></td>
<td>Branch on PLus</td>
<td>If <code>N</code> flag is not set, or bit 7 is not set when using <code>BIT</code>, go to label</td>
</tr>
</tbody></table><h4><a href="stack.html">Stack</a></h4>
<table>
<thead>
<tr>
<th>Command</th>
<th>Name</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>PHA</code></td>
<td>PusH Accumulator</td>
<td>Push the accumulator onto the top of the stack</td>
</tr>
<tr>
<td><code>PLA</code></td>
<td>PulL (Pop) Accumulator</td>
<td>Pop the accumulator off the top of the stack</td>
</tr>
</tbody></table><h4>Bitwise Operators</h4>
<table>
<thead>
<tr>
<th>Command</th>
<th>Parameters</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>AND n</code></td>
<td><code>n</code>: Value or address</td>
<td>Bitwise AND of value of <code>n</code> or value at address <code>n</code> against the accumulator register</td>
</tr>
<tr>
<td><code>LSR [n]</code></td>
<td><code>n</code>: Value or address (if omitted, default to accumulator)</td>
<td>Bitwise shift right</td>
</tr>
</tbody></table><h2>References</h2>
<ul>
<li><a href="http://skilldrick.github.io/easy6502/" target="_blank">http://skilldrick.github.io/easy6502/</a></li>
<li><a href="https://famicom.party/book/" target="_blank">https://famicom.party/book/</a></li>
<li><a href="https://wiki.xxiivv.com/site/assembly.html" target="_blank">https://wiki.xxiivv.com/site/assembly.html</a></li>
<li><a href="https://git.sr.ht/~rabbits/famicom_cookbook/tree/master/item/README.md" target="_blank">https://git.sr.ht/~rabbits/famicom_cookbook/tree/master/item/README.md</a></li>
<li><a href="https://www.youtube.com/watch?v=lsvSZamCCBM" target="_blank">https://www.youtube.com/watch?v=lsvSZamCCBM</a></li>
<li><a href="https://en.m.wikibooks.org/wiki/6502_Assembly" target="_blank">https://en.m.wikibooks.org/wiki/6502_Assembly</a></li>
<li><a href="https://archive.org/details/6502_Assembly_Language_Subroutines/page/n15/mode/2up" target="_blank">https://archive.org/details/6502_Assembly_Language_Subroutines/page/n15/mode/2up</a></li>
<li><a href="https://taywee.github.io/NerdyNights/nerdynights/asmfirstapp.html" target="_blank">https://taywee.github.io/NerdyNights/nerdynights/asmfirstapp.html</a></li>
<li><a href="https://rosettacode.org/wiki/Category:6502_Assembly" target="_blank">https://rosettacode.org/wiki/Category:6502_Assembly</a></li>
<li><a href="https://www.middle-engine.com/blog/posts/2020/06/23/programming-the-nes-the-6502-in-detail" target="_blank">https://www.middle-engine.com/blog/posts/2020/06/23/programming-the-nes-the-6502-in-detail</a></li>
<li><a href="https://irkenkitties.com/blog/2015/03/29/creating-sound-on-the-nes/" target="_blank">https://irkenkitties.com/blog/2015/03/29/creating-sound-on-the-nes/</a></li>
<li><a href="http://www.6502.org/tutorials/compare_beyond.html" target="_blank">http://www.6502.org/tutorials/compare_beyond.html</a></li>
<li><a href="http://visual6502.org/JSSim/index.html" target="_blank">http://visual6502.org/JSSim/index.html</a></li>
<li>PRNG and others: <a href="http://retro.hansotten.nl/6502-sbc/lee-davison-web-site/some-code-bits/" target="_blank">http://retro.hansotten.nl/6502-sbc/lee-davison-web-site/some-code-bits/</a></li>
<li>A better PRNG: <a href="https://wiki.nesdev.org/w/index.php/Random_number_generator#Overlapped" target="_blank">https://wiki.nesdev.org/w/index.php/Random_number_generator#Overlapped</a></li>
<li><a href="https://eli.li/2022/01/27/notes-on-6502-assembly" target="_blank">https://eli.li/2022/01/27/notes-on-6502-assembly</a></li>
</ul>
<p>IDE and Debugger:</p>
<ul>
<li><a href="https://8bitworkshop.com/v3.9.0/?platform=vcs&file=examples%2Fhello.a" target="_blank">https://8bitworkshop.com/v3.9.0/?platform=vcs&file=examples%2Fhello.a</a></li>
</ul>
<p>Instructions and Flags Reference:</p>
<ul>
<li><a href="http://www.6502.org/tutorials/6502opcodes.html" target="_blank">http://www.6502.org/tutorials/6502opcodes.html</a></li>
<li><a href="https://retroscience.net/writing-6502-assembler.html" target="_blank">https://retroscience.net/writing-6502-assembler.html</a></li>
<li><a href="http://www.obelisk.me.uk/6502/registers.html#Z" target="_blank">http://www.obelisk.me.uk/6502/registers.html#Z</a></li>
<li><a href="https://cyborgsystems.loadcode.co.uk/cpu/6502/" target="_blank">https://cyborgsystems.loadcode.co.uk/cpu/6502/</a></li>
<li><a href="https://masswerk.at/6502/6502_instruction_set.html" target="_blank">https://masswerk.at/6502/6502_instruction_set.html</a></li>
<li><a href="https://wiki.cdot.senecacollege.ca/wiki/6502_Jumps,_Branches,_and_Procedures" target="_blank">https://wiki.cdot.senecacollege.ca/wiki/6502_Jumps,_Branches,_and_Procedures</a></li>
</ul>
<p>Examples:</p>
<ul>
<li><a href="https://github.com/milofultz/adventofcode2015/" target="_blank">https://github.com/milofultz/adventofcode2015/</a></li>
<li><a href="https://milofultz.com/2021/11/14/sorting-in-6502" target="_blank">https://milofultz.com/2021/11/14/sorting-in-6502</a></li>
<li><a href="https://wiki.nesdev.org/w/index.php/Random_number_generator" target="_blank">https://wiki.nesdev.org/w/index.php/Random_number_generator</a></li>
</ul>
<section id="backlinks"><details><summary>Backlinks</summary><ul><li><a href="minicube64.html">Minicube64</a></li></ul></details></section><p class="last-modified">Last modified: 202206101419</p></article></main><footer><nav><a href="index.html">Sitemap</a></nav><div class="social"><p>Built using <a href="http://codeberg.org/milofultz/swiki" target="_blank" rel="noopener noreferrer">{{SWIKI}}</a></p><p><a href="http://codeberg.org/milofultz/" target="_blank" rel="noopener noreferrer">Codeberg</a></p><p><a href="http://milofultz.com/" target="_blank" rel="noopener noreferrer">milofultz.com</a></p><p><a href="https://merveilles.town/@milofultz" target="_blank" rel="me noopener noreferrer">Mastodon</a></p></div></footer></body></html>