-
Notifications
You must be signed in to change notification settings - Fork 0
/
smc.html
639 lines (594 loc) · 18.2 KB
/
smc.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
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<LINK REL="stylesheet" TYPE="text/css" HREF="doc.css">
<META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.82">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<TITLE>ca65 Macros for Self Modifying Code</TITLE>
</HEAD>
<BODY>
<H1>ca65 Macros for Self Modifying Code</H1>
<H2>Christian Krüger</H2>
<HR>
<EM>The 'smc.inc' macro package for ca65 eases the use, increases the safeness and
self-explanation of 'self-modifying-code' (SMC).</EM>
<HR>
<P>
<H2><A NAME="toc1">1.</A> <A HREF="smc.html#s1">Overview</A></H2>
<P>
<H2><A NAME="toc2">2.</A> <A HREF="smc.html#s2">Usage</A></H2>
<UL>
<LI><A NAME="toc2.1">2.1</A> <A HREF="smc.html#ss2.1">Argument placeholders</A>
<LI><A NAME="toc2.2">2.2</A> <A HREF="smc.html#ss2.2">Accessing opcodes</A>
<LI><A NAME="toc2.3">2.3</A> <A HREF="smc.html#ss2.3">Accessing arguments</A>
<LI><A NAME="toc2.4">2.4</A> <A HREF="smc.html#ss2.4">Operational macros</A>
<LI><A NAME="toc2.5">2.5</A> <A HREF="smc.html#ss2.5">Scope macros</A>
</UL>
<P>
<H2><A NAME="toc3">3.</A> <A HREF="smc.html#s3">A complex example</A></H2>
<HR>
<H2><A NAME="s1">1.</A> <A HREF="#toc1">Overview</A></H2>
<P>When reading assembler sources, self modifying code is often hard to identify
and applying it needs a lot of discipline.</P>
<P>Since the cacheless 6502 is a thankful target of such kind of code, the macro
package will not only reduce this complexness, but also document the use. The
resulting source is more self-explanatory and so easier to maintain.</P>
<P>While for general purposes SMC is not a desired form for implementations, it
can be quite useful for a small range of scenarios. Normally SMC will be
introduced when optimizing code in respect to:</P>
<P>
<UL>
<LI>speed and/or</LI>
<LI>size.</LI>
</UL>
</P>
<P>Please mind that SMC can only be applied for code in RAM, which means that a
general purpose library with SMC excludes ROM targets!</P>
<P>The ca65 SMC macro package consists of two files:</P>
<P>
<UL>
<LI><CODE>smc.inc</CODE></LI>
<LI><CODE>opcodes.inc</CODE></LI>
</UL>
</P>
<P>The latter is only needed if you also plan to modify opcodes and not only data
within your code.</P>
<H2><A NAME="s2">2.</A> <A HREF="#toc2">Usage</A></H2>
<P>The use of the macros is quite simple:</P>
<P>Original:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
PHA
JSR SUBROUTINE
PLA
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>By applying SMC, the speed will now be increased by once cycle:</P>
<P>SMC:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
SMC_StoreValue RestoreAccu
JSR SUBROUTINE
SMC RestoreAccu, { LDA #SMC_Value }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>The first line stores the value of the accu into the '<CODE>RestoreAccu</CODE>'
labeled SMC target.</P>
<P>Please note:
<OL>
<LI> for all SMC store or transfer operations, a second argument can be
given. This determines the register for the operation:
'<CODE>SMC_StoreValue Label, y</CODE>' will store the value of the
Y-register.
If the second argument is missing, the accu will be used automatically.
</LI>
<LI> The label targets a 'special SMC namespace'. It fits only to
destinations which are introduced with the macro '<CODE>SMC</CODE>'. A
normal label '<CODE>RestoreAccu</CODE>' wouldn't match and could even
coexist (even if you should abstain from doing so).
</LI>
<LI> The macro '<CODE>SMC_StoreValue</CODE>' takes care, that the store
operation will occur on the value-position of a SMC-instruction. As
you will see, other macros influence other instruction part positions.
There is no consistency check, if the targeted SMC instruction actually
contains a value. Storing a 'value' on an immplied SMC instruction
would corrupt the following memory cell!</LI>
</OL>
</P>
<P>The second line needs no further explanation, this is just a placeholder for
some code in the example.</P>
<P>The third line is the code line which is about to be modified. It has to start
with the '<CODE>SMC</CODE>' macro and must be labeled, so that the modification
can be designated. Then the unmodified code is given in curly braces.</P>
<P>Please note the usage of the value placeholder 'SMC_Value'. Using such a
placeholder has two advantages:</P>
<P>
<OL>
<LI> The code is better documented. It is clearly visible that the given
value is about to be changed.</LI>
<LI> When examining an (initial) disassembly (e.g. in a debugger), these
placeholders can be better identified: They are fixed and, you may
notice that below, quite eye catching defined.</LI>
</OL>
</P>
<H2><A NAME="ss2.1">2.1</A> <A HREF="#toc2.1">Argument placeholders</A>
</H2>
<P>There are four kinds of placeholders:</P>
<P>
<DL>
<P>
<A NAME="Address placeholder"></A> </P>
<DT><B><CODE>SMC_AbsAdr</CODE></B><DD>
<P>Used to indicate an address. The value is '<CODE>$FADE</CODE>'.</P>
<P>Example: <CODE>STA SMC_AbsAdr</CODE></P>
<P>
<A NAME="Zero-Page-Address placeholder"></A> </P>
<DT><B><CODE>SMC_ZpAdr</CODE></B><DD>
<P>Used to indicate a zero-page-address. The value is '<CODE>$00</CODE>'.</P>
<P>Example: <CODE>LDA SMC_ZpAdr</CODE></P>
<P>
<A NAME="Opcode placeholder"></A> </P>
<DT><B><CODE>SMC_Opcode</CODE></B><DD>
<P>Used to indicate an instruction. The value is '<CODE>NOP</CODE>'.</P>
<P>Example: <CODE>SMC_Opcode</CODE></P>
<P>
<A NAME="Immediate value placeholder"></A> </P>
<DT><B><CODE>SMC_Value</CODE></B><DD>
<P>Used to indicate a value. The value is '<CODE>$42</CODE>'.</P>
<P>Example: <CODE>LDX #SMC_Value</CODE></P>
</DL>
</P>
<P>Attention: Often code is modified after the initial use - where using the
placeholders does not makes sense. Please mind also, that in very variable
expressions (e.g. opcode and argument is about to be changed), placeholders
can lead to unidentifyable code for a debugger/disassembler:</P>
<P><CODE>SMC Example, { SMC_Opcode SMC_AbsAdr } </CODE></P>
<P>Since the opcode is '<CODE>NOP</CODE>', the value '<CODE>$DE</CODE>' from '<CODE>$FADE</CODE>' will
interpreted as opcode in a disassembler too. This breaks the correct
disassembly, because '<CODE>$DE</CODE>' is interpreted as '<CODE>DEC abx</CODE>'. Establishing
a valid placeholder instruction may be better:</P>
<P><CODE>SMC Example, { sta SMC_AbsAdr } ; Note: Opcode will be modified too!</CODE></P>
<H2><A NAME="ss2.2">2.2</A> <A HREF="#toc2.2">Accessing opcodes</A>
</H2>
<P>Some macros are designed to access the instruction of a code line. To increase
readability, please use the opcodes as defined in the '<CODE>opcodes.inc</CODE>'
file.</P>
<P>
<DL>
<P>
<A NAME="Transfer opcode"></A> </P>
<DT><B><CODE>SMC_TransferOpcode label, opcode (, register)</CODE></B><DD>
<P>Loads and store an opcode to given SMC instruction.</P>
<P>Example:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
SMC SumRegister, { LDA #10 }
JSR OUTPUT
SMC_TransferOpcode SumRegister, OPC_ADC_imm, x
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>The macro above will load the opcode '<CODE>ADC #</CODE>' into the x - register
and stores it at the place of the '<CODE>LDA #</CODE>'.</P>
<P>
<A NAME="Load opcode"></A> </P>
<DT><B><CODE>SMC_LoadOpcode label (, register)</CODE></B><DD>
<P>Loads the opcode of a SMC line to the given register.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
SMC ShiftOrNothing, { LSL }
SMC_LoadOpcode ShiftOrNothing, y
CPY #OPC_NOP
BEQ Exit
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Store opcode"></A> </P>
<DT><B><CODE>SMC_StoreOpcode label (, register)</CODE></B><DD>
<P>Stores the value of the given register at the opcode place of a SMC line.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
SetBoldMode:
LDA #OPC_INX
SMC_StoreOpcode AdaptCharWidth
SMC_StoreOpcode AdaptUnderlineWidth
RTS
...
SMC AdaptCharWidth, { NOP }
...
SMC AdaptUnderlineWidth, { NOP }
</PRE>
</CODE></BLOCKQUOTE>
</P>
</DL>
</P>
<H2><A NAME="ss2.3">2.3</A> <A HREF="#toc2.3">Accessing arguments</A>
</H2>
<P>These marcos are determined to get, set and change arguments of instructions:</P>
<P>
<DL>
<P>
<A NAME="Change branch"></A> </P>
<DT><B><CODE>SMC_ChangeBranch label, destination (, register)</CODE></B><DD>
<P>Used to modify the destination of a branch instruction. If the address offset
exceeds the supported range of 8-bit of the 6502, a error will be thrown.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
Disable Handler:
SMC_ChangeBranch BranchToHandler, Exit
RTS
...
LDA warning
SMC BranchToHandler, { BNE Handler }
Exit:
RTS
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Transfer value"></A> </P>
<DT><B><CODE>SMC_TransferValue label, value (, register)</CODE></B><DD>
<P>Changes the value of a SMC line.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
ClearDefault:
SMC_TransferValue LoadDefault, 0
RTS
...
SMC LoadDefault, { LDX #25 }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Load value"></A> </P>
<DT><B><CODE>SMC_LoadValue label (, register)</CODE></B><DD>
<P>Retrieves the value of a SMC line.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
ShowDefault:
SMC_LoadValue LoadDefault
JSR PrintValue
RTS
...
SMC LoadDefault, { LDX #25 }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Store value"></A> </P>
<DT><B><CODE>SMC_StoreValue label (, register)</CODE></B><DD>
<P>Stores the value in the register to given SMC line.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
InitCounters:
LDY #0
SMC_StoreValue GetI, y
SMC_StoreValue GetJ, y
SMC_StoreValue GetK, y
...
SMC GetI, { LDX #SMC_Value }
...
SMC GetJ, { LDX #SMC_Value }
...
SMC GetK, { LDX #SMC_Value }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Transfer low-byte"></A> </P>
<DT><B><CODE>SMC_TransferLowByte label, value (, register)</CODE></B><DD>
<P>Does the same as '<CODE>SMC_TransferValue</CODE>' but should be used for
low-bytes of addresses for better readability.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
ActivateSecondDataSet:
SMC_TransferLowByte LoadData, $40
RTS
...
SMC LoadData, { LDA $2000 }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Load low-byte"></A> </P>
<DT><B><CODE>SMC_LoadLowByte label (, register)</CODE></B><DD>
<P>Does the same as '<CODE>SMC_LoadValue</CODE>' but should be used for low-bytes
of addresses for better readability.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
IsSecondDataSetActive:
SMC_LoadLowByte LoadData, y
CPY #$40
BNE NotActive
...
SMC LoadData, { LDA $2000 }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Store low-byte"></A> </P>
<DT><B><CODE>SMC_StoreLowByte label (, register)</CODE></B><DD>
<P>Does the same as '<CODE>SMC_StoreValue</CODE>' but should be used for low-bytes
of addresses for better readability.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
InitStructureBaseAddresses:
LDX #0
SMC_StoreLowByte GetPlayerGraphic, x
SMC_StoreLowByte GetObjectGraphic, x
SMC_StoreLowByte StoreCollisionData, x
RTS
...
SMC GetPlayerGraphic, { LDX $2000 }
...
SMC GetObjectGraphic, { LDA $2100,x }
...
SMC StoreCollisionData, { STY $2200 }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Transfer high-byte"></A> </P>
<DT><B><CODE>SMC_TransferHighByte label, value (, register)</CODE></B><DD>
<P>Loads and stores the given value via the named register to the high-byte
address portion of an SMC-instruction.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
PlaySFX:
SMC GetVolume { LDA $3200,x }
STA SoundOut
INX
BNE PlaySFX
...
PlayOtherSound:
SMC_TransferHighByte GetVolume, $34
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Load high-byte"></A> </P>
<DT><B><CODE>SMC_LoadHighByte label (, register)</CODE></B><DD>
<P>Loads the high-byte part of an SMC-instruction address to the given register.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
PlaySFX:
SMC GetVolume { LDA $3200,x }
...
SMC_LoadHighByte GetVolume
cmp #$34
beq OtherSoundPlaying
...
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Store high-byte"></A> </P>
<DT><B><CODE>SMC_StoreHighByte label (, register)</CODE></B><DD>
<P>Stores the high-byte address part of an SMC-instruction from the given
register.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
SetupLevel2:
LDX #(>Level2Base)
SMC_StoreHighByte GetLevelData, x
SMC_StoreHighByte GetScreenData, x
SMC_StoreHighByte GetSoundData, x
RTS
...
SMC GetLevelData, { LDA Level1Base+Data }
...
SMC GetScreenData, { LDA Level1Base+Screen, x }
...
SMC GetSoundData, { LDA Level1Base+Sound, y }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Transfer single address"></A> </P>
<DT><B><CODE>SMC_TransferAddressSingle label, address (, register)</CODE></B><DD>
<P>Transfers the contents of the given address via the given register to the
designated SMC instruction.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
PrintHello:
SMC_TransferAddressSingle GetChar, #HelloMsg
...
LDX #0
NextChar:
SMC GetChar, { LDA SMC_AbsAdr, x }
BEQ leave
JSR CharOut
INX
BNE NextChar
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Transfer address"></A> </P>
<DT><B><CODE>SMC_TransferAddress label, address</CODE></B><DD>
<P>Loads contents of given address to A/X and stores the result to SMC
instruction. Allows reuse of register contents by using
'<CODE>SMC_StoreAddress</CODE>' for multiple SMC instruction modifications.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
SMC_TransferAddress JumpTo, #CloseChannel
...
SMC JumpTo, { JMP OpenChannel }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Store address"></A> </P>
<DT><B><CODE>SMC_StoreAddress label</CODE></B><DD>
<P>Stores the address value in a/x to a SMC instruction address position.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
SMC_StoreAddress GetData
...
SMC GetData, { LDA SMC_AbsAdr }
</PRE>
</CODE></BLOCKQUOTE>
</P>
</DL>
</P>
<H2><A NAME="ss2.4">2.4</A> <A HREF="#toc2.4">Operational macros</A>
</H2>
<P>These marcos are determined to let read/modify/write opcodes work on parts of
SMC instructions.</P>
<P>
<DL>
<P>
<A NAME="Operate on value"></A> </P>
<DT><B><CODE>SMC_OperateOnValue opcode, label</CODE></B><DD>
<P>Let given opcode work on the value part of a SMC instruction.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
SMC_OperateOnValue ASL, LoadMask ; shift mask to left
...
SMC LoadMask, { LDA #$20 }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Operate on low-byte"></A> </P>
<DT><B><CODE>SMC_OperateOnLowByte opcode, label</CODE></B><DD>
<P>Same as '<CODE>SMC_OperateOnValue</CODE>' but renamed for better readability when
accessing low-bytes of address.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
SMC_OperateOnLowByte DEC, AccessData
...
SMC AccessData, { LDX Data }
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Operate on high-byte"></A> </P>
<DT><B><CODE>SMC_OperateOnHighByte opcode, label</CODE></B><DD>
<P>Let the given opcode work on the high-byte part on a SMC-instruction.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
NextPage:
SMC_OperateOnHighByte INC, GetPageData
...
SMC GetPageData, { LDA SourceData, X }
</PRE>
</CODE></BLOCKQUOTE>
</P>
</DL>
</P>
<H2><A NAME="ss2.5">2.5</A> <A HREF="#toc2.5">Scope macros</A>
</H2>
<P>These marcos are determined to export and import SMC labels out of the current
file scope. Please handle with care! If you cannot abstain from leaving the
file scope, you should at least document the exported SMC lines very well. On
import side no checking is available if the SMC line is correct accessed (e.g.
invalid access to the value of an implied instruction)!</P>
<P>
<DL>
<P>
<A NAME="Export SMC line under given name"></A> </P>
<DT><B><CODE>SMC_Export alias, label</CODE></B><DD>
<P>SMC label will be exported under given alias.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
.proc GetValue
SMC LoadValue, { LDA #12 }
rts
.endproc
SMC_Export GetValueLoader, GetValue::LoadValue
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>
<A NAME="Import SMC alias"></A> </P>
<DT><B><CODE>SMC_Import alias</CODE></B><DD>
<P>SMC line is made accessible under given alias.</P>
<P>Example:
<BLOCKQUOTE><CODE>
<PRE>
SMC_Import GetValueLoader
...
SMC_TransferValue GetValueLoader, #47
...
</PRE>
</CODE></BLOCKQUOTE>
</P>
</DL>
</P>
<H2><A NAME="s3">3.</A> <A HREF="#toc3">A complex example</A></H2>
<P>Let's have a look on a quite sophisticated example for the usage of SMC. It
not only modifies code, but also the modification of the code is modified -
allowing reuse of some instructions.</P>
<P>
<DL>
<DT><B>The code is from my 'memset()'implementation:</B><DD>
<P>
<BLOCKQUOTE><CODE>
<PRE>
1: ...
2: SMC_StoreAddress StoreAccuFirstSection
3:
4: StoreToFirstSection:
5: SMC StoreAccuFirstSection, { sta SMC_AbsAdr, Y }
6: ...
7: RestoreCodeBranchBaseAdr:
8: SMC FirstIncHighByte, { SMC_OperateOnHighByte inc, StoreAccuFirstSection } ; code will be overwritten to 'beq RestoreCode' (*)
9: ...
10: SMC_TransferOpcode FirstIncHighByte, OPC_BEQ , x ; change code marked above with (*)
11: SMC_TransferValue FirstIncHighByte, #(restoreCode - RestoreCodeBranchBaseAdr-2), x ; set relative address to 'RestoreCode'
12: ...
13: restoreCode:
14: SMC_TransferOpcode FirstIncHighByte, OPC_INC_abs , x ; restore original code...
15: SMC_TransferValue FirstIncHighByte, #(<(StoreToFirstSection+2)), x ; (second byte of inc contained low-byte of address)
16: ...
</PRE>
</CODE></BLOCKQUOTE>
</P>
<DT><B>Some explanation:</B><DD>
<P>Line 2: The register pair A/X contains an address, which is stored on the
address location of a SMC line called 'StoreAccuFirstSection'. According to
cc65's calling convention, the low-byte is in accu while the high-byte is in
the X-register.</P>
<P>Line 5: The (modified) address is accessed.</P>
<P>Line 8: We have a line here, which is about to be modified (it begins with
SMC), but itself modifies code. Please note: Contrary to the rest of SMC-line
modifying macros, the 'OperateOn'-macros just expand their given arguments
into a single instruction line. These can be changed of course too.</P>
<P>Line 10,11: These lines construct a branch operation for line 8: The
X-register will be used to change it from 'inc StoreAccuFirstSection+2'
(high-byte operation) to 'beq restoreCode'. Please note: To calculate the
relative branch offset, we introduced a second label
('RestoreCodeBranchBaseAdr') for to calculate it. Some could also use the
internal name of the SMC label, but you should abstain to do so - it may be
changed in the future...</P>
<P>Line 14,15: The original code from line 8 is reestablished.</P>
</DL>
</P>
</BODY>
</HTML>