3 Replies - 3385 Views - Last Post: 12 October 2012 - 05:13 AM

#1 andrex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

MIPS Program to find palidrome

Posted 08 October 2012 - 03:04 PM

I have to make a program in MIPS that uses three functions - get_longest, is_palindrome, and get_to_end - to determine which ascii string is the longest. I was given the shell of the program, so all I have to do is make the functions, but I'm having trouble. I keep getting arithmetic overloads and other exceptions, and I don't know why. This is the first time I've ever used MIPS, so it's been a lot of trouble figuring this out. I have what I thought should work, but it doesn't, and I can't figure out why. Any sort of hints or anything would be greatly appreciated.

	.data
pali0:	
	.asciiz "Go deliver a dare, vile dog"
pali1:	
	.asciiz "Some men interpret nine memos"
pali2:	
	.asciiz "Gateman sees name, garageman sees name tag"
pali3:	
	.asciiz "Ein Neger mit Gazelle zagtim Regen nie" 
pali4:	
	.asciiz "stressed  desserts"
pali5:
	.asciiz "palindromes"
pali6:	
	.asciiz "Drab as a fool, aloof as a bard"
pali7:	
	.asciiz "Tattarrattat"
pali8:	
	.asciiz "Socorram me subi no on ibus em Marrocos" 
pali9:	
	.asciiz "May a moody baby doom a yam"
test_str:
	.word  pali0, pali1, pali2, pali3, pali4, pali5, pali6, pali7, pali8, pali9
is_pali_msg: 
	.asciiz "The string is a palindrome."
not_pali_msg: 
	.asciiz "The string is not a palindrome."
newline:	
	.asciiz "\n"	
res_msg: 
	.asciiz "The longest palindrome is "
forward_msg:
	.space 400
backward_msg:
	.space 400

	.text
	.globl main
main:	la $a0, test_str		# Load the starting address of the array
	li $a1, 10			# the number of elements
	jal get_longest

	ori $t0, $v0, 0

	la $a0, res_msg	
	li $v0, 4			# print $a0
	syscall				
	
	ori $a0, $t0, 0
	li $v0, 4			# print $a0
	syscall				

	la $a0, newline
	li $v0, 4 			# print $a0
	syscall
	
done:	li $v0, 10			# exit	
	syscall


get_longest:
	addi $sp, $sp, -8
	sw $ra, 4($sp)
	sw $a0, 0($sp)
	li $s1, 0			# loop counter
	la $s0, 0($a0)
PAL:					# determines which are palindromes	
	lw $a0, 0($s0)			# stores the address of the palindrome into a0
	la $a0, 0($a0)
	addi $s1, $s1, 1
	jal is_palindrome		# call is_palindrome
	beq $v0, $0, NOT		# jump to NOT if string is not a palindrome
	ori $t1, $a0, 0			# store the length and address of the first palindrome
	ori $t0, $v1, 0
	addi $s0, $s0, 4
PALLOOP:
	la $a0, 0($s0)			# stores the address of the next palindrome into a0
	addi $s1, $s1, 1
	beq $s1, $a1, ENDPAL		# checks if loop is done
	jal is_palindrome		# call is_palindrome
	beq $v0, $0, NOTLOOP		# if string is not palindrome
	ori $t3, $a0, 0			# store the length and address of the next palindrome
	ori $t2, $v1, 0
	bge $t0, $t2, NOTLOOP		# if first length > second, continue
	add $t0, $t2, $0		# else copy second length and address to t0 and t1
	add $t1, $t3, $0
NOTLOOP:
	addi $s0, $s0, 4
	j PALLOOP
NOT:					# if the string is not a palindrome
	addi $s0, $s0, 4
	j PAL
ENDPAL:					# end of pal loop
	lw $a0, 0($sp)
	lw $ra, 4($sp)
	addi $sp, $sp, 8
	addi $v0, $t1, 0
	jr $ra


is_palindrome:
	addi $sp, $sp, -8
	sw $ra, 4($sp)
	sw $s0, 0($sp)
	jal get_to_end			# call get_to_end
	ori $t0, $a0, 0
	la $t1, forward_msg
	la $t2, backward_msg
	li $t4, 65			# ascii value for A
	li $t5, 90			# ascii value for Z
	li $t6, 97			# ascii value for a
	li $t7, 122			# ascii value for z
