-
Notifications
You must be signed in to change notification settings - Fork 8
/
palindrome.asm
58 lines (49 loc) · 947 Bytes
/
palindrome.asm
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
# check if user provided string is palindrome
.data
welcomeMsg: .asciiz "Enter a string: "
userString: .space 256
newline: .asciiz "\n"
yes: .asciiz "IS PALINDROME"
no: .asciiz "NOT PALINDROME"
.text
main:
li $v0, 4
la $a0, welcomeMsg
syscall
la $a0, userString
li $a1, 256
li $v0, 8
syscall
li $v0, 4
la $a0, userString
syscall
lb $t5, newline
# store address of last element of userString in $a1
add $t1, $a0, $zero
lengthZero:
lb $t3, ($t1)
addi $t1, $t1, 1
beq $t3, $t5, done # this step is required as MIPS reads newline after string
bne $t3, $zero, lengthZero
done:
addi $t1, $t1, -2
# check if palindrome
add $t0, $a0, $zero
check_letter:
lb $t2, ($t0)
lb $t3, ($t1)
bne $t2, $t3, notPalindrome
addi $t0, $t0, 1
addi $t1, $t1, -1
blt $t0, $t1, check_letter
li $v0, 4
la $a0, yes
syscall
j endProgram
notPalindrome:
li $v0, 4
la $a0, no
syscall
endProgram:
li $v0, 10
syscall