-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path6809_DELETE.s
161 lines (154 loc) · 4.25 KB
/
6809_DELETE.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
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
.macro CLC
ANDCC #$FE
.endm
.macro SEC
ORCC #$01
.endm
;
; Title: Delete a Substring from a String
;
; Name: DELETE
;
; Purpose: Delete a substring from a string given
; a starting index and a length.
;
; Entry: TOP OF STACK
; High byte of return address
; Low byte of return address
; Number of bytes to delete (count)
; Starting index to delete from (index)
; High byte of string address
; Low byte of string address
;
;The string consists of a Length byte followed by a maximum of 255 characters.
;
; Exit: Substring deleted.
; If no errors then Carry := 0
; else
; begin
; the following conditions cause an
; error with Carry flag = 1.
; if (index = 0) or (index > length(string))
; then do not change string
; if count is too large
; then delete only the characters from
; index to end of string
; end
;
; Registers used: All
;
; Time: Approximately 17 * (LENGTH(STRG) * INDEX * COUNT+1)
; plus 120 cycles overhead
;
; Size: Program 80 bytes
;
DELETE: LDU ,S ; SAVE RETURN ADDRESS
;
; INITIALIZE ERROR INDICATOR (DELERR) T0 0
;
CLR ,S ; INDICATE NO ERRORS
;
; EXIT IF COUNT IS ZERO, STARTING INDEX IS ZERO, OR
; STARTING INDEX IS BEYOND THE END OF THE STRING
LDB 2,S ; CHECK NUMBER OF BYTES T0 DELETE
BEQ OKEXIT ; BRANCH (GOOD EXIT) IF NOTHING T0 DELETE
LDA 3,S ; CHECK STARTING INDEX
BEQ EREXIT ; BRANCH (ERROR EXIT) IF STARTING INDEX IS ZERO
; THAT IS, IN LENGTH BYTE
LDX 4,S ; GET BASE ADDRESS OF STRING
CMPA ,X ; CHECK IF STARTING INDEX IS WITHIN STRING
BHI EREXIT ; BRANCH (ERROR EXIT) IF STARTING INDEX
; IS BEYOND END OF STRING
;
; CHECK WHETHER NUMBER OF CHARACTERS REQUESTED TO BE
; DELETED ARE PRESENT
; THEY ARE IF STARTING INDEX + NUMBER OF BYTES TO DELETE - 1
; IS LESS THAN OR EQUAL TO STRING LENGTH
; IF NOT, THEN DELETE ONLY TO END OF STRING
;
ADDA 2,S ; COMPUTE STARTING INDEX + COUNT
OKEXIT:
BCS TRUNC ; TRUNCATE IF INDEX + COUNT > 255
DECA ; END OF DELETED AREA IS AT INDEX GIVEN BY
; STARTING INDEX + COUNT 1
CMPA ,X ; COMPARE TO LENGTH OF SUBSTRING
BCS CNTOK ; BRANCH IF MORE THAN ENOUGH CHARACTERS
BEQ TRUNC ; TRUNCATE BUT NO ERROR (EXACTLY ENOUGH
; CHARACTERS)
COM ,S ; INDICATE ERROR NOT ENOUGH CHARACTERS
; TO DELETE
;
; TRUNCATE THE STRING NO COMPACTING NECESSARY
; SIMPLY REDUCE ITS LENGTH TO STARTING INDEX 1
;
TRUNC:
LDA 3,S ; STRING LENGTH = STARTING INDEX 1
DECA
STA ,X
;
; TEST ERROR INDICATOR AND EXIT ACCORDINGLY
;
LDA ,S ; TEST ERROR INDICATOR
BEQ OKEXIT ; NO ERROR, TAKE GOOD EXIT
BNE EREXIT ; OTHERWISE, TAKE ERROR EXIT
;
; DELETE SUBSTRING BY COMPACTING THE STRING
; MOVE ALL CHARACTERS ABOVE THE DELETED AREA DOWN
;
CNTOK:
STA 1,S ; SAVE INDEX TO END OF AREA TO BE DELETED
LDB ,X ; NUMBER OF CHARACTERS TO MOVE = STRING
SUBB 1,S ; LENGTH - INDEX AT END OF AREA
INCA ; ADD 1 TO INDEX AT END OF DELETED AREA
; THUS GIVING FIRST BYTE TO MOVE DOWN
LEAY A,X ; POINT TO FIRST CHARACTER TO BE
; MOVED DOWN
LDA 3,S ; GET STARTING INDEX
LEAX A,X ; POINT TO FIRST BYTE IN AREA TO BE DELETED
MVLP:
LDA ,Y+ ; GET CHARACTER FROM ABOVE DELETED AREA
STA ,X+ ; MOVE IT DOWN TO COMPACT STRING
DECB ; CONTINUE THROUGH END OF STRING
BNE MVLP
;
; COMPUTE AND SAVE LENGTH OF STRING AFTER DELETION
;
LDX 4,S ; POINT TO STRING LENGTH
LDA ,X ; GET ORIGINAL LENGTH
SUBA 2,S ; SUBTRACT NUMBER OF BYTES TO DELETE
STA ,X ; DIFFERENCE IS NEW LENGTH
; CLEAR CARRY, INDICATING NO ERRORS
CLC ; CLEAR CARRY, NO ERRORS
BRA EXITDE
;
;
; SET CARRY, INDICATING AN ERROR
;
EREXIT:
SEC ; SET CARRY, INDICATING ERROR
;
; REMOVE PARAMETERS FROM STACK AND EXIT
;
EXITDE:
LEAS 6,S ; REMOVE PARAMETERS FROM STACK
JMP ,U ; EXIT TO RETURN ADDRESS
;
; SAMPLE EXECUTION:
;
SC5E:
LDX #SSTG ; GET BASE ADDRESS OF STRING
LDB IDX ; GET STARTING INDEX FOR DELETION
LDA CNT ; GET NUMBER OF CHARACTERS TO DELETE
PSHS A,B,X ; SAVE PARAMETERS IN STACK
JSR DELETE ; DELETE CHARACTERS
; DELETING 4 CHARACTERS STARTING AT INDEX 1
; FROM "JOE HANDOVER" LEAVES "HANDOVER"
BRA SC5E ; LOOP THROUGH TEST
;
; DATA SECTION
;
IDX: FCB 1 ; STARTING INDEX FOR DELETION
CNT: FCB 4 ; NUMBER OF CHARACTERS TO DELETE
SSTG: FCB 12 ; LENGTH OF STRING IN BYTES
FCC /JOE HANDOVER/
END