0

I'm trying to solve a problem to write assembly language program to detect if a phrase or characters entered by the user is a palindrome.

I've gotten this far, and I believe everything should work, but I'm wondering how I can implement this so that it takes an actual word to test. When I run in MARS, there's simply no input option.

.data
string_space: .space 1024
is_palin_msg: .asciiz "The string is a palindrome.\n"
not_palin_msg: .asciiz "The string is not a palindrome.\n"

.text
main: 
la $a0, string_space
li $a1, 1024
li $v0, 8
syscall

la $t1, string_space
la $t2, string_space

length_loop:
lb $t3, ($t2)
beqz $t3, end_length_loop
addu $t2, $t2, 1 
b length_loop

end_length_loop:
subu $t2, $t2, 2

test_loop:
bge $t1, $t2, is_palin

lb $t3, ($t1)
lb $t4, ($t2)
bne $t3, $t4, not_palin

addu $t1, $t1, 1
subu $t2, $t2, 1
b test_loop

is_palin: 
la $a0, is_palin_msg
li $v0, 4
syscall
b exit

not_palin:
la $a0, not_palin_msg
li $v0, 4
syscall
b exit

exit: 
li $v0, 10
syscall

I've tried

string_space: .asciiz "Enter your word:\n"

and also

string_space: .asciiz "racecar"

but I haven't quite been able to get it yet.

Any tips?

1 Answer 1

1

So - taking also as template this question here [you are strongly invited to check similar questions before posting] - you need to introduce in your code a .data section with an input string to ask the user to enter the string to check

.data
string_space: .space 1024

input:  .asciiz "Enter a string: "
is_palin_msg: .asciiz "The string is a palindrome.\n"
not_palin_msg: .asciiz "The string is not a palindrome.\n"  
# other strings you may need

Then you can start setting the logic to push them out

.text

main:
    li $v0, 4              # system call code for print_str
    la $a0, input          # address of string to print
    syscall                # print the input

    li $v0, 8              # code for syscall read_string
    la $a0, string_space   # tell syscall where the buffer is
    li $a1, 1024           # tell syscall how big the buffer is
    syscall    

    # double check the buffer content [see the next snippet]

# rest of the code to test      

You will start asking the user a string [in this case limited to 1024 bytes/characters]. Reading the "System Calls and I/O" section of this link you will find the same prompt used in the snippet above [search "Print out string (useful for prompts)" within the page]

The table in the same section will explain you the meaning of the li $v0, 4 and li $v0, 8 instructions. This is another good read. The same table will make you understand that before calling a print string you have to set one argument [$a0] while for the read string operation you will need two [$a0 and $a1]

In your code the main begins with a read string operation. But please notice that string_space is used both to allocate the size of the buffer where to read from [in .data section] and for asking to enter the word [you are trying to call string_space: .asciiz "Enter your word:\n"]. This issue has been fixed by the snippets above

In case of troubles don't forget to double check the content of your string_space buffer:

la $a0, string_space  # move buffer into a0
li $v0, 4             # print buffer
syscall

Complete code tested and working in MARS 4.5:

.data
string_space: .space 1024

input:  .asciiz "Enter a string: "
is_palin_msg: .asciiz "The string is a palindrome.\n"
not_palin_msg: .asciiz "The string is not a palindrome.\n"

.text
main: 

li $v0, 4              # system call code for print_str
la $a0, input          # address of string to print
syscall                # print the input

la $a0, string_space
li $a1, 1024
li $v0, 8
syscall

#la $a0, string_space  # move buffer into a0
#li $v0, 4             # print buffer
#syscall

la $t1, string_space
la $t2, string_space

length_loop:
lb $t3, ($t2)
beqz $t3, end_length_loop
addu $t2, $t2, 1 
b length_loop

end_length_loop:
subu $t2, $t2, 2

test_loop:
bge $t1, $t2, is_palin

lb $t3, ($t1)
lb $t4, ($t2)
bne $t3, $t4, not_palin

addu $t1, $t1, 1
subu $t2, $t2, 1
b test_loop

is_palin: 
la $a0, is_palin_msg
li $v0, 4
syscall
b exit

not_palin:
la $a0, not_palin_msg
li $v0, 4
syscall
b exit

exit: 
li $v0, 10
syscall

MARS 4.5 output

IMPORTANT: this code logic has a bug. If you enter palindromes whose length is odd it will give the wrong result [e.g. civic is detected as a non-palindrome but actually it is]

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply! I think I did have a .data and .text separation in the code I listed above. Is that along the right lines?
@Jonathan you are right... I scrolled down the code, started searching for the links provided and when typing the answer the visible code was beginning exactly from length_loop... check it out now and good luck!
Thanks! After all of that, I'm getting another type of error: Error in /Users/jonathanlin/Downloads/HW3Problem4.asm line 19: Runtime exception at 0x00400024: address out of range 0x00000000 Regarding the lb $t3, ($t2). Would you have any idea what this error entails?
@Jonathan I pasted the entire code that is working in MARS 4.5 [screenshot attached]. If you have other errors please don't use this question [which is about "I'm wondering how I can implement this so that it takes an actual word to test"] but please open a new one. Have a good day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.