Push Each Character of a String Into a Stack MIPS - string

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!

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.

Related

Mips error printing length of string

This seems like it should work but clearly doesn't. When I try to count the length of the string it returns some crazy long number. When I one step the program it works fine but prints out the wrong number. Can somebody point me back on track so that I can work on the next function? I just need to figure out what is throwing off the count
############################################
# Program Name: strings.s
# Programmer: David Bierbrauer
# Date: 9/07/2017
############################################
# Functional Description:
#
#
############################################
# Cross References:
#
#
############################################
.data # Data declaration section
stringa: .asciiz "Enter the first string: "
stringb: .asciiz "\nEnter the second string: "
.text
main: # Start of code section
#li $v0, 4 #get first string input
la $a0, stringa
jal read
jal length
jal print
#get second string input
la $a0, stringb
jal read
jal length
jal print
j end
length:
li $t0, -1 #loop count 0
j lengthloop
lengthloop: #exit address $s1
lb $t1, 0($a0) #load next char into $t1
beqz $t1, endlength
addi $a0, $a0, 1 #increment string pointer
addi $t0, $t0, 1 #increment count
j lengthloop
endlength:
jr $ra
equality:
lb $t2, 0($a0)
lb $t3, 0($a1)
bne $t2,$t3 notequal
append:
print:
li $v0, 1
la
syscall
jr $ra
read:
li $v0, 4
syscall #print prompt
li $v0,9
li $a0,80
syscall
move $a0,$v0
li $v0,8
li $a1,80
syscall
jr $ra
end:
li $v0, 10
syscall
The code you've posted doesn't even assemble, because there's a stray la in the print routine.
Aside from that, your length routine returns the length in $t0, while your print routine assumes that the value is in $a0. That's why you get the wrong output.
Your print looks odd. spim yells at me - bad syntax of la pseudoinstruction. I guess, you mean
print:
li $v0, 1
la $a0, 0($t0)
syscall
jr $ra

Reverse string in MIPS

I am trying to write a program that gets a user string input and reverse that string in MIPS.
However, I must be doing something horribly wrong as it doesn't just display the user input in reverse but, it also reverses the prompt to the user. It seems that the user input is not identified with a null(zero?) character in the end.
.data
prompt: .asciiz "Please enter your name. You're only permitted 20 characters. \n"
userInput: .space 20 #user is permitted to enter 20 characters
.globl main
.text
main:
# user prompt
li $v0, 4
la $a0, prompt
syscall
# getting the name of the user
li $v0, 8
la $a0, userInput
li $a1, 20
syscall
add $t0, $a0, $0 # loading t0 with address of array
strLength:
lbu $t2, 0($t0)
beq $t2, $zero, Exit # if reach the end of array, Exit
addiu $t0, $t0, 1 # add 1 to count the length
j strLength
Exit:
add $t1, $t0, $0 # t1 = string length
li $t2, 0 # counter i = 0
li $v0, 11
reverseString:
slt $t3, $t2, $t1 # if i < stringlength
beq $t3, $0, Exit2 # if t3 reaches he end of the array
addi $t0, $t0, -1 # decrement the array
lbu $a0, 0($t0) # load the array from the end
syscall
j reverseString
Exit2:
li $v0, 10
syscall
Problem number 1:
add $t1, $t0, $0 #t1 = string length
What you're assigning to $t1 here isn't the length of the string; it's the address of the first byte past the end of the string.
Problem number 2 is that you never increment $t2 (or decrement $t1) within the reverseString loop.
I suggest that you make use of the debugging features in SPIM/MARS (like the ability to set breakpoints and single-step through the code), as that would've made finding these problems yourself fairly simple.

Checksum assembly algorithm not adding up to correct value

