Kyle2595
Kyle2595

Reputation: 303

Push Each Character of a String Into a Stack MIPS

So I'm working on a project in MIPS to check if a string input by the user is a palindrome or not. The part I'm stuck on is reading the string and pushing each character of the string into the stack one by one (the PushLoop part of the code). When I debug it, the program seems to think I haven't entered anything at all. Here's what I have so far:

.text

main:

li $v0, 4                       # Prints str1
la $a0, str1
syscall

jal Init                        # Sets $s0 equal to $sp to compare if the stack is empty later

li $v0, 8                       # Read String
la $a0, buffer                  # Loads memory buffer (100)
li $a1, 100                     # Defines length of buffer
syscall

la $t0, buffer                  # Moves base register to $t0

PushLoop:   
    lb $a2, ($t0)               # Loads current character into $a2
    beqz $a2, fin               # if $a2 is equal to zero, the loop is terminated
    jal Push                    # Pushes what is stored in $a0 to the stack
    add $t0, $t0, -8            # Subtracts from buffer 
    j PushLoop

fin:
    la $t0, buffer              # Resets the memory buffer (I think)

PopLoop:
    jal IsEmpty                 # Checks if the stack is empty
    lb $a2, ($t0)               # Loads current character into $a2
    beq $v0, 1, isPal           # If stack is empty, jump to isPal
    jal Pop                     # Pops what is stored in the stack to $t1
    add $t0, $t0, -8            # Subtracts from buffer
    bne $a2, $t1, notPal
    j PopLoop


notPal:
    li $v0, 4                   # Prints str3
    la $a0, str3
    syscall

    li $v0, 0                   # loads 0 into $v0
    j end

isPal:
    li $v0, 4                   # Prints str2
    la $a0, str2
    syscall

    li $v0, 1                   # loads 1 into $v0
    j end

#EXIT
end: 
    li $v0, 10                  # ends the program
    syscall

Push:
    addi $sp, $sp, -8           # Move stack pointer
    sb $a2, ($sp)               # Store contents of $a2 at ($sp)
    jr $ra


Pop:
    lw $t1, ($sp)               # Pop char from stack and store in $t1
    addi $sp, $sp, 8            # Move stack pointer
    jr $ra

Init:
    add $s0, $sp, $zero         # Sets $s0 equal to $sp
    jr $ra


IsEmpty:
    beq $sp, $s0, Yes           # If $s0 is equal to the initial value of $sp, then stack is empty
    li $v0, 0                   # Loads 0 into $v0
    jr $ra

    Yes:
    li $v0, 1                   # Loads 1 into $v0
    jr $ra



 .data # Data declaration section
 str1: .asciiz "Please enter a String:  "
 str2: .asciiz "Is a palindrome!"
 str3: .asciiz "Is NOT a palindrome"

 buffer: .space 100

I'm sure there are more things wrong with the code, but I'm just trying to squash one bug at a time. Thanks so much for helping me out!

Upvotes: 0

Views: 4900

Answers (1)

Michael
Michael

Reputation: 58447

You're not using syscall 8 properly:

li $v0, 8                       # Read String
la $t0, buffer                  # Loads memory buffer (100)
syscall

If you read the description of syscall 8, it says "Arguments $a0 = buffer, $a1 = length". So those three lines of code should be changed into something like:

li $v0, 8                       # Read String
la $a0, buffer
li $a1, 100                  
syscall

Then you can do la $t0, buffer after the syscall if you still want to use $t0 as the base register for the memory reads in PushLoop.

Upvotes: 1

Related Questions