COPY:					# copies char if letter
	lbu $t3, 0($t0)
	beq $t3, $0, COPYDONE
	blt $t3, $t4, NO
	bgt $t3, $t5, MAYBE
UPPER:					# if char is uppercase, changes to lower
	addi $t3, $t3, 32
LOWER:
	sb $t3, 0($t1)
	addi $sp, $sp, -4
	sb $t3, 0($sp)
	addi $t1, $t1, 1
	addi $t0, $t0, 1
	j COPY
MAYBE:					# char is above upper, but not known
	blt $t3, $t6, NO
	bgt $t3, $t7, NO
	j LOWER
NO:					# char is not letter
	addi $t0, $t0, 1
	j COPY
COPYDONE:
	li $t0, 0			# loop counter
REVCOPY:				# copies char string in reverse
	addi $t2, $t2, 1
	lbu $t3, 0($sp)
	addi $sp, $sp, 4
	sb $t3, 0($t2)
	addi $t0, $t0, 1
	beq $t0, $v1, REVDONE
	j REVCOPY
REVDONE:
	li $t0, 1
	la $t1, forward_msg
	la $t2, backward_msg
CHECK:
	lb $t3, 0($t1)
	lb $t4, 0($t2)
	bne $t3, $t4, FALSE		# if string is same forward and back, then equal
	beq $t0, $v1, TRUE
	addi $t1, $t1, 1
	addi $t2, $t2, 1
	addi $t0, $t0, 1
	j CHECK
TRUE:
	li $v0, 1
	j PALDONE
FALSE:
	li $v0, 0
PALDONE:
	lw $s0, 0($sp)
	lw $ra, 4($sp)
	addi $sp, $sp, 8
	jr $ra


get_to_end:
	addi $sp, $sp, -4
	sw $ra, 4($sp)
	la $t1, 0($a0)
	li $t3, 0
	li $t0, 0			#counts how many chars
	li $t4, 65			# ascii value for A
	li $t5, 90			# ascii value for Z
	li $t6, 97			# ascii value for a
	li $t7, 122			# ascii value for z
CHARCHECK:				# goes through and counts the number of letter chars
	lb $t3, 0($t1)
	beq $t3, $0, RUNDONE
	la $t2, 0($t3)
	blt $t3, $t4, NOCHAR
	bgt $t3, $t5, MAYBECHAR
	j CHAR
MAYBECHAR:				# if the character is above upper case and might be lower case
	blt $t3, $t6, NOCHAR
	bgt $t3, $t7, NOCHAR
	j CHAR 
NOCHAR:					# if the char is not a letter
	addi $t1, $t1, 1
	j CHARCHECK
CHAR:					# if the char is a letter
	addi $t0, $t0, 1
	addi $t1, $t1, 1
	j CHARCHECK
RUNDONE:
	add $v0, $t2, $0
	add $v1, $t0, $0
	lw $ra, 4($sp)
	addi $sp, $sp, 4
	jr $ra
	

Like I said, I've never used MIPS before, so the code is probably really sloppy. I don't need someone to give me the code completed or anything, just some suggestions or something as to what I'm doing wrong here.

Is This A Good Question/Topic? 0
  • +

Replies To: MIPS Program to find palidrome

#2 andrex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

Re: MIPS Program to find palidrome

Posted 08 October 2012 - 04:25 PM

#EDIT#
I have made some leeway with this, the program now runs all the way through. It just isn't giving me the right answer. But that's better than it was before. My code is still a mess, but I'm getting there.

	.data
pali0:	
	.asciiz "Go deliver a dare, vile dog"
pali1:	
	.asciiz "Some men interpret nine memos"
pali2:	
	.asciiz "Gateman sees name, garageman sees name tag"
pali3:	
	.asciiz "Ein Neger mit Gazelle zagtim Regen nie" 
pali4:	
	.asciiz "stressed  desserts"
pali5:
	.asciiz "palindromes"
pali6:	
	.asciiz "Drab as a fool, aloof as a bard"
pali7:	
	.asciiz "Tattarrattat"