I'm working on implementing a basic checksum algorithm for an inputted string in MIPS assembly as a general introduction to working in the language, and I could use some error checking.
Here's what I have so far, comments included to keep track of what's happening each step:
.data
str1:
.asciiz "This is a short string."
cs1:
.word 0x84a
str2:
.asciiz "This is a much longer string. In fact, it has two sentences in it, and some funny characters (~`)€."
cs2:
.word 0x230a
NonSuccessString:
.asciiz "You have more work to do."
Test1Success:
.asciiz "Test 1 was successful. "
Test2Success:
.asciiz "Test 2 was successful. "
.text
la $a0, str1 #Set $a0 to address of label str1:
jal checksum #Set $ra to address of next instruction, then jump to label checksum:
la $s0, cs1 #Set $s0 to address of label cs1:
lw $s1, 0($s0) #Set $s1 to contents of memory address $s0
beq $v0, $s1, Success1 # If $v0 and $sl are equal, jump to label Success1:
j NonSuccess # Jump to label NonSuccess:
Success1:
la $a0, Test1Success #Set $a0 to address of label Test1Success:
addi $v0, $zero, 4
syscall
la $a0, str2 #Set $a0 to address of label str2:
jal checksum #Set $ra to return address, then jump to label checksum:
la $s0, cs2 #Set $s0 to address of label cs2:
lw $s1, 0($s0) #Set $s1 to contents of memory address $s0
beq $v0, $s1, Success2 # If $v0 and $sl are equal, jump to label Success2:
j NonSuccess # Jump to label NonSuccess:
Success2:
la $a0, Test2Success #Set $a0 to address of label Test2Success:
addi $v0, $zero, 4
syscall
j Quit # Jump to label Quit:
NonSuccess:
la $a0, NonSuccessString #Set $a0 to address of label NonSuccessString:
addi $v0, $zero, 4
syscall
Quit:
addi $v0, $zero, 10
syscall
checksum:
addi $sp, $sp, -4 # adjust stack for 1 item
sw $s0, 0($sp) # save $s0
add $s0, $zero, $zero # i = 0
L1:
add $t1, $s0, $a0 # Locate the character at y[i]
lbu $t2, 0($t1) # Load the unsigned byte value of y[i]
beq $t2, $zero, L2 # exit loop if y[i] == 0
add $v0, $v0, $t2 # Add the byte to the total value
addi $s0, $s0, 1 # i = i + 1
j L1
L2:
jr $ra #jump back to $ra
The simple string in the first test calculates correctly and I get the first "Test 1 was successful." message, but on the second checksum the debug reports a sum of 0x230e, 4 more than the expected value, meaning something wonky happened. I'm guessing the special characters are probably causing the issue, but I don't know for sure. Any ideas?
You're not resetting $v0 prior to calculating the checksum. There should be an add $v0,$zero,$zero before the L1: label.
But I still don't see where the value 0x230a comes from. It seems to me like the correct checksum for the second string is 0x22de.

MIPS randomized word scrambler

I am trying to write code that uses the MARS (my MIPS simulator) pseudorandom number generator to pick a random char in the string, take it out of memory and into a register, and replace that char in memory with an asterisk, '*'.
So far, it only scrambles part of the word, and it's driving me insane. I can't find what in this code isn't working. I don't even need a direct answer, just hints/tips would be SO helpful.
Here is the code:
#this loop extracts a char at random from a string in memory, stores it in a register, and replaces the char in the string with an asterisk '*'
.data
.align 2
string0: .ascii "Tyler\n"
.align 2
endString: .asciiz "Loop completed!\n"
.align 2
scrambleString: .asciiz
.text
#counter
li $t0, 5
#pointer to string0
la $s0, string0
loop2:
#is counter = 0? go to loop3 if so
beq $t0, $0, loop3
#seed & prepare randomized number generator
li $v0, 30
syscall
li $v0, 40 #sets seed
syscall
#generates random number in $a0, with the coUnter $t0 being the upper bound
addi $a1, $t0, 1
li $v0, 42
syscall
#add STRING POINTER by random number in $a0, store this new address in $t1
#addi $a0, $a0, 1
add $t1, $s0, $a0
#srlv $t1, $s0, $a0
#isolates that bytesized char, puts it into $t2
lbu $t2, ($t1)
#beq $t2, 0x5c, loop2
#replaces char in original string with "*"
li $t3, 0x2a
sb $t3, ($t1)
beq $t1, $t3, loop2
#decrement counter
addi $t0, $t0, -1
#loop return
j loop2
loop3:
la $a0, string0
li $v0, 4
syscall
li $v0, 10
syscall
You are resetting the random number seed on every iteration of
your loop (loop2:).
Syscalls 40 and 42 each take 2 parameters which should be in $a0 and
$a1. See here.
The temporary registers $t0,...,$t9 get altered by each
syscall. You should be using the callee saved registers
$s0,...$s8 instead.

Removing spaces in a string, MIPS assembly

