Skip to content

Commit 214a66a

Browse files
committed
Added an example to check if a string is palindrome.
1 parent dfb771e commit 214a66a

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

27-palindrome/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all:
2+
nasm -f elf64 palindrome.asm -o palindrome.o
3+
gcc -nostartfiles palindrome.o -o palindrome
4+
5+
clean:
6+
rm -f palindrome.o palindrome

27-palindrome/palindrome.asm

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
extern exit
2+
extern puts
3+
extern printf
4+
5+
SECTION .data
6+
str: db 'No word, no bond, row on.', 0
7+
str_is_palindrome: db '"%s" - is palindrome.', 10, 0
8+
str_is_not_palindrome: db '"%s" - is not palindrome.', 10, 0
9+
10+
SECTION .text
11+
GLOBAL _start
12+
13+
_start:
14+
; Start string.
15+
mov r10, str
16+
17+
; Find end of string str.
18+
mov r11, str
19+
20+
loop:
21+
mov cl, [r11]
22+
cmp cl, 0
23+
je init_is_palindrome
24+
25+
inc r11
26+
jmp loop
27+
28+
init_is_palindrome:
29+
dec r11
30+
call is_palindrome
31+
32+
finish:
33+
cmp rax, 0
34+
je print_is_not_palindrome
35+
36+
; Print string is palindrome.
37+
mov rsi, str
38+
mov rdi, str_is_palindrome
39+
mov rax, 0
40+
call printf
41+
jmp exit_app
42+
43+
print_is_not_palindrome:
44+
; Print string is not palindrome.
45+
mov rsi, str
46+
mov rdi, str_is_not_palindrome
47+
mov rax, 0
48+
call printf
49+
50+
exit_app:
51+
; Exit application.
52+
mov rdi, 0
53+
call exit
54+
55+
is_palindrome:
56+
cmp r10, r11
57+
jge finish_palindrome
58+
59+
; Check start string is letter.
60+
mov dil, [r10]
61+
call is_letter
62+
cmp rax, 0
63+
je inc_left
64+
65+
; Check end string is letter.
66+
mov dil, [r11]
67+
call is_letter
68+
cmp rax, 0
69+
je dec_right
70+
71+
; Compare start of string and end of string.
72+
mov cl, [r10]
73+
mov dil, cl
74+
call lowercase
75+
mov cl, al
76+
77+
mov dl, [r11]
78+
mov dil, dl
79+
call lowercase
80+
mov dl, al
81+
82+
cmp cl, dl
83+
jne not_palindrome
84+
85+
inc r10
86+
dec r11
87+
jmp is_palindrome
88+
89+
finish_palindrome:
90+
mov rax, 1
91+
ret
92+
93+
not_palindrome:
94+
mov rax, 0
95+
ret
96+
97+
inc_left:
98+
inc r10
99+
jmp is_palindrome
100+
101+
dec_right:
102+
dec r11
103+
jmp is_palindrome
104+
105+
; Function to check rdi is letter (a-zA-Z).
106+
is_letter:
107+
; Check is character between a-z.
108+
cmp rdi, 'a'
109+
jl not_between_az
110+
111+
cmp rdi, 'z'
112+
jg not_between_az
113+
114+
mov rax, 1
115+
ret
116+
117+
not_between_az:
118+
; Check is character between A-Z.
119+
cmp rdi, 'A'
120+
jl not_between_AZ
121+
122+
cmp rdi, 'Z'
123+
jg not_between_AZ
124+
125+
mov rax, 1
126+
ret
127+
128+
not_between_AZ:
129+
mov rax, 0
130+
ret
131+
132+
lowercase:
133+
mov al, dil
134+
135+
cmp dil, 'A'
136+
jl lowercase_dont_change
137+
138+
cmp dil, 'Z'
139+
jg lowercase_dont_change
140+
141+
add al, 'a'
142+
sub al, 'A'
143+
lowercase_dont_change:
144+
ret

0 commit comments

Comments
 (0)