-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpstring.s
126 lines (118 loc) · 2.32 KB
/
pstring.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
# 214289415 dan binnun
.section .rodata
str1: .string "invalid input!\n"
.text
.globl pstrlen
.type pstrem, @function
pstrlen:
movzbq (%rdi),%rax
ret
.globl replaceChar
.type replaceChar, @function
replaceChar:
movzbq (%rdi),%r8 #save length of str
movq $0,%rcx #set counter to zero
jmp .L2 #check for-loop condition
.L3:
cmpb %sil,1(%rdi,%rcx,1) #compare old char and current char
jne .L7
movb %dl,1(%rdi,%rcx,1) #set new char if equals
.L7:
addq $1,%rcx #increment loop counter
.L2:
cmpq %r8,%rcx
jl .L3
movq %rdi,%rax
ret
.globl pstrijcpy
.type pstrijcpy, @function
pstrijcpy:
cmpb (%rdi),%dl #check if index are out of strings borders
jae .L6
cmpb (%rdi),%cl
jae .L6
cmpb (%rsi),%dl
jae .L6
cmpb (%rsi),%cl
jae .L6
jmp .L4
.L5:
movzbq %dl,%rdx #load loop index
movb 1(%rsi,%rdx,1),%al #load i-th char of src
movb %al,1(%rdi,%rdx,1) #set i-th charof dest
addb $1,%dl
.L4:
cmpb %dl,%cl #loop condition
jae .L5
movq %rdi,%rax
ret
.L6:
leaq -16(%rsp),%rsp
movq %rdi,(%rsp)
movq $str1,%rdi
movq $0,%rax
call printf
movq (%rsp),%rax
leaq 16(%rsp),%rsp
ret
.globl swapCase
.type swapCase, @function
swapCase:
movzbq (%rdi),%rsi
movq $0,%rdx
.L9:
cmpb $97,1(%rdi,%rdx,1) #check if below 'a'
jb .L10
cmpb $122,1(%rdi,%rdx,1) #check if above 'z'
ja .L10
movb 1(%rdi,%rdx,1),%cl
subb $32,%cl #substract 32 to capitalize letter
movb %cl,1(%rdi,%rdx,1)
jmp .L8
.L10:
cmpb $65,1(%rdi,%rdx,1) #check if below 'a'
jb .L8
cmpb $90,1(%rdi,%rdx,1) #check if above 'z'
ja .L8
movb 1(%rdi,%rdx,1),%cl
addb $32,%cl #substract 32 to capitalize letter
movb %cl,1(%rdi,%rdx,1)
.L8:
addq $1,%rdx
cmpq %rsi,%rdx
jb .L9
movq %rdi,%rax
ret
.globl pstrijcmp
.type pstrijcmp, @function
pstrijcmp:
cmpb (%rdi),%dl #check if index are out of strings borders
jae .L11
cmpb (%rdi),%cl
jae .L11
cmpb (%rsi),%dl
jae .L11
cmpb (%rsi),%cl
jae .L11
.L13:
movb 1(%rdi,%rdx,1),%r8b
cmpb %r8b,1(%rsi,%rdx,1)
jbe .L12
movq $-1,%rax
ret
.L12:
je .L14
movq $1,%rax
ret
.L14:
addq $1,%rdx
cmpq %rdx,%rcx
jae .L13
movq $0,%rax
ret
.L11:
movq $str1,%rdi
movq $0,%rax
call printf
movq $-2,%rax
ret