pali8:	
	.asciiz "Socorram me subi no on ibus em Marrocos" 
pali9:	
	.asciiz "May a moody baby doom a yam"
test_str:
	.word  pali0, pali1, pali2, pali3, pali4, pali5, pali6, pali7, pali8, pali9
is_pali_msg: 
	.asciiz "The string is a palindrome."
not_pali_msg: 
	.asciiz "The string is not a palindrome."
newline:	
	.asciiz "\n"	
res_msg: 
	.asciiz "The longest palindrome is "
forward_msg:
	.space 400
backward_msg:
	.space 400

	.text
	.globl main
main:	la $a0, test_str		# Load the starting address of the array
	li $a1, 10			# the number of elements
	jal get_longest

	ori $t0, $v0, 0

	la $a0, res_msg	
	li $v0, 4			# print $a0
	syscall				
	
	ori $a0, $t0, 0
	li $v0, 4			# print $a0
	syscall				

	la $a0, newline
	li $v0, 4 			# print $a0
	syscall
	
done:	li $v0, 10			# exit	
	syscall


get_longest:
	addi $sp, $sp, -8
	sw $ra, 4($sp)
	sw $a0, 0($sp)
	li $s1, 0			# loop counter\
	la $s0, 0($a0)
PAL:					# determines which are palindromes	
	lw $a0, 0($s0)			# stores the address of the palindrome into a0
	addi $s1, $s1, 1
	jal is_palindrome		# call is_palindrome
	beq $v0, $0, NOT		# jump to NOT if string is not a palindrome
	ori $t1, $a0, 0			# store the length and address of the first palindrome
	ori $t0, $v1, 0
	addi $s0, $s0, 4
PALLOOP:
	la $a0, 0($s0)			# stores the address of the next palindrome into a0
	addi $s1, $s1, 1
	beq $s1, $a1, ENDPAL		# checks if loop is done
	jal is_palindrome		# call is_palindrome
	beq $v0, $0, NOTLOOP		# if string is not palindrome
	ori $t3, $a0, 0			# store the length and address of the next palindrome
	ori $t2, $v1, 0
	bge $t0, $t2, NOTLOOP		# if first length > second, continue
	add $t0, $t2, $0		# else copy second length and address to t0 and t1
	add $t1, $t3, $0
NOTLOOP:
	addi $s0, $s0, 4
	j PALLOOP
NOT:					# if the string is not a palindrome
	addi $s0, $s0, 4
	j PAL
ENDPAL:					# end of pal loop
	lw $a0, 0($sp)
	lw $ra, 4($sp)
	addi $sp, $sp, 8
	addi $v0, $t1, 0
	jr $ra


is_palindrome:
	addi $sp, $sp, -4
	sw $ra, 0($sp)
	jal get_to_end			# call get_to_end
	ori $t0, $a0, 0
	la $t1, forward_msg
	la $t2, backward_msg
	li $t4, 65			# ascii value for A
	li $t5, 90			# ascii value for Z
	li $t6, 97			# ascii value for a
	li $t7, 122			# ascii value for z
	addi $sp, $sp, -1
	sb $0, 0($sp)
COPY:					# copies char if letter
	lb $t3, 0($t0)
	beq $t3, $0, COPYDONE
	blt $t3, $t4, NO
	bgt $t3, $t5, MAYBE
UPPER:					# if char is uppercase, changes to lower
	addi $t3, $t3, 32
LOWER:
	sb $t3, 0($t1)
	addi $sp, $sp, -1
	sb $t3, 0($sp)
	addi $t1, $t1, 1
	addi $t0, $t0, 1
	j COPY
MAYBE:					# char is above upper, but not known
	blt $t3, $t6, NO
	bgt $t3, $t7, NO
	j LOWER
NO:					# char is not letter
	addi $t0, $t0, 1
	j COPY
COPYDONE:
	li $t0, 0			# loop counter
REVCOPY:				# copies char string in reverse
	lb $t0, 0($sp)
	addi $sp, $sp, 1
	sb $t0, 0($t2)
	beq $t0, $0, REVDONE
	addi $t2, $t2, 1
	j REVCOPY
REVDONE:
	li $t0, 1
	la $t1, forward_msg
	la $t2, backward_msg
