MARS need help~ Using MARS, write a program that determines if an ASCII string e
ID: 3563267 • Letter: M
Question
MARS need help~
Using MARS, write a program that determines if an ASCII string entered into the console is a palindrome. A palindrome is a string that is the same forwards and backwards. For example, the word radar is a palindrome, but the word Radar is not. The string of ASCII characters is input into a single line on the console followed by a new line (carriage return) with code like
inpt: li $v0, 8 #sys call code for read_str
syscall #read it
The read_string input command (system call code 8) reads up to n-1 ASCII characters into a buffer and terminates the string with a null byte. The string is left in a buffer in memory whose address is contained in $a0 and its length is recorded in $a1. Your program should determine if the string is a palindrome and then output to the console
The string a palindrome
Console output is accomplished by placing the ASCII characters to be output in memory at the address contained in $a0 and issuing a print_string output command (system call code 4) as follows
oupt: li $v0, 4 #sys call code for print_str
syscall #print it
You may assume the input string will be no more than ten characters.
if possible put the file in the drop box ty~
Explanation / Answer
.data
ask_user : .asciiz "Please enter a string:"
the_string: .asciiz "The String "
invalid_str: .asciiz " -1 "
string_buffer : .space 10
the_string1: .asciiz " is not a palindrome"
the_string2: .asciiz " is a palindrome"
.text
main :
li $v0,4 #syscall to print string
la $a0,ask_user
syscall
li $v0,8 #syscall for read_str
la $a0,string_buffer #address to store the read input
li $a1,10 #maximum 10 characters
syscall
#first find the end of string and check of any non alpha character is passed
la $a0,string_buffer #load the starting address
loop:
lb $t1,0($a0) #load the character
addi $a0,$a0,1 #increment the address
#all the alpha has ascii values in range 0x41h-0x5Ah and 0x61h-0x7Ah
ori $t2,$t1,0x20 #converts all the aplha to range 0x61h-0x7Ah
li $t3,0x61
blt $t2,$t3,invalid
li $t3,0x7A
bgt $t2,$t3,invalid
bne $t1,$0,loop #continue until end of string. string is terminated by a null byte
#for a string to be palindrom string[x] = string[(size-1)-x] ie string[0] = string[last], string[1] = string[last-1]
#$a0 has the address to the null terminating byte => $a0 is the last address
loopend:
sb $0,-1($a0) #replace the new line string with 0
subi $a0,$a0,2 #as the last character will be new line subtract 2 to get the address of last character
la $a1,string_buffer #$a1 has the first address of the string
loop1:
lb $t0,0($a0)
lb $t1,0($a1)
bne $t0,$t1,notpalindrome
addi $a1,$a1,1
subi $a0,$a0,1
bge $a0,$a1,loop1
b palindrome
forever:
li $v0, 10 #exit syscall
syscall
invalid:
li $t2,10#ascii equivalent of new line as mars adds an extra new line to the end
beq $t1,$t2,loopend
li $v0,4 #syscall to print string
la $a0,invalid_str
syscall
b forever
notpalindrome:
li $v0,4 #syscall to print string
la $a0,the_string
syscall
li $v0,4 #syscall to print string
la $a0,string_buffer
syscall
li $v0,4 #syscall to print string
la $a0,the_string1
syscall
b forever
palindrome:
li $v0,4 #syscall to print string
la $a0,the_string
syscall
li $v0,4 #syscall to print string
la $a0,string_buffer
syscall
li $v0,4 #syscall to print string
la $a0,the_string2
syscall
b forever
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.