forked from jmatzen/leventhal-6809
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6809_MAXELM.s
104 lines (97 loc) · 2.36 KB
/
6809_MAXELM.s
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
.macro CLC
ANDCC #$FE
.endm
.macro SEC
ORCC #$01
.endm
; Title: Find Maximum byte-length Element
; Name: MAXELM
;
; Purpose: Given the base address and size of an array,
; find the Largest element.
;
; Entry: Register X = Base address of array
; Register A = Size of array in bytes
;
; Exit: If size of array not zero then Carry flag = 0
; Register A = Largest element
; Register X = Address of that element
;
; If there are duplicate values of the largest element,
; register X contains the address nearest to the base address.
; else Carry flag = 1
;
; Registers Used: A,B,CC,X,Y
;
; Time: Approximately 14 to 26 cycles per byte
; plus 27 cycles overhead
;
; Size: Program 25 bytes
;
MAXELM:
;
; EXIT WITH CARRY SET IF NO ELEMENTS IN ARRAY
;
SEC ; SET CARRY IN CASE ARRAY HAS NO ELEMENTS
TSTA ; CHECK NUMBER OF ELEMENTS
BEQ EXITMX ; BRANCH (EXIT) WITH CARRY SET IF NO
; ELEMENTS INDICATES INVALID RESULT
;
; EXAMINE ELEMENTS ONE AT A TIME, COMPARING EACH 0NE'S VALUE
; WITH CURRENT MAXIMUM AND ALWAYS KEEPING LARGER VALUE AND
; ITS ADDRESS. IN THE FIRST ITERATION, TAKE THE FIRST
; ELEMENT AS THE CURRENT MAXIMUM.
;
TFR A,B ; SAVE NUMBER OF ELEMENTS IN B
LEAY 1,X ; SET POINTER AS IF PROGRAM HAD JUST
; EXAMINED THE FIRST ELEMENT AND FOUND
; IT TO BE LARGER THAN PREVIOUS MAXIMUM
MAXLP:
LEAX -1,Y ; SAVE ADDRESS OF ELEMENT JUST EXAMINED
; AS ADDRESS OF MAXIMUM
LDA ,X ; SAVE ELEMENT JUST EXAMINED AS MAXIMUM
;
; COMPARE CURRENT ELEMENT TO MAXIMUM
; KEEP LOOKING UNLESS CURRENT ELEMENT IS LARGER
;
MAXLP1:
DECB ; COUNT ELEMENTS
BEQ EXITLP ; BRANCH (EXIT) IF ALL ELEMENTS EXAMINED
CMPA ,Y+ ; COMPARE CURRENT ELEMENT TO MAXIMUM
; ALSO MOVE POINTER TO NEXT ELEMENT
BCC MAXLP1 ; CONTINUE UNLESS CURRENT ELEMENT LARGER
BCS MAXLP ; ELSE CHANGE MAXIMUM
;
; CLEAR CARRY TO INDICATE VALID RESULT MAXIMUM FOUND
;
EXITLP:
CLC ; CLEAR CARRY TO INDICATE VALID RESULT
EXITMX:
RTS
;
; SAMPLE EXECUTION:
;
SC6C:
LDX #ARY ; GET BASE ADDRESS OF ARRAY
LDA #SZARY ; GET SIZE OF ARRAY IN BYTES
JSR MAXELM ; FIND LARGEST UNSIGNED ELEMENT
; RESULT FOR TEST DATA IS
; A = FF HEX (MAXIMUM), X = ADDRESS OF
; FF IN ARY.
BRA SC6C ; LOOP FOR MORE TESTING
SZARY EQU $10 ; SIZE OF ARRAY IN BYTES
ARY: FCB 8
FCB 7
FCB 6
FCB 5
FCB 4
FCB 3
FCB 2
FCB 1
FCB $FE
FCB $FD
FCB $FC
FCB $FB
FCB $FA
FCB $FB
END