CHECK:
	lb $t3, 0($t1)
	lb $t4, 0($t2)
	bne $t3, $t4, FALSE		# if string is same forward and back, then equal
	beq $t4, $0, TRUE
	addi $t1, $t1, 1
	addi $t2, $t2, 1
	j CHECK
TRUE:
	li $v0, 1
	j PALDONE
FALSE:
	li $v0, 0
PALDONE:
	lw $ra, 0($sp)
	addi $sp, $sp, 4
	jr $ra


get_to_end:
	addi $sp, $sp, -4
	sw $ra, 0($sp)
	la $t1, 0($a0)
	li $t3, 0
	li $t0, 0			#counts how many chars
	li $t4, 65			# ascii value for A
	li $t5, 90			# ascii value for Z
	li $t6, 97			# ascii value for a
	li $t7, 122			# ascii value for z
CHARCHECK:				# goes through and counts the number of letter chars
	lb $t3, 0($t1)
	beq $t3, $0, RUNDONE
	la $t2, 0($t3)
	blt $t3, $t4, NOCHAR
	bgt $t3, $t5, MAYBECHAR
	j CHAR
MAYBECHAR:				# if the character is above upper case and might be lower case
	blt $t3, $t6, NOCHAR
	bgt $t3, $t7, NOCHAR
	j CHAR 
NOCHAR:					# if the char is not a letter
	addi $t1, $t1, 1
	j CHARCHECK
CHAR:					# if the char is a letter
	addi $t0, $t0, 1
	addi $t1, $t1, 1
	j CHARCHECK
RUNDONE:
	add $v0, $t2, $0
	add $v1, $t0, $0
	lw $ra, 0($sp)
	addi $sp, $sp, 4
	jr $ra
	

Was This Post Helpful? 0
  • +
  • -

#3 andrex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

Re: MIPS Program to find palidrome

Posted 08 October 2012 - 08:22 PM

*ANOTHER UPDATE*
For anyone who cares, I've worked more and tweaked it, but I still need some help. Now my program says the longest palindrome is interpret nine memos, and that is wrong in multiple ways. If someone could give some feedback, that'd be great.

	.data
pali0:	
	.asciiz "Go deliver a dare, vile dog"
pali1:	
	.asciiz "Some men interpret nine memos"
pali2:	
	.asciiz "Gateman sees name, garageman sees name tag"
pali3:	
	.asciiz "Ein Neger mit Gazelle zagtim Regen nie" 
pali4:	
	.asciiz "stressed desserts"
pali5:
	.asciiz "palindromes"
pali6:	
	.asciiz "Drab as a fool, aloof as a bard"
pali7:	
	.asciiz "Tattarrattat"
pali8:	
	.asciiz "Socorram me subi no on ibus em Marrocos" 
pali9:	
	.asciiz "May a moody baby doom a yam"
test_str:
	.word  pali0, pali1, pali2, pali3, pali4, pali5, pali6, pali7, pali8, pali9
is_pali_msg: 
	.asciiz "The string is a palindrome."
not_pali_msg: 
	.asciiz "The string is not a palindrome."
newline:	
	.asciiz "\n"	
res_msg: 
	.asciiz "The longest palindrome is "
forward_msg:
	.space 400
backward_msg:
	.space 400

	.text
	.globl main
main:	la $a0, test_str		# Load the starting address of the array
	li $a1, 10			# the number of elements
	jal get_longest

	ori $t0, $v0, 0

	la $a0, res_msg	
	li $v0, 4			# print $a0
	syscall				
	
	ori $a0, $t0, 0
	li $v0, 4			# print $a0
	syscall				

	la $a0, newline
	li $v0, 4 			# print $a0
	syscall
	
done:	li $v0, 10			# exit	
	syscall


get_longest:
	addi $sp, $sp, -8
	sw $ra, 4($sp)
	sw $a0, 0($sp)
	li $s1, 0			# loop counter\
	lw $s0, 0($a0)
	li $t1, 0
	li $t0, 0
	li $t4, 0
