Debian Codegraph @

docs/AddrModes.txt

raw
1
2
3 ACME
4
5 ...the ACME Crossassembler for Multiple Environments
6
7 --- addressing modes ---
8
9
10 If an instruction can be used with different addressing modes, ACME
11 has to decide which one to use. Several instructions of the 6502 CPU
12 can be used with either "absolute" addressing or "zeropage-absolute"
13 addressing. The former one means there's a 16-bit argument, the latter
14 one means there's an 8-bit argument.
15 And the 65816 CPU even has some instructions with 24-bit addressing...
16
17 So how does ACME know which addressing mode to use?
18 The simple approach is to always use the smallest possible argument,
19 of course: If the argument fits in a byte, use zeropage addressing. If
20 it doesn't, use absolute addressing. If it needs more than two bytes
21 and the 65816 CPU is chosen, use 24-bit addressing.
22
23 In most cases this works - with two exceptions. The remainder of this
24 text may sound somewhat confusing now, so if you don't have any
25 problems with addressing modes, then don't bother trying to understand
26 everything this texts says. :)
27
28 The two exceptions are:
29
30
31
32 *** 1) Symbols are defined too late
33
34 If ACME cannot figure out the argument value in the first pass, it
35 assumes that the instruction uses 16-bit addressing.
36
37 If it later finds out that the argument only needs 8 bits, ACME gives
38 a warning ("using oversized addressing mode") and continues. However,
39 if it finds out that the argument needs 24 bits, it gives an error.
40
41 These problems can be solved by defining the symbols *before* using
42 them, so that the value can be figured out in the first pass. If this
43 is not possible, you can use the postfix method, effectively exactly
44 defining what addressing mode to use. The postfix method is described
45 in a separate section below.
46
47
48
49 *** 2) You *want* to use an oversized addressing mode
50
51 On the 65816 CPU, "zeropage addressing" is called "direct page
52 addressing". The difference is that the position of the "direct page"
53 can be changed. Then, "lda $fa" does not necessarily access the same
54 memory location as "lda $00fa" anymore. The same goes for 16- and 24-
55 bit addressing: "lda $fabc" does not necessarily access the same
56 memory location as "lda $00fabc", because the default bank can be set
57 to something other than zero.
58 But even on the plain 6502 CPU you might want to force ACME to use an
59 oversized addressing mode, for example because of timing issues.
60
61 Again there are two ways to solve the problem: You can define the
62 target location using leading zeros. ACME will then use an addressing
63 mode that is big enough even if the leading zeros would have been
64 other digits:
65
66 symbol1 = $fb
67 symbol2 = $00fd
68 symbol3 = $0000ff
69
70 lda $fa
71 sta $00fc
72 lda $0000fe
73 sta symbol1
74 lda symbol2
75 sta symbol3
76
77 will be assembled to
78
79 a5 fa ; lda $fa
80 8d fc 00 ; sta $00fc
81 af fe 00 00 ; lda $0000fe
82 85 fb ; sta $fb
83 ad fd 00 ; lda $00fd
84 8f ff 00 00 ; sta $0000ff
85
86 This feature can be disabled using the "--ignore-zeroes" CLI switch.
87
88
89 The other possibility is to use the postfix method (described in the
90 next section).
91
92
93
94 *** The postfix method
95
96 Warning: This may sound very complicated at first, but I think that
97 once you get used to it you'll agree it's useful. If you don't want to
98 use this, stick to the "leading zeros" method and don't bother about
99 postfixes.
100
101 Still with me? Okay:
102 You can force ACME to use a specific addressing mode by adding "+1",
103 "+2" or "+3" to the assembler mnemonic. Each one of these postfixes
104 sets the relevant "Force Bit" in ACME's result. If Force Bit 3 is set,
105 ACME will use 24-bit addressing. Force Bit 2 means 16-bit addressing
106 and Force Bit 1 means 8-bit addressing. Higher Force Bits have higher
107 priorities.
108
109 Here's an (overly complicated) example:
110
111 symbol1 = $fb
112 symbol2 = $fd
113 symbol3+3 = $ff ; set Force Bit 3 and store in symbol's flags
114
115 ldx $fa
116 ldy+2 $fc ; set Force Bit 2 (16-bit addressing)
117 lda+3 $fe ; set Force Bit 3 (24-bit addressing)
118 stx symbol1
119 sty+2 symbol2 ; set Force Bit 2 (16-bit addressing)
120 sta symbol3 ; no need to set Force Bit 3 as it is
121 ; already set in "symbol3".
122
123 will be assembled to
124
125 a6 fa ; ldx $fa
126 ac fc 00 ; ldy $00fc
127 af fe 00 00 ; lda $0000fe
128 86 fb ; stx $fb
129 8c fd 00 ; sty $00fd
130 8f ff 00 00 ; sta $0000ff
131
132 Postfixes added directly to the mnemonic have higher priority than
133 those given to the argument. As you can see, you can add the postfix
134 to the symbol definition as well (equivalent to leading zeros).
135
136 Applying the byte extraction operators ("<" gives the low byte, ">"
137 gives the high byte and "^" gives the bank byte of a value) to any
138 value will clear the argument's Force Bits 2 and 3 and set Force
139 Bit 1 instead. So "lda <symbol" will use 8-bit addressing, regardless
140 of the symbol's Force Bits. Of course, you can change this by
141 postfixing the instruction again... :)
142
143
144
145 *** The algorithm to find the right addressing mode
146
147 You don't need to read this paragraph just to use ACME, I only
148 included it for completeness' sake. This is a description of ACME's
149 strategy for finding the addressing mode to use:
150
151 First, ACME checks whether the instruction has any postfix. If it has,
152 ACME acts upon it. So postfixes have the highest priority.
153
154 Otherwise, ACME checks whether the argument has any Force Bits set
155 (because of leading zeros or byte extraction operators, for example)
156 or references symbols that have. If any Force Bit is set, ACME acts
157 upon it. This is the next priority level: Leading zeros or postfixes
158 added to the symbol definition.
159
160 Otherwise, ACME checks whether the argument could be figured out in
161 the first pass. If it couldn't, ACME tries to use a default addressing
162 mode (normally this will be 16-bit addressing).
163 In case the value could be figured out even in the first pass, then
164 everything's cool and froody: ACME just looks at the argument's value
165 and uses the smallest addressing mode that matches.
166
167 I admit that this algorithm sounds complicated, but it hasn't failed
168 yet. :) And in everyday usage, it does exactly what one expects.
169
170 If you want to take a closer look at the algorithm, examine the
171 "calc_arg_size" function in the "src/mnemo.c" file.