PDA

View Full Version : MIPS is destroying my will to live



yourule97
02-13-2016, 09:17 PM
Hi everyone, I'm currently learning MIPS in an architecture class, and one program which we have to write is one that prints the first N integer palindromes (starting with 11). I've been working on this for an ungoldy amount of time, but I have no idea what's wrong. Could someone steer a lost soul in the right direction?


I'm pretty sure the reverse procedure works, I just don't know why it doesn't recognize anything other than 11 as a palindrome




.data
strPromptFirst: .asciiz "Enter Number: "
strResult: .asciiz "\nThe first n palindromes are: \n"
newLine: .asciiz "\n"
numPalindromes: .word 0
.text
.globl main

main:
#Print String
li $v0, 4
la $a0, strPromptFirst
syscall

#Read in num integers to print
li $v0, 5
syscall
move $s0, $v0 #move that variable to s0

li $t0, 0 #Num palindromes found
li $t1, 11 #Starting palindrome

loop:
bgt $t0, $s0, exit #If numb palindromes have been found, then exit
la $t4, ($t1)
move $a0, $t1
jal palindromes
add $t2, $v0, $zero

bne $t4, $t2, skip #If it's not equal, skip ahead

addi $t0, $t0, 1 #Increment number of palindrome found

li $v0, 1 #Print out number
la $a0, ($t2)
syscall

li $v0, 4 #Print out new line
la $a0, newLine
syscall

j skip

skip:
move $t1, $t4 #Restore value
addi $t1, $t1, 1 #incrememnt original by 1
j loop

palindromes:
addi $sp, $sp, -16
sw $ra, 0($sp)
sw $t0, 4($sp)
sw $t1, 8($sp)
sw $t2, 12($sp)

move $t0, $a0 #Set t0 to equal to starting num

wloop:
beqz $t0, breakLoop
rem $t1, $t0, 10 #t1 = num % 10
div $t0, $t0, 10
mul $t2, $t2, 10 #rem = rem * 10
add $t2, $t2, $t1 #rem = rem + t1
j wloop

breakLoop:
move $v0, $t2
lw $ra, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
lw $t2, 12($sp)
addi $sp, $sp, 16
jr $ra



exit:
li $v0, 10 # Syscall number 10 is to terminate the program
syscall # exit now





Any help is much appreciated!


EDIT: It looks like $v0 contains a weird value when returned. Goes from 11 -> 1121. I have no idea why that's happening though, will keep checking my reverse method

yourule97
02-13-2016, 09:38 PM
Got it. For some reason, I have to reset the value of $t2 in the reverse function to 0.

Time to clean up the mess of code I left

Sin
02-14-2016, 12:50 AM
God bless your soul. I was in grade 4 when I first started doing MIPS (for PSP subroutines) and trust me I nearly gave up on computers because of it.