I have a small problem with a program i'm trying to write. Basically i'm prompting a user for input. I then count the number of spaces in the string, and display the count. I also need to print out the original string with spaces removed.
The bug I'm having is when i'm adding characters to my string without spaces, it only adds the first character. $t2 is the register that is holding the character to be added, and I've checked the registers during run-time to be sure the value there was changing. I don't know where else to look.
.data
str: .space 81 # buffer for input string
strNS: .space 81 # buffer for string w/o spaces
prompt: .asciiz "Enter a string up to 80 characters\n"
head1: .asciiz "\nOriginal String: "
head2: .asciiz "\nNumber of spaces: "
head3: .asciiz "\nWith spaces removed: "
.text
main:
#print the first prompt and get the input string from console
li $v0, 4 #load syscall value to print string into $v0
la $a0, prompt #address of prompt to print
syscall #print prompt to console
li $v0, 8 #load syscall value to read input string
la $a0, str #addr of allocated space for input string is now in $a0
li $a1, 81
syscall
jal countSpace
addi $t1, $v0, 0 #the count of spaces is in $v0, save it into $t1
li $v0, 4 #print header then the count
la $a0, head1
syscall
la $a0, str #print the original string
syscall
la $a0, head2 #print second header before printing count
syscall
li $v0, 1
addi $a0, $t1, 0 #place the count in $a0
syscall #print the count
li $v0, 4
la $a0, head3 #print the third header
syscall
la $a0, strNS #print no spaces string
syscall
End:
li $v0, 10 #load syscall value for exit
syscall #exit
countSpace:
la $s0, strNS
addi $sp, $sp, -12 #adjust the stack pointer for saving
sw $s0, 8($sp) #store addr of nospace string
sw $ra, 4($sp) #store return addr on the stack
sw $a0, 0($sp) #store the count on the stack
#Begin counting spaces
addi $t3, $a0, 0 #$t3 has addr of user input
addi $t5, $s0, 0 #$t5 has addr of string with no spaces
li $t6, 0 #$t6 holds index of string with no spaces
li $t0, 0 #$t0 will hold the count of spaces
li $t4, 0 #$t4 holds the index of the string
loop:
add $t1, $t3, $t4 #$t1 = addr of str[i]
lb $t2, 0($t1) #$t2 = character in str[i]
beq $t2, $zero, exitCS #break from loop if $t2 contains null character
addi $a0, $t2, 0 #place value to be checked in $a0
#save values onto stack from temp registers to preserve them
addi $sp, $sp, -28 #adjust the stack pointer for 5 values
sw $t6, 24($sp) #save index of string with no spaces
sw $t5, 20($sp) #save addr of string with no spaces
sw $t4, 16($sp) #save index of user input
sw $t3, 12($sp) #save the addr of user input
sb $t2, 8($sp) #save the character in str[i]
sw $t1, 4($sp) #save the address of str[i]
sw $t0, 0($sp) #save the count of spaces
jal isSpace #result from this jump and link will be in $v0 after call
#pop saved values from the stack, then reset the pointer
lw $t6, 24($sp)
lw $t5, 20($sp)
lw $t4, 16($sp)
lw $t3, 12($sp)
lb $t2, 8($sp)
lw $t1, 4($sp)
lw $t0, 0($sp)
addi $sp, $sp, 28 #reset stack pointer
beq $v0, $zero, addTo #if not a space, continue to next character
addi $t0, $t0, 1 #if it is a space, increment count
addTo:
bne $v0, $zero, nextChar #If character is a space, branch
sll $t7, $t6, 2 #index if nospaces string stores width of 4
add $t7, $t7, $t5 #now $t7 points at nospaces[i]
sb $t2, 0($t7) #store the character in the nospaces string
addi $t6, $t6, 1 #increment the index of nospaces
nextChar:
addi $t4, $t4, 1 #increment the index value
j loop #jump back to loop and continue processing
exitCS:
addi $v0, $t0, 0 #count of spaces placed into $v0
addi $v1, $t5, 0
lw $ra, 4($sp) #load return addr from the stack
lw $a0, 0($sp) #load value to check from the stack
addi $sp, $sp, 8 #reset stack pointer
jr $ra #return
isSpace:
addi $sp, $sp, -12 #adjust stack pointer to make room
sw $s0, 8($sp)
sw $ra, 4($sp) #store value of return addr onto stack
sw $a0, 0($sp) #store value to check onto stack
#Check to see if the character is a space
li $t0, 32 #ascii value for space character loaded into $t0
li $v0, 0 #Set default return to 0, or "not a space character"
bne $t0, $a0, endSC #if ascii values match, character is a space
li $v0, 1 #$v0 = 1 means it is a space character
endSC:
lw $s0, 8($sp)
lw $ra, 4($sp) #restore return address
lw $a0, 0($sp) #restore addr of str
addi $sp, $sp, 12 #reset the stack pointer
end: jr $ra
Change sll $t7, $t6, 2 to move $t7, $t6. There's no need to align anything manually.
Here's the data segment of your program after a run with the input here's some spaces. Once you see the bug, the fix should be obvious.

Resources