-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_select.s
More file actions
297 lines (236 loc) · 10.4 KB
/
func_select.s
File metadata and controls
297 lines (236 loc) · 10.4 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Sagi Wilentzik
#func_select.s
.text # Creating formats for strings.
format1: .string "invalid option!\n" # string for the case that the input in out of bound.
format11: .string "invalid input!\n"
formatScanChar: .string "%s"
formatScanNumber: .string "%d"
format3: .string "old char: %c, new char: %c, first string: %s, second string: %s\n"
format2: .string "first pstring length: %d, second pstring length: %d\n" # string for pstrlen.
format6: .string "compare result: %d\n"
format5: .string "length: %d, string: %s\n"
# Building the jump table
.align 8 # Align address to multiple of 8.
.L10:
.quad .L5060 # case 50 or 60
.quad .Ld # Csase 51 - go to defult.
.quad .L52 # Case 52.
.quad .L53 # Case 53.
.quad .L54 # Case 54.
.quad .L55 # Case 55.
.quad .Ld # Case 56 - go to defult.
.quad .Ld # Case 57 - go to defult
.quad .Ld # Case 58 - go to defult
.quad .Ld # Case 59 - go to defult.
.quad .L5060 # case 50 or 60
.L5060: # lable for input 50 or 60.
leaq (%rsi), %r12 #save first string in r12
leaq (%rdx), %r13 #save second string in r13
leaq (%r12), %rdi
call pstrlen # call to funciton pstrlen.
leaq (%rax), %r12 # save the first string length in r12
leaq (%r13), %rdi
call pstrlen #call the function with the second string.
movq %r12, %rsi #save first string length in rsi.
leaq (%rax), %r13 # save second string length in r13.
movq %r13, %rdx
movq $format2, %rdi # save format in rdi
xorq %rax, %rax
call printf
popq %r13 # return r13 his original value.
popq %r12 # return r12 his original value.
movq %rbp,%rsp
popq %rbp
ret
.L52: # lable for input 52.
leaq (%rsi), %r12 #save first string in r12
leaq (%rdx), %r13 #save second string in r13
# read the first char from usem.
movq $formatScanChar, %rdi # save string in rdi register.
leaq 128(%rsp), %rsi # insert the address to rsi register.
xorq %rax, %rax # make rax register 0.
call scanf
# read the second char from user.
movq $formatScanChar, %rdi #insert the string format to rdi register.
leaq 160(%rsp), %rsi # insert the address to rsi register.
xorq %rax, %rax # make rax register 0.
call scanf
leaq (%r12), %rdi # save first string in %rdi.
leaq (%r13), %rsi # save second string in %rsi.
leaq 128(%rsp), %rsi # save the old char in rsi.
leaq 160(%rsp), %rdx # save the second number in rdx.
call replaceChar # call to replaceChar with first string.
leaq (%rax), %r12 # save the new string (for the first one) in r12.
leaq (%r13), %rdi # save the second string in rdi.
leaq 128(%rsp), %rsi # save the old char in rsi.
leaq 160(%rsp), %rdx # save the second number in rdx.
call replaceChar # call to replaceChar with second string.
leaq (%rax), %r13 # save the new string (for the second one) in r13
movq $format3, %rdi # save the string format in rdi
leaq 128(%rsp), %rsi # save the old char in rsi.
movq (%rsi), %rsi
leaq 160(%rsp), %rdx # save the second number in rdx.
movq (%rdx), %rdx
leaq 1(%r12), %rcx # save the first string in rcx
leaq 1(%r13), %r8 # save the second string in r8
xorq %rax, %rax #make rax zero
call printf
popq %r13 # return r13 his original value.
popq %r12 # return r12 his original value.
movq %rbp,%rsp
popq %rbp
ret
.L53: # lable for input 53.
leaq (%rsi), %r12 #save first string in r12
leaq (%rdx), %r13 #save second string in r13
# read the first number from usem.
movq $formatScanNumber, %rdi # save string in rdi register.
leaq 128(%rsp), %rsi # insert the address to rsi register.
xorq %rax, %rax # make rax register 0.
call scanf
# read the second number from user.
movq $formatScanNumber, %rdi #insert the string format to rdi register.
leaq 160(%rsp), %rsi # insert the address to rsi register.
xorq %rax, %rax # make rax register 0.
call scanf
leaq (%r12), %rdi # save first string in %rdi.
leaq (%r13), %rsi # save second string in %rsi.
leaq 128(%rsp), %rdx # save the first number in rdx.
leaq 160(%rsp), %rcx # save the second number in rcx.
movzbq (%rdi), %r12 # save the first string length in r12.
movzbq (%rsi), %r13 # save the second string length in r13.
xorq %r8, %r8
xorq %r9, %r9
movb (%rdx), %r8b # save start index (i) in r8b
movb (%rcx), %r9b # save end index (j) in r9b
cmpb %r12b, %r8b # check if the starting index is bigger than length.
jg .L53invalidInput
cmpb %r13b, %r9b # checking if the end index is bigger than length.
jge .L53invalidInput
cmpb %r12b, %r9b
jg .L53invalidInput
cmpb %r13b, %r8b
jge .L53invalidInput
cmpb $0, %r8b # checking if starting index is negative.
jl .L53invalidInput
cmpb $0, %r9b # checking if the end index in negative.
jl .L53invalidInput
cmpb %r8b, %r9b #checking if i > j
jl .L53invalidInput
jmp .L53Call
.L53invalidInput: # This lable will print the value when the input is invalid.
leaq (%rdi), %r12 # save first string in r12
leaq (%rsi), %r13 # save second string in r13
movq $format11, %rdi # invalid input format
xorq %rax, %rax
call printf
movq $format5, %rdi # save the format in rdi
movzbq (%r12), %rsi #save the string length in rsi
leaq 1(%r12), %rdx #save the string in rdx
xorq %rax, %rax
call printf
movq $format5, %rdi # save the format in rdi
movzbq (%r13) , %rsi #save the string length in rsi
leaq 1(%r13), %rdx #save the string in rdx
xorq %rax, %rax
call printf
jmp .L1
.L53Call:
call pstrijcpy # call to function pstrijcpy.
popq %r13 # return r13 his original value.
popq %r12 # return r12 his original value.
movq %rbp,%rsp
popq %rbp
ret
.L54: # lable for input 54.
leaq (%rsi), %r12 #save first string in r12
leaq (%rdx), %r13 #save second string in r13
leaq (%r12), %rdi #save first string in rdi
call swapCase # call to function swapCase.
leaq (%r13), %rdi # save the secons string in rdi
call swapCase # call to swapCase with second string.
popq %r13 # return r13 his original value.
popq %r12 # return r12 his original value.
movq %rbp,%rsp
popq %rbp
ret
.L55: # lable for input 55.
leaq (%rsi), %r12 #save first string in r12
leaq (%rdx), %r13 #save second string in r13
# read the first number from usem.
movq $formatScanNumber, %rdi # save string in rdi register.
leaq 128(%rsp), %rsi # insert the address to rsi register.
xorq %rax, %rax # make rax register 0.
call scanf
# read the second number from user.
movq $formatScanNumber, %rdi #insert the string format to rdi register.
leaq 160(%rsp), %rsi # insert the address to rsi register.
xorq %rax, %rax # make rax register 0.
call scanf
leaq (%r12), %rdi # save first string in %rdi.
leaq (%r13), %rsi # save second string in %rsi.
leaq 128(%rsp), %rdx # save the first number in rdx.
leaq 160(%rsp), %rcx # save the second number in rcx.
movzbq (%rdi), %r12 # save the first string length in r12.
movzbq (%rsi), %r13 # save the second string length in r13.
xorq %r8, %r8 # make r8 zero.
xorq %r9, %r9 # make r9 zero.
movb (%rdx), %r8b # save start index (i) in r8b
movb (%rcx), %r9b # save end index (j) in r9b
cmpb %r12b, %r8b # check if the starting index is bigger than length.
jg .L55InvalidInput
cmpb %r13b, %r9b # checking if the end index is bigger than length.
jg .L55InvalidInput
cmpb %r12b, %r9b
jg .L55InvalidInput
cmpb %r13b, %r8b
jg .L55InvalidInput
cmpb $0, %r8b # checking if starting index is negative.
jl .L55InvalidInput
cmpb $0, %r9b # checking if the end index in negative.
jl .L55InvalidInput
cmpb %r8b, %r9b #checking if i > j
jl .L55InvalidInput
call pstrijcmp # call to function pstrijcpy.
movq $format6, %rdi # insert the format to rdi
movq %rax, %rsi # insert the result to rsi.
xorq %rax, %rax # make rax zero.
call printf
jmp .L1
.L55InvalidInput:
movq $format11, %rdi # invalid input format
xorq %rax, %rax
call printf
movq $format6, %rdi # print the -2 value.
movq $-2, %rsi
xorq %rax, %rax
call printf
popq %r13 # return r13 his original value.
popq %r12 # return r12 his original value.
movq %rbp,%rsp
popq %rbp
ret
.Ld: # lable for defult.
movq $format1,%rdi # insert the string "invalid option!\n" to rdi register.
call printf # Call to printf function.
jmp .L1
.L1: # lable for exit from the switch case options.
popq %r13
popq %r12
movq %rbp,%rsp
popq %rbp
ret
.text # creating the run_func function.
.global run_func
.type run_func, @function # The function will recieve the option from the switch case and returns
# the rigth fucntion.
run_func:
pushq %rbp # push rbp to stack
movq %rsp, %rbp # move rsp to rbs
sub $256, %rsp
leaq (%rsp), %r12 # save r12 register in stack
leaq 64(%rsp), %r13 # save r13 register in stack
leaq -50(%rdi),%rcx # reduce the address by 50 that we could find the correct adress for the functions.
cmpq $10,%rcx # checking if the number if greater than 10, which means that the argument is greate than 60.
ja .Ld # if True, go to defult.
jmp *.L10(,%rcx,8) # if False (The number is between 50-60 jump to the correct adress in the array.