forked from davidgiven/ack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6500.doc
1893 lines (1888 loc) · 42.5 KB
/
6500.doc
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
. \" $Id$"
.RP
.ND Dec 1984
.TL
.B
A backend table for the 6500 microprocessor
.R
.AU
Jan van Dalen
.AB
The backend table is part of the Amsterdam Compiler Kit (ACK).
It translates the intermediate language family EM to a machine
code for the MCS6500 microprocessor family.
.AE
.bp
.DS C
.B
THE MCS6500 MICROPROCESSOR.
.R
.DE
.NH 0
Introduction
.PP
Why a back end table for the MCS6500 microprocessor family.
Although the MCS6500 microprocessor family has an simple
instruction set and internal structure, it is used in a
variety of microcomputers and homecomputers.
This is because of is low cost.
As an example the Apple II, a well known and width spread
microprocessor, uses the MCS6502 CPU.
Also the BBC homecomputer, whose popularity is growing day
by day uses the MCS6502 CPU.
The BBC homecomputer is based on the MCS6502 CPU although
better and stronger microprocessors are available.
The designers of Acorn computer Industries have probably
choosen for the MCS6502 because of the amount of software
available for this CPU.
Since its width spreaded use, a variaty of software
will be needed for it.
One can think of games!!, administration programs,
teaching programs, basic interpreters and other application
programs.
Even do it will not be possible to run the total compiler kit
on a MCS6500 based computer, it is possible to write application
programs in a high level language, such as Pascal or C on a
minicomputer.
These application programs can be tested and compiled on that
minicomputer and put in a ROM (Read Only Memory), for example,
cso that it an be executed by a MCS6500 CPU.
The strategy of writing testprograms on a minicomputer,
compile it and then execute it on a MCS6500 based
microprocessor is used by the development of the back end.
The minicomputer used is M68000 based one, manufactured by
Bleasdale Computer Systems Ltd..
The micro- or homecomputer used is a BBC microcomputer,
manufactured by Acorn Computer Ltd..
.NH
The MOS Technology MCS6500
.PP
The MCS6500 is as a family of CPU devices developed by MOS
Technology [1].
The members of the MCS6500 family are the same chips in a
different housing.
The MCS6502, the big brother in the family, can handle 64k
bytes of memory, while for example the MCS6504 can only handle
8k bytes of memory.
This difference is due to the fact that the MCS6502 is in a
40 pins house and the MCS6504 has a 28 pins house, so less
address lines are available.
.bp
.NH
The MCS6500 CPU programmable registers
.PP
The MCS6500 series is based on the same chip so all have the
same programmable registers.
.sp 9
.NH 2
The accumulator A.
.PP
The accumulator A is the only register on which the arithmetic
and logical instructions can be used.
For example, the instruction ADC (add with carry) adds the
contents of the accumulator A and a byte from memory or data.
.NH 2
The index register X.
.PP
As the name suggests this register can be used for some
indirect addressing modes.
The modes are explaned below.
.NH 2
The index register Y.
.PP
This register is, just as the index register X, used for
certain indirect addressing modes.
These addressing modes are different from the modes which
use index register X.
.NH 2
The program counter PC
.PP
This is the only 16-bit register available.
It is used to point to the next instruction to be
carried out.
.NH 2
The stack pointer SP
.PP
The stack pointer is an 8-bit register, so the stack can contain
at most 256 bytes.
The CPU always appends 00000001 as highbyte of any stack address,
which means that memory locations
.B
0100
.R
through
.B
01FF
.R
are permanently assigned to the stack.
.sp 12
.NH 2
The status register
.PP
The status register maintains six status flags and a master
interrupt control bit.
.br
These are the six status flags:
Carry (c)
Zero (z)
Overflow (o)
Sign (n)
Decimal mode (d)
Break (b)
The bit (i) is the master interrupt control bit.
.NH
The MCS6500 memory layout.
.PP
In the MCS6500 memory space three area's have special meaning.
These area's are:
.IP 1)
Top page.
.IP 2)
Zero page.
.IP 3)
The stack.
.PP
MCS6500 memory is divided up into pages.
These pages consist 256 bytes.
So in a memory address the highbyte denotes the page number
and the lowbyte the offset within the page.
.NH 2
Top page.
.PP
When a MCS6500 is restared it jumps indirect via memory address
.B
FFFC.
.R
At
.B
FFFC
.R
(lowbyte) and
.B
FFFD
.R
(highbyte) there must be the address of the bootstrap subroutine.
When a break instruction (BRK) occurs or an interrupt takes place,
the MCS6500 jumps indirect through memory address
.B
FFFE.
.R
.B
FFFE
.R
and
.B
FFFF
.R
thus, must contain the address of the interrupt routine.
The former only goes for maskeble interrupt.
There also exist a nonmaskeble interrupt.
This cause the MCS6500 to jump indirect through memory address
.B
FFFA.
.R
So the top six bytes of memory are used by the operating system
and therefore not available for the back end.
.NH 2
Zero page.
.PP
This page has a special meaning in the sence that addressing
this page uses special opcodes.
Since a page consists of 256 bytes, only one byte is needed
for addressing zero page.
So an instruction which uses zero page occupies two bytes.
It also uses less clock cycle's while carrying out the instruction.
Zero page is also needed when indirect addressing is used.
This means that when indirect addressing is used, the address must
reside in zero page (two consecutive bytes).
In this case (the back end), zero page is used, for example
to hold the local base, the second local base, the stack pointer
etc.
.NH 2
The stack.
.PP
The stack is described in paragraph 3.5 about the MCS6500
programmable registers.
.NH
The memory adressing modes
.PP
MCS6500 memory reference instructions use direct addressing,
indexed addressing, and indirect addressing.
.NH 2
direct addressing.
.PP
Three-byte instructions use the second and third bytes of the
object code to provide a direct 16-bit address:
therefore, 65.536 bytes of memory can be addressed directly.
The commonly used memory reference instructions also have a two-byte
object code variation, where the second byte directly addresses
one of the first 256 bytes.
.NH 2
Base page, indexed addressing.
.PP
In this case, the instruction has two bytes of object code.
The contents of either the X or Y index registers are added to the
second object code byte in order to compute a memory address.
This may be illustrated as follows:
.sp 15
Base page, indexed addressing, as illustrated above, is
wraparound - which means that there is no carry.
If the sum of the index register and second object code byte contents
is more than
.B
FF
.R
, the carry bit will be dicarded.
This may be illustrated as follows:
.sp 9
.NH 2
Absolute indexed addressing.
.PP
In this case, the contents of either the X or Y register are added
to a 16-bit direct address provided by the second and third bytes
of an instruction's object code.
This may be illustrated as follows:
.sp 10
.NH 2
Indirect addressing.
.PP
Instructions that use simple indirect addressing have three bytes of
object code.
The second and third object code bytes provide a 16-bit address;
therefore, the indirect address can be located anywhere in
memory.
This is straightforward indirect addressing.
.NH 3
Pre-indexed indirect addressing.
.PP
In this case, the object code consists of two bytes and the
second object code byte provides an 8-bit address.
Instructions that use pre-indexed indirect addressing add the contents
of the X index register and the second object code byte to access
a memory location in the first 256 bytes of memory, where the
indirect address will be found:
.sp 18
When using pre-indexed indirect addressing, once again wraparound
addition is used, which means that when the X index register contents
are added to the second object code byte, any carry will be discarded.
Note that only the X index register can be used with pre-indexed
addressing.
.NH 3
Post-indexed indirect addressing.
.PP
In this case, the object code consists of two bytes and the
second object code byte provides an 8-bit address.
Now the second object code byte indentifies a location
in the first 256 bytes of memory where an indirect address
will be found.
The contents of the Y index register are added to this indirect
address.
This may be illustrated as follows:
.sp 18
Note that only the Y index register can be used with post-indexed
indirect addressing.
.bp
.NH
What the CPU has and doesn't has.
.PP
Although the designers of the MCS6500 CPUs family state that
there is nothing very significant about the short stack (only
256 bytes) this stack caused problems for the back end.
The designers say that a 256-byte stack usually is sufficient
for any typical microcomputer, this is only true if the stack
is used only for return addresses of the JSR (jump to
subroutine) instruction.
But since the EM machine is suppost to be a stack machine and
high level languages need the ability of parameters and
locals in there procedures and function, this short stack
is unsufficiant.
So an software stack is implemented in this back end, requiring two
additional subroutines for stack handling.
These two stack handling subroutines slow down the processing time
of a program since the stack is used heavely.
.PP
Since parameters and locals of EM procedures are offseted
from the localbase of that procedure, indirect addressing
is havily used.
Offsets are positive (for parameters) and negative (for
local variables).
As explaned before the addressing modes the MCS6500 have a
post indexed indirect addressing mode.
This addressing mode can only handle positive offsets.
This raises a problem for accessing the local variables
I have chosen for the next solution.
A second local base is introduced.
This second local base is the real local base subtracted by
a constant BASE.
In the present situation of the back end the value of BASE
is 240.
This means that there are 240 bytes reseved for local
variables to be indirect addressed and 14 bytes for
the parameters.
.DS C
.B
THE CODE GENERATOR.
.R
.DE
.NH 0
Description of the machine table.
.PP
The machine description table consists of the following sections:
.IP 1.
The macro definitions.
.IP 2.
Constant definitions.
.IP 3.
Register definitions.
.IP 4.
Token definitions.
.IP 5.
Token expressions.
.IP 6.
Code rules.
.IP 7.
Move definitions.
.IP 8.
Test definitions.
.IP 9.
Stack definitions.
.NH 2
Macro definitions.
.PP
The macro definitions at the top of the table are expanded
by the preprocessor on occurence in the rest of the table.
.NH 2
Constant definitions.
.PP
There are three constants which must be defined at first.
The are:
.IP EM_WSIZE: 11
Number of bytes in a machine word.
This is the number of bytes a simple
.B
loc
.R
instruction will put on the stack.
.IP EM_PSIZE:
Number of bytes in a pointer.
This is the number of bytes a
.B
lal
.R
instruction will put on the stack.
.IP EM_BSIZE:
Number of bytes in the hole between AB and LB.
The calling sequence only saves LB on the stack so this
constant is equal to the pointer size.
.NH 1
Register definitions.
.PP
The only important register definition is the definition of
the registerpair AX.
Since the rest of the machine's registers Y, PC, ST serve
special purposes, the code generator cannot use them.
.NH 2
Token definitions
.PP
There is a fake token.
This token is put in the table, since the code generator generator
complains if it cannot find one.
.NH 2
Token expression definitions.
.PP
The token expression is also a fake one.
This token expression is put in the table, since the code generator
generator complains if it cannot find one.
.NH 2
Code rules.
.PP
The code rule section is the largest section in the table.
They specify EM patterns, stack patterns, code to be generated,
etc.
The syntax is:
.IP "code rule:"
EM pattern '|' stack pattern '|' code '|'
stack replacement '|' EM replacement '|'
.PP
All patterns are optional, however there must be at least one
pattern present.
If the EM pattern is missing the rule becomes a rewriting
rule or a
.B
coercion
.R
to be used when code generation cannot continue because of an
invalid stack pattern.
The code rules are preceeded by the word CODE:.
.NH 3
The EM pattern.
.PP
The EM pattern consists of a list of EM mnemonics followed by
a boolean expression. Examples:
.sp 1
.br
.B
loe
.R
.sp 1
will match a single
.B
loe
.R
instruction,
.sp 1
.br
.B
loc loc cif
.R
$1==2 && $2==8
.sp 1
is a pattern that will match
.sp 1
.br
.B
loc
.R
2
.br
.B
loc
.R
8
.br
.B
cif
.R
.sp 1
and
.sp 1
.br
.B
lol
inc
stl
.R
$1==$3
.sp 1
will match for example
.sp 1
.br
.B
lol
.R
6
.br
.B
inc
.R
.br
.B
stl
.R
6
.sp 1
A missing boolean expession evaluates to TRUE.
.PP
The code generator will match the longest EM pattern on every occasion,
if two patterns of the same length match the first in the table
will be chosen, while all patterns of length greater than or equal
to three are considered to be of the same length.
.NH 3
The stack pattern.
.PP
The only stack pattern that can occur is R16, which means that the
registerpair AX contains the word on top of the stack.
If this is not the case a coersion occurs.
This coersion generates a "jsr Pop", which means that the top
of the stack is popped and stored in the registerpair AX.
.NH 3
The code part.
.PP
The code part consists of three parts, stack cleanup, register
allocation, and code to be generated.
All of these may be omitted.
.NH 4
Stack cleanup.
.PP
When generating something like a branch instruction it might be
needed to empty the fake stack, that is, remove the AX registerpair.
This is done by the instruction remove(ALL)
.NH 4
Register allocation.
.PP
If the machine code to be generated uses the registerpair AX,
this is signaled to the code generator by the allocate(R16)
instruction.
If the registerpair AX resides on the fake stack, this will result
in a "jsr Push", which means that the registerpair AX is pushed on
the stack and will be free for further use.
If registerpair AX is not on the fake stack nothing happens.
.NH 4
Code to be generated.
.PP
Code to be generated is specified as a list of items of the following
kind:
.IP 1)
A string in double quotes("This is a string").
This is copied to the codefile and a newline ('\n') is appended.
Inside the string all normal C string conventions are allowed,
and substitutions can be made of the following sorts.
.RS
.IP a)
$1, $2 etc. These are the operand of the corresponding EM
instructions and are printed according to there type.
To put a real '$' inside the string it must be doubled ('$$').
.IP b)
%[1], %[2.reg], %[b.1] etc. these have there obvious meaning.
If they describe a complete token (%[1]) the printformat for
the token is used.
If they stand fo a basic term in an expression they will be
printed according to their type.
To put a real '%' inside the string it must be doubled ('%%').
.IP c)
%( arbitrary expression %). This allows inclusion of arbitrary
expressions inside strings.
Usually not needed very often, so that the akward notation
is not too bad.
Note that %(%[1]%) is equivalent to %[1].
.RE
.NH 3
stack replacement.
.PP
The stack replacement is a possibly empty list of items to be
pushed on the fake stack.
Three things can occur:
.IP 1)
%[1] is used if the registerpair AX was on the fake stack and is
to be pushed back onto it.
.IP 2)
%[a] is used if the registerpair AX is allocated with allocate(R16)
and is to be pushed onto the fake stack.
.IP 3)
It can also be empty.
.NH 3
EM replacement.
.PP
In exeptional cases it might be useful to leave part of the an EM
pattern undone.
For example, a
.B
sdl
.R
instruction might be split into two
.B
stl
.R
instructions when there is no 4-byte quantity on the stack.
The EM replacement part allows one to express this.
Example:
.sp 1
.br
.B
stl
.R
$1
.B
stl
.R
$1+2
.sp 1
The instructions are inserted in the stream so they can match
the first part of a pattern in the next step.
Note that since the code generator traverses the EM instructions
in a strict linear fashion, it is impossible to let the EM
replacement match later parts of a pattern.
So if there is a pattern
.sp 1
.br
.B
loc
stl
.R
$1==0
.sp 1
and the input is
.sp 1
.br
.B
loc
.R
0
.B
sdl
.R
4
.sp 1
the
.B
loc
.R
0
will be processed first, then the
.B
sdl
.R
might be split into two
.B
stl
.R
's but the pattern cannot match now.
.NH 3
Move definitions.
.PP
This definition is a fake. This definition is put in the
table, since the code generator generator complains if it
cannot find one.
.NH 3
Test definitions.
.PP
Test definitions aren't used by the table.
.NH 3
Stack definitions.
.PP
When the generator has to push the registerpair AX, it must
know how to do so.
The machine code to be generated is defined here.
.NH 1
Some remarks.
.PP
The above description of the machine table is
a description of the table for the MCS6500.
It uses only a part of the possibilities which the code generator
generator offers.
For a more precise and detailed description see [2].
.DS C
.B
THE BACK END TABLE.
.R
.DE
.NH 0
Introduction.
.PP
The code rules are divided in 15 groups.
These groups are:
.IP 1.
Load instructions.
.IP 2.
Store instructions.
.IP 3.
Integer arithmetic instructions.
.IP 4.
Unsigned arithmetic instructions.
.IP 5.
Floating point arithmetic instructions.
.IP 6.
Pointer arithmetic instructions.
.IP 7.
Increment, decrement and zero instructions.
.IP 8.
Convert instructions.
.IP 9.
Logical instructions.
.IP 10.
Set manipulation instructions.
.IP 11.
Array instructions.
.IP 12.
Compare instructions.
.IP 13.
Branch instructions.
.IP 14.
Procedure call instructions.
.IP 15.
Miscellaneous instructions.
.PP
From all of these groups one or two typical EM pattern will be explained
in the next paragraphs.
Comment is placed between /* and */ (/* This is a comment */).
.NH
The instructions.
.NH 2
The load instructions.
.PP
In this group a typical instruction is
.B
lol
.R
.
A
.B
lol
.R
instruction pushes the word at local base + offset, where offset
is the instructions argument, onto the stack.
Since the MCS6500 can only offset by 256 bytes, as explaned at the
memory addressing modes, there is a need for two code rules in the
table.
One which can offset directly and one that must explicit
calculate the address of the local.
.NH 3
The lol instruction with indirect offsetting.
.PP
In this case an indirect offsetted load from the second local base
is possible.
The table content is:
.sp 1
.br
.B
lol
.R
IN($1) | |
.br
allocate(R16) /* allocate registerpair AX */
.br
"ldy #BASE+$1" /* load Y with the offset from the second
.br
local base */
.br
"lda (LBl),y" /* load indirect the lowbyte of the word */
.br
"tax" /* move register A to register X */
.br
"iny" /* increment register Y (offset) */
.br
"lda (LBl),y" /* load indirect the highbyte of the word */
.br
| %[a] | | /* push the word onto the fake stack */
.NH 3
The lol instruction whose offset is to big.
.PP
In this case, the library subroutine "Lol" is used.
This subroutine expects the offset in registerpair AX, then
calculates the address of the local or parameter, and loads
it into registerpair AX.
The table content is:
.sp 1
.br
.B
lol
.R
| |
.br
allocate(R16) /* allocate registerpair AX */
.br
"lda #[$1].h" /* load highbyte of offset into register A */
.br
"ldx #[$1].l" /* load lowbyte of offset into register X */
.br
"jsr Lol" /* perform the subroutine */
.br
| %[a] | | /* push word onto the fake stack */
.NH 2
The store instructions.
.PP
In this group a typical instruction is
.B
stl.
.R
A
.B
stl
.R
instruction poppes a word from the stack and stores it in the word
at local base + offset, where offset is the instructions argument.
Here also is the need for two code rules in the table as a result
of the offset limits.
.NH 3
The stl instruction with indirect offsetting.
.PP
In this case it an indirect offsetted store from the second local
base is possible.
The table content is:
.sp 1
.br
.B
stl
.R
IN($1) | R16 | /* expect registerpair AX on top of the
.br
fake stack */
.br
"ldy #BASE+1+$1" /* load Y with the offset from the
.br
second local base */
.br
"sta (LBl),y" /* store the highbyte of the word from A */
.br
"txa" /* move register X to register A */
.br
"dey" /* decrement offset */
.br
"sta (LBl),y" /* store the lowbyte of the word from A */
.br
| | |
.NH 3
The stl instruction whose offset is to big.
.PP
In this case the library subroutine 'Stl' is used.
This subroutine expects the offset in registerpair AX, then
calculates the address, poppes the word stores it at its place.
The table content is:
.sp 1
.br
.B
stl
.R
| |
.br
allocate(R16) /* allocate registerpair AX */
.br
"lda #[$1].h" /* load highbyte of offset in register A */
.br
"ldx #[$1].l" /* load lowbyte of offset in register X */
.br
"jsr Stl" /* perform the subroutine */
.br
| | |
.NH 2
Integer arithmetic instructions.
.PP
In this group typical instructions are
.B
adi
.R
and
.B
mli.
.R
These instructions, in this table, are implemented for 2-byte
and 4-byte integers.
The only arithmetic instructions available on the MCS6500 are
the ADC (add with carry), and SBC (subtract with not(carry)).
Not(carry) here means that in a subtraction, the one's complement
of the carry is taken.
The absence of multiply and division instructions forces the
use of subroutines to handle these cases.
Because there are no registers left to perform on the multiply
and division, zero page is used here.
The 4-byte integer arithmetic is implemented, because in C there
exists the integer type long.
A user is freely to use the type long, but will pay in performance.
.NH 3
The adi instruction.
.PP
In case of the
.B
adi
.R
2 (and
.B
sbi
.R
2) instruction there are many EM
patterns, so that the instruction can be performed in line in
most cases.
For the worst case there exists a subroutine in the library
which deals with the EM instruction.
In case of a
.B
adi
.R
4 (or
.B
sbi
.R
4) there only is a subroutine to deal with it.
A table content is:
.sp 1
.br
.B
lol lol adi
.R
(IN($1) && IN($2) && $3==2) | | /* is it in range */
.br
allocate(R16) /* allocate registerpair AX */
.br
"ldy #BASE+$1+1" /* load Y with offset for first operand */
.br
"lda (LBl),y" /* load indirect highbyte first operand */
.br
"pha" /* save highbyte first operand on hard_stack */
.br
"dey" /* decrement offset first operand */
.br
"lda (LBl),y" /* load indirect lowbyte first operand */
.br
"ldy #BASE+$2" /* load Y with offset for second operand */
.br
"clc" /* clear carry for addition */
.br
"adc (LBl),y" /* add the lowbytes of the operands */
.br
"tax" /* store lowbyte of result in place */
.br
"iny" /* increment offset second operand */
.br
"pla" /* get highbyte first operand */
.br
"adc (LBl),y" /* add the highbytes of the operands */
.br
| %[a] | | /* push the result onto the fake stack */
.NH 3
The mli instruction.
.PP
The
.B
mli
.R
2 instruction uses most the subroutine 'Mlinp'.
This subroutine expects the multiplicand in zero page
at locations ARTH, ARTH+1, while the multiplier is in zero
page locations ARTH+2, ARTH+3.
For a description of the algorithms used for multiplication and
division, see [3].
A table content is:
.sp 1
.br
.B
lol lol mli
.R
(IN($1) && IN($2) && $3==2) | |
.br
allocate(R16) /* allocate registerpair AX */
.br
"ldy #BASE+$1" /* load Y with offset of multiplicand */
.br
"lda (LBl),y" /* load indirect lowbyte of multiplicand */
.br
"sta ARTH" /* store lowbyte in zero page */
.br
"iny" /* increment offset of multiplicand */
.br
"lda (LBl),y" /* load indirect highbyte of multiplicand */
.br
"sta ARTH+1" /* store highbyte in zero page */
.br
"ldy #BASE+$2" /* load Y with offset of multiplier */
.br
"lda (LBl),y" /* load indirect lowbyte of multiplier */
.br
"sta ARTH+2" /* store lowbyte in zero page */
.br
"iny" /* increment offset of multiplier */
.br
"lda (LBl),y" /* load indirect highbyte of multiplier */
.br
"sta ARTH+3" /* store highbyte in zero page */
.br
"jsr Mlinp" /* perform the multiply */
.br
| %[a] | | /* push result onto fake stack */
.NH 2
The unsgned arithmetic instructions.
.PP
Since unsigned addition an subtraction is performed in the same way
as signed addition and subtraction, these cases are dealt with by
an EM replacement.
For mutiplication and division there are special subroutines.
.NH 3
Unsigned addition.
.PP
This is an example of the EM replacement strategy.
.sp 1
.br
.B
lol lol adu
.R
| | | |
.B
lol
.R
$1
.B
lol
.R
$2