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
|
# CSCI 424, Homework 2
# Adam Carpenter - username acarpent - acarpenter@email.wm.edu
.text
.globl main
main:
# Get the string to test and store it.
jal Input_string
la $s0, ($a0)
# ---Make a reversed copy of the string---
la $a0, buffer2 # set aside space for new string
la $s1, ($a0) # set s1 to space for new string
la $t0, ($s0) # create address pointer
add $t1, $zero, 10 # create index
copier:
add $t0, $s0, $t1 # point to character at index
lb $t2, ($t0) # load byte located at index
beq $t2, 0, skipJunk # Skip null characters
beq $t2, 10, skipJunk # Skip new lines
sb $t2, ($a0) # store byte in reversed string
addi $a0, $a0, 1 # increment reversed string pointer
skipJunk:
addi $t1, $t1, -1 # decrement string pointer
bne $t1, -1, copier # if $t1 != -1, repeat the process
# Now s0 points to the original string and
# s1 points to the reverse of the original
# string, which has been stripped of
# whitespace and null characters
# ----------------------------------------
# Start printing out the response
la $a0, str1
jal Print_string
la $a0, ($s0)
jal Print_string
la $a0, str2
jal Print_string
# ---Loop to check if strings match---
add $t0, $zero, $zero # flag marks whether string is palindrome
add $t1, $zero, $zero # create index
checkPalindrome:
add $t2, $s0, $t1 # point to character at next index
lb $s2, ($t2) # load character into t2
beq $s2, 10, endLoop # end loop if newline
add $t3, $s1, $t1 # point to character at same index
lb $s3, ($t3) # load character into t3
sub $t0, $s2, $s3 # if the two characters match, flag will remain 0
bne $t0, $zero, notPalindrome # if flag is not zero, it's not a palindrome
addi $t1, $t1, 1 # increment index
jal checkPalindrome
endLoop:
beq $t0, $zero, isPalindrome
# This loop determines if the string is a
# palindrome by comparing it to the
# reversed duplicate string. The loop terminates
# when it finds the carriage return in the
# original string and jumps based on
# how the flag was set.
# ------------------------------------
notPalindrome: # The string is NOT a palindrome
la $a0, str3
jal Print_string
jal Exit
isPalindrome: # The string is a palindrome
la $a0, str4
jal Print_string
jal Exit
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.data
.align 0
str1:
.asciiz "\nThe string "
str2:
.asciiz "is "
str3:
.asciiz "not a palindrome!\n"
str4:
.asciiz "a palindrome!\n"
buffer:
.space 12
buffer2:
.space 12
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Wrapper functions around some of the system calls
# From P&H COD, Fig. A.9.1
.text
.globl Input_string
Input_string: # take in a string from keyboard. String is in $a0 and length is in $a1
la $a0, buffer # Place result in location of myString
li $a1, 12 # Maximum length for string
addi $v0, $zero, 8
syscall
jr $ra
.globl Print_string
Print_string: # print the string whose starting address is in register a0
addi $v0, $zero, 4
syscall
jr $ra
.globl Exit
Exit: # end the program, no explicit return status
addi $v0, $zero, 10
syscall
jr $ra
.globl Exit2
Exit2: # end the program, with return status from register a0
addi $v0, $zero, 17
syscall
jr $ra
|