PALLOOP:
	la $a0, 0($s0)
	la $t2, 0($s0)			# stores the address of the next palindrome into a0
	addi $s1, $s1, 1
	beq $s1, $a1, ENDPAL		# checks if loop is done
	jal is_palindrome		# call is_palindrome
	beq $v0, $0, NOTLOOP		# if string is not palindrome
	ori $t3, $a0, 0			# store the length and index of the next palindrome
	bge $t0, $t2, NOTLOOP		# if first length > second, continue
	add $t0, $t2, $0		# else copy second length and address to t0 and t1
	add $t1, $t3, $0
NOTLOOP:
	addi $s0, $s0, 4
	j PALLOOP
ENDPAL:					# end of pal loop
	lw $a0, 0($sp)
	lw $ra, 4($sp)
	addi $sp, $sp, 8
	ori $v0, $t2, 0
	jr $ra


is_palindrome:
	addi $sp, $sp, -16
	sw $ra, 0($sp)
	sw $a0, 4($sp)
	sw $s0, 8($sp)
	sw $s1, 12($sp)
	jal get_to_end			# call get_to_end
	ori $t0, $a0, 0
	la $t1, forward_msg
	la $t2, backward_msg
	li $t4, 65			# ascii value for A
	li $t5, 90			# ascii value for Z
	addi $sp, $sp, -1
	sb $0, 0($sp)
COPY:					# copies char
	lb $t3, 0($t0)
	beq $t3, $0, COPYDONE
	blt $t3, $t5, MAYBE
LOWER:
	sb $t3, 0($t1)
	addi $sp, $sp, -1
	sb $t3, 0($sp)
	addi $t1, $t1, 1
	addi $t0, $t0, 1
	j COPY
MAYBE:					# char is above upper, but not known
	blt $t3, $t4, LOWER
	addi $t3, $t3, 32
	j LOWER
COPYDONE:
REVCOPY:				# copies char string in reverse
	lb $t0, 0($sp)
	addi $sp, $sp, 1
	sb $t0, 0($t2)
	beq $t0, $0, REVDONE
	addi $t2, $t2, 1
	j REVCOPY
REVDONE:
	li $t0, 1
	la $t1, forward_msg
	la $t2, backward_msg
CHECK:
	lb $t3, 0($t1)
	lb $t4, 0($t2)
	bne $t3, $t4, FALSE		# if string is same forward and back, then equal
	beq $t4, $0, TRUE
	addi $t1, $t1, 1
	addi $t2, $t2, 1
	j CHECK
TRUE:
	li $v0, 1
	j PALDONE
FALSE:
	li $v0, 0
PALDONE:
	add $v1, $v1, $0
	lw $s0, 8($sp)
	lw $s1, 12($sp)
	lw $a0, 4($sp)
	lw $ra, 0($sp)
	addi $sp, $sp, 16
	jr $ra


get_to_end:
	addi $sp, $sp, -4
	sw $ra, 0($sp)
	la $t1, 0($a0)
	li $t3, 0
	li $t0, 0			#counts how many chars
CHARCHECK:				# goes through and counts the number of chars
	lb $t3, 0($t1)
	beq $t3, $0, RUNDONE
	la $t2, 0($t1)
	addi $t0, $t0, 1
	addi $t1, $t1, 1
	j CHARCHECK
RUNDONE:
	add $v0, $t2, $0
	add $v1, $t0, $0
	lw $ra, 0($sp)
	addi $sp, $sp, 4
	jr $ra
	

Was This Post Helpful? 0
  • +
  • -

#4 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: MIPS Program to find palidrome

Posted 12 October 2012 - 05:13 AM

Could you put comments about register usages in each subroutine?
It's hard to follow, and I suspect there is something funny going on with them.

039 main: la $a0, test_str # Load the starting address of the array
040 li $a1, 10 # the number of elements
041 jal get_longest
...
066 lw $s0, 0($a0) # Here $a0 contains the address of test_str
...

070 PALLOOP:
071 la $a0, 0($s0)
072 la $t2, 0($s0) # stores the address of the next palindrome into a0
073 addi $s1, $s1, 1
074 beq $s1, $a1, ENDPAL

Where is the "result" put in $t0?

049 ori $a0, $t0, 0
050 li $v0, 4 # print $a0
051 syscall
(I understand that here $t0 should point to the string to be printed?)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1