-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode1.S
187 lines (135 loc) · 3.54 KB
/
code1.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
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
.data
/* specifier only reads in 14 characters*/
/* doesn't read in the new line character */
.balign 4
pattern: .asciz "%14[^\n]%*c"
.balign 4
message: .asciz "Input a string that is at most 12 characters:\n"
/* program's output string, gives concatenated string & total number of characters */
.balign 4
output: .asciz "Here's the concatenated string:\n%s%s\nIt has %d characters.\n"
/* If scanf reads in more than 14 characters, program prints out these error messages */
/* for string1 */
.balign 4
errorM1: .asciz "Error code 7: The string has more than 12 characters.\n"
/* for string2 */
.balign 4
errorM2: .asciz "Error code 8: The string has more than 12 characters.\n"
/* allocates 16 bytes of memory for string1 and string2 */
.balign 4
string1: .space 16
.balign 4
string2: .space 16
.balign 4
return: .word 0
.text
.global main
.global printf
.global scanf
main:
/* loads contents of link register to return variable */
ldr r1, =return
str lr, [r1]
first:
/* prompts user for first string and scans it in */
ldr r0, =message
bl printf
ldr r0, addr_of_pattern
ldr r1, addr_of_string1
bl scanf
/* sets up loop to find string length */
/* r10 contains current index, r1 the address of the first character of string1, */
/* and r0, the first character of string */
mov r10,#0
ldr r1, addr_of_string1
ldrb r0, [r1]
/* if string is empty skip to second branch */
cmp r0, #0
beq second
/* else set r10 to 1 */
add r10, r10, #1
loop:
/* checks to see if index > 13 */
/* if it is give error */
cmp r10, #13
beq firsterror
/* load next character onto r0 */
ldrb r0, [r1, #+1]
/* have r1 store address of next string*/
add r1, r1, #1
/* check to see if you have reached end of string */
cmp r0, #0
/* if so move on to second branch */
beq second
/* else increment index by 1 */
add r10, r10, #1
/* and continue */
b loop
second:
/* essentially the same process as first branch */
/* except if string is 0 move to concatenation branch */
ldr r0, =message
bl printf
ldr r0, addr_of_pattern
ldr r1, addr_of_string2
bl scanf
/* r11: index, r2: address of first char of string2, & r3 first char */
mov r11, #0
ldr r2, addr_of_string2
ldrb r3, [r2]
cmp r3, #0
beq printConcat
add r11, r11, #1
loop2:
/* if index is greater than 13 go to seconderror branch */
/* rest of loop2 branch is same as the loop branch */
cmp r11, #13
beq seconderror
ldrb r3, [r2, #+1]
add r2, r2, #1
cmp r3, #0
beq printConcat
add r11, r11, #1
b loop2
printConcat:
/* load arguments for printing output variable */
/* r0 is the output variable itself */
ldr r0, =output
/* r1 (string1) substitute for first specifier */
ldr r1, =string1
/* r2(string2) for second specifier*/
ldr r2, =string2
/* r3 (total length) for third */
add r3,r10,r11
mov r4, r3
bl printf
/* stores combined length in r0 for error code */
add r0, r10, r11
b end
firsterror:
/* print out errorM1 message */
ldr r0, addr_of_errorM1
bl printf
/* store 7 in r0 for error code */
mov r0, #7
b end
seconderror:
/* print out errorM1 message */
ldr r0, addr_of_errorM2
bl printf
/* store 7 in r0 for error code */
mov r0, #8
b end
end:
/* restore link register */
ldr lr, =return
ldr lr, [lr]
bx lr
addr_of_output: .word output
addr_of_message: .word message
addr_of_string1: .word string1
addr_of_string2: .word string2
addr_of_return: .word return
addr_of_errorM1: .word errorM1
addr_of_errorM2: .word errorM2
addr_of_pattern: .word pattern