forked from jmatzen/leventhal-6809
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6809_SPACES.s
120 lines (117 loc) · 2.96 KB
/
6809_SPACES.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
; Title: Remove Extra Spaces from a String
; Name: SPACES
;
; Purpose: Remove leading, trailing, and extra
; internal spaces from a string
;
; Entry: Register X = Base address of string
;
; The string consists of a length byte followed by
; a maximum of 255 characters.
;
; Exit: Leading, trailing, and excess internal spaces removed
;
; Registers Used: All
;
; Time: Approximately 35 * (LENGTH(STRG) + 65 cycles overhead
;
; Size: Program 61 bytes
; Data 2 stack bytes
;
SPACES:
; SAVE BASE ADDRESS OF STRING
; START COMPACTED STRING'S LENGTH AT ZERO
; INDICATE INITIALLY LAST CHARACTER HAS NOT A SPACE
;
TFR X,U ; SAVE BASE ADDRESS OF STRING
CLRA ; INDICATE LAST CHARACTER HAS NOT A SPACE
CLRB ; COMPACTED STRING'S LENGTH = ZERO
PSHS A,B ; SAVE INDICATOR, LENGTH IN STACK
;
; EXIT IF STRING LENGTH IS ZERO
;
LDB ,X+ ; GET STRING LENGTH
BEQ EXITRE ; BRANCH (EXIT) IF STRING LENGTH IS ZERO
;
; REMOVE ALL LEADING SPACES
;
TFR X,Y ; START POINTERS TO BOTH ORIGINAL, COMPACTED
; STRINGS AT FIRST CHARACTER IN STRING
LEADSP:
LDA ,X+ ; GET NEXT CHARACTER
CMPA #SPACE ; IS IT A SPACE?
BNE MARKCH ; BRANCH IF CHARACTER IS NOT A SPACE
DECB ; DECREMENT CHARACTER COUNT
BNE LEADSP ; BRANCH IF NOT DONE WITH STRING
CLR ,U ; STRING CONSISTED ENTIRELY OF SPACES
; MAKE ITS LENGTH ZERO
BRA EXITRE ; EXIT
;
; WORK THROUGH MAIN PART OF STRING, OMITTING SPACES
; THAT OCCUR IMMEDIATELY AFTER OTHER SPACES
; CHECK IF CURRENT CHARACTER IS A SPACE
; CHECK IF PREVIOUS CHARACTER UAS A SPACE
; IF SO, 0MIT CHARACTER FROM COMPACTED STRING
; IF NOT, MARK CHARACTER AS A SPACE
;
MVCHAR:
LDA ,X+ ; GET NEXT CHARACTER
CMPA #SPACE ; IS IT A SPACE?
BNE MARKCH ; BRANCH IF CHARACTER IS NOT A SPACE
TST ,S ; CHECK IF LAST CHARACTER HAS A SPACE
BEQ CNTCHR ; BRANCH IF IT WAS
COM ,S ; INDICATE CURRENT CHARACTER IS A SPACE
BRA SVCHR ;
;
; INDICATE CURRENT CHARACTER IS NOT A SPACE
;
MARKCH:
CLR ,S ; INDICATE CURRENT CHARACTER NOT A SPACE
;
; SAVE CURRENT CHARACTER IN COMPACTED STRING
;
SVCHR:
STA ,Y+ ; SAVE CHARACTER IN COMPACTED STRING
INC 1,S ; ADD 1 TO LENGTH OF COMPACTED STRING
;
; COUNT CHARACTERS
;
CNTCHR:
DECB ; COUNT CHARACTERS
BNE MVCHAR ; BRANCH IF ANY CHARACTERS LEFT
;
; OMIT LAST CHARACTER IF IT WAS A SPACE
;
TST ,S ; CHECK IF FINAL CHARACTER WAS A SPACE
BEQ SETLEN ; BRANCH IF IT WAS NOT
DEC 1,S ; OMIT FINAL CHARACTER IF IT WAS A SPACE
;
; SET LENGTH OF COMPACTED STRING
;
SETLEN:
LDA 1,S ; GET LENGTH OF COMPACTED STRING
STA ,U ; SAVE AS LENGTH BYTE IN STRING
;
; REMOVE TEMPORARIES FROM STACK AND EXIT
;
EXITRE:
LEAS 2,S ; REMOVE TEMPORARY DATA FROM STACK
RTS
;
; CHARACTER DEFINITION
;
SPACE EQU $20 ; ASCII SPACE CHARACTER
;
; SAMPLE EXECUTION:
;
SCSG:
LDX #STG ; GET BASE ADDRESS OF STRING
JSR SPACES ; REMOVE SPACES
; RESULT OF REMOVING SPACES FROM
; ' JOHN SMITH ' IS 'JOHN SMITH'
;
; DATA SECTION
;
STG: FCB $0E ; LENGTH OF STRING IN BYTES
FCC " JOHN SMITH " ; STRING
END