-
Notifications
You must be signed in to change notification settings - Fork 8
/
6809_CONCAT.s
135 lines (133 loc) · 3.85 KB
/
6809_CONCAT.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
; Title: String Concatenation
; Name: CONCAT
; Purpose: Concatenate 2 strings into one string.
; Entry:
; TOP OF STACK
;
; High byte of return address
; Low byte of return address
; Maximum Length of string 1
; High byte of string 2 address
; Low byte of string 2 address
; High byte of string 1 address
; Low byte of string 1 address
;
; Each string consists of a length byte
; followed by a maximum of 255 characters.
;
; Exit: String 1 := string 1 concatenated with string 2
;
; If no errors then
; Carry := 0
; else
; begin
; Carry := 1
; if the concatenation makes string 1 too long,
; concatenate only the part of string 2
; that results in string 1 having its maximum Length
; if Length(string1) > maximum Length then
; no concatenation is done
; end
;
; Registers Used: All
;
; Time: Approximately 17 * (length of string 2) cycles
; plus 95 cycles overhead
;
; Size: Program 59 bytes
;
;
CONCAT:
LDU ,S ; SAVE RETURN ADDRESS
;
; DETERMINE WHERE TO START ADDING CHARACTERS
; CONCATENATION STARTS AT THE END OF STRING 1
; END OF STRING 1 = BASE 1 + LENGTH1 + 1, WHERE
; THE EXTRA 1 IS FOR THE LENGTH BYTE
;
CLR 1,S ; INDICATE NO TRUNCATION NECESSARY
LDX 5,S ; GET BASE ADDRESS OF STRING 1
LDA ,X ; GET LENGTH OF STRING 1
LEAX A,X ; POINT TO LAST BYTE IN STRING 1
LEAX 1,X ; POINT JUST BEYOND END OF STRING 1
;
; NEW CHARACTERS COME FROM STRING 2, STARTING AT
; BASE2+1 (SKIPPING OVER LENGTH BYTE)
;
LDY 3,S ; GET BASE ADDRESS OF STRING 2
LDB ,Y+ ; GET LENGTH OF STRING 2 AND POINT TO
; FIRST DATA BYTE
BEQ SETTRN ; BRANCH (EXIT) IF STRING 2 HAS ZERO LENGTH
; NO ERROR IN THIS CASE
;
; DETERMINE HOW MANY CHARACTERS TO CONCATENATE
; THIS IS LENGTH OF STRING 2 IF COMBINED STRING WOULD
; NOT EXCEED MAXIMUM LENGTH
; OTHERWISE, IT IS THE NUMBER THAT HOULD BRING COMBINED
; STRING TO ITS MAXIMUM LENGTH THAT IS, MAXIMUM LENGTH
; MINUS LENGTH OF STRING 1
;
STB ,S ; SAVE LENGTH OF STRING 2 IN STACK
ADDA ,S ; ADD STRING LENGTHS TO DETERMINE LENGTH
; OF COMBINED STRING
BCS TOOLNG ; BRANCH IF LENGTH HILL EXCEED 255 BYTES
CMPA 2,S ; COMPARE TO MAXIMUM LENGTH BLS DOCAT
; BRANCH IF LENGTH DOES NOT EXCEED MAXIMUM
;
; COMBINED STRING IS TOO LONG
; INDICATE STRING OVERFLOW NITH FF MARKER IN STACK
; SET NUMBER OF CHARACTERS TO CONCATENATE = MAXLEN S1LEN
; SET NEW LENGTH OF STRING 1 TO MAXIMUM LENGTH
;
TOOLNG:
COM 1,S ; INDICATE STRING TRUNCATION (MARKER = FF)
LDB 2,S ; NUMBER OF CHARACTERS TO CONCATENATE =
SUBB [5,S] ; MAXIMUM LENGTH STRING 1 LENGTH
BLS SETTRN ; BRANCH (EXIT) IF ORIGINAL STRING wAS
; TOO LONG
LDA 2,S ; NEW LENGTH = MAXIMUM LENGTH
;
; CONCATENATE STRINGS BY MOVING CHARACTERS FROM STRING 2
; TO THE AREA FOLLOWING STRING 1
;
DOCAT:
STA [5,S] ; SAVE NEW LENGTH IN STRING 1'S LENGTH BYTE
TSTB ; CHECK NUMBER OF BYTES TO CONCATENATE
BEQ SETTRN ; BRANCH (EXIT) IF NO BYTES TO CONCATENATE
CATLP:
LDA ,Y+ ; GET BYTE FROM STRING 2
STA ,X+ ; MOVE BYTE TO AREA FOLLOWING STRING 1
DECB ; CONTINUE UNTIL ALL BYTES MOVED
BNE CATLP ; SET CARRY FROM TRUNCATION INDICATOR IN STACK
;
; CARRY = 1 IF CHARACTERS HAD TO BE TRUNCATED, 0 OTHERWISE
;
SETTRN:
ROR 1,S ; SET CARRY FROM TRUNCATION INDICATOR
; CARRY = 1 IF TRUNCATION, 0 IF NOT
;
; REMOVE PARAMETERS FROM STACK AND EXIT
;
LEAS 7,S ; REMOVE PARAMETERS FROM STACK
JMP ,U ; EXIT TO RETURN ADDRESS
;
; SAMPLE EXECUTION:
;
SC5B: LDY #S1 ; GET BASE ADDRESS OF STRING 1
LDX #S2 ; GET BASE ADDRESS OF STRING 2
LDA #$20 ; GET MAXIMUM LENGTH OF STRING 1
PSHS A,X,Y ; SAVE PARAMETERS IN STACK
JSR CONCAT ; CONCATENATE STRINGS
; RESULT OF CONCATENATING
; "LASTNAME" AND ", FIRSTNAME"
; IS S1 = 13H,"LASTNAME, FIRSTNAME"
BRA SC5B ; LOOP THROUGH TEST
;
; TEST DATA
;
S1: FCB 8 ; LENGTH OF S1 IN BYTES
FCC /LASTNAME / 32 BYTE MAX LENGTH
S2:
FCB 11 ; LENGTH OF S2 IN BYTES
FCC /, FIRSTNAME / 32 BYTE MAX LENGTH
END