MIPS Runtime exception and Address out of range - string

Hello I am trying to write this code for a project and at the end of the code I am getting the error "line 84: Runtime exception at 0x004000dc: address out of range 0x0000006e" I am new to MIPS and can not figure out how to resolve this. The purpose of the code is for a user to enter a string and manipulate as they desire and to print the new string at the end. Any help??
.data
Prompt1: .asciiz "Your current string is: \n"
Prompt2: .asciiz "\nDo you want to make any changes to the string? (Y/N) \n"
Prompt3: .asciiz "\nEnter the character in the string would you like replaced: \n"
Prompt4: .asciiz "\nEnter what you would like to change the character to: \n"
Prompt5: .asciiz "\nYour final string is: "
Word: .space 40
yes: .asciiz "y"
.text
#######################
#Print Instructions
li $v0, 4
la $a0, Prompt1
syscall
#getting text from the user
li $v0, 8 #print string
la $a0, Word
li $a1, 40
syscall
#########################
Loop:
#yes or no
li $v0, 4
la $a0, Prompt2
syscall
li $v0, 8 #get input
la $a0, Word
li $a1, 2
move $t0, $a0 #save string to $t0
syscall
lb $t1, yes
lb $t0, 0($t0)
bne $t0, $t1, endloop
#replace char
li $v0,4
la $a0, Prompt3 #replace char message
syscall
li $v0, 12 #read in char
syscall
addi $s0, $v0, 0 #store char in $s0
li $v0, 4
la $a0, Prompt4 #new char message
syscall
li $v0, 12
syscall
addi $s1, $v0, 0 #store new char in $s1
la $t0, Word
li $t1, -1
j Loop
replaceLoop:
lbu $t2, 0($t0) #load our input's first char is at
addi $t0, $t0, 1 #increment address of our string.
addi $t1, $t1, 1
beq $t2, $s0, replace #check if char in input, matches
beq $t2, $0, endloop #char we want to replace.
replace:
sb $s1, -1($t0)
b replaceLoop
endloop:
li $v0, 4
la $a0, Prompt5
syscall
li $v0, 4
la $a0, ($t0)
la $a1, 40
syscall
#exit program
li $v0,10
syscall

Related

What's wrong in my MIPS code about merge all separate input into a string?

.data
string: .space 256
temp: .space 4
.text
la $t0, string
input:
li $v0, 8
la $a0, temp
li $a1, 4
syscall
beq $a0, '=', print
lb $s0, 0($a0)
sw $s0, ($t0)
addi $t0, $t0, 4
bne $s0, '=', input
print:
li $v0,4
la $a0, string
syscall
end:
li $v0, 10
syscall
I can only store the first character of my input, but I want to store every single input.
input example:
6
+
2
*
3
=
what I want to store in string:
6+2*3=
what I actually store in string:
6

Why is the mips palindrome function not working according to specifications?

I'm trying to finish writing a mips palindrome function that "reads the same forward as backwards" and my function is not working as needed. This is hurting my brain and any help is greatly appreciated. Thank you!
A recursive definition of a palindrome is as follows:
Let the string S be represented as S = S1 S2 … SN, where N is the length of the string.
S is a palindrome
if N = 0 or N = 1 or
if N 2 and S1 = SN and the substring (S2 .. SN-1) is a palindrome.
Function Palin($a0, $a1)
Precondition:
$a0 holds the address of the first byte in the string (or substring) being tested while $a1 holds the length of the string (or substring)
Postcondition:
1 (true) is returned in the register $v0 if the given string (or substring) is a palindrome; otherwise, 0 (false) is returned in $v0.
.data
prompt1: .asciiz "Enter length of string to be read: "
prompt2: .asciiz "Enter the string "
ItIs: .asciiz "\nThe string IS a palindrome!"
IsNot: .asciiz "\nThe string is NOT a palindrome!"
string: .asciiz ""
.text
.globl main
main:
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
move $a1, $v0
la $a0, prompt2
li $v0, 4
syscall
addi $a1, $a1, 1
la $a0, string
li $v0, 8
syscall
addi $a1, $a1, -1
jal Palin
bne $v0, $zero, label1
la $a0, IsNot
j label2
label1:
la $a0, ItIs
label2:
li $v0, 4
syscall
li $v0 10
syscall
nop
Palin:
# HERE IS THE FUNCTION
li $v0, 10
syscall
addu $ra, $zero, $s7 #restore $ra since the function calles
#another function
jr $ra
add $zero, $zero, $zero
add $zero, $zero, $zero
EndPalin:
The problem is this:
Palin:
# HERE IS THE FUNCTION
li $v0, 10 # These
syscall # lines
The value 10 is the trap to exit the program and since you loaded that into register $v0 and then did a syscall, the program exits. Remove those offending lines and you should be on your jolly way.
HTH

Removing extra spaces after i enter a string

Hi i was wondering if there was a simple way to remove the spaces i allocated for a string to be entered, or perhaps a way to not compare the empty space after the word. This program is a palindrome detector. Any suggestions would be great.
Ex. output:
racecar, not a palindrome because it has a few empty spaces.
aaaaaaaaaa is a palindrome because it has no empty spaces...
again just need a hotfix to remove the spaces after the word is inputted, or a way to ignore the spaces after the word. Thank You.
.data
intro : .asciiz "Lets See If Your Word is a Palinfrome!\n"
question: .asciiz "Please Enter up to a 10 Character word: "
Y_P: .asciiz "Yes it is!"
N_P: .asciiz "No Its Not!"
str1: .space 10
.text
.globl main
main:
li $v0, 4
la $a0, intro
syscall
li $v0, 4
la $a0, question
syscall
li $v0, 8
la $a0,str1
addi $a1,$zero,10
syscall
move $t0, $a0
palindrome:
addi $t0, $0, 0 # j = 0
length: add $t2, $a0, $t0 # $t2 = &array[j]
lb $t2, 0($t2) # $t2 = array[j]
beq $t2, $0, done # end of string?
addi $t0, $t0, 1 # j = j+1
j length
done: addi $t0, $t0, -1 # j = j-1
addi $t1, $0, 0 # i = 0
loop: slt $t2, $t1, $t0 # $t2 = 1 if i < j
beq $t2, $0, yes # if !(i < j) return
add $t2, $a0, $t1 # $t2 = &array[i]
lb $t2, 0($t2) # $t2 = array[i]
add $t3, $a0, $t0 # $t3 = &array[j]
lb $t3, 0($t3) # $t3 = array[j]
bne $t2, $t3, no # is palindrome?
addi $t0, $t0, -1 # j = j-1
addi $t1, $t1, 1 # i = i+1
j loop
yes: # yes a palindrome
addi $v0, $0, 1
li $v0, 4
la $a0, Y_P
syscall
li $v0, 10
syscall
j yes
jr $ra
no: # not a palindrome
addi $v0, $0, 0
li $v0, 4
la $a0, N_P
syscall
li $v0, 10
syscall
j no
jr $ra

Changing Characters in a String in MIPS (Caesar Shift)

Hey guys i want to write a simple Caesar Shift in MIPS. My program takes in the string to be encrypted/decrypted. Then the program asks for the key (the number of letters to shift) and then asks to encode or decode.
---EDIT ---
Sorry i forgot to clarify my question. The output of my program is "estuvwL" for a shift of 1 on the string "abcde". Obviously, this is not correct. Any thoughts on where i am going wrong?
Here is my code:
.data # Data declaration section
prompt: .asciiz "Enter a message: " #prompt to enter message
key_prompt: .asciiz "Enter a key for this message: " # enter number to shift letters
enc_dec: .asciiz "(e)ncrypt or (d)ecrypt? " # encrypt or decrypt prompt
encode_bytes: .byte 1 #bytes for encoding
key: .word 0 # the variable for encoding key
String: .space 255 # the string to be encrypted/decrypted
encode: .space 1 #the character (e) or (d) to ask encrypt or decrypt
e: .ascii "e"
d: .ascii "d"
.text
main: # Start of code section
__Start:
li $v0, 4 #print string
la $a0, prompt # print the first prompt
syscall
li $v0, 8 # read in string to be encrpyted
la $a0, String # stores string in variable
li $a1, 255 # the string allocates room for up to 255 chars
syscall
li $v0, 4 #print string
la $a0, key_prompt
syscall
li $v0, 5 #code to read in int
syscall # reading in the integer
move $t5, $a0 #moving key to $t5
li $v0, 4 #print string
la $a0, enc_dec #print the encode string
syscall
li $v0, 12 #read character code
#la $a0, encode #load address of encode to register $a0
#li $a1, 2 #limit input to 1 char
syscall
li $s7, 101
li $s6, 100
beq $v0, $s7, __encrypt_Loop
beq $v0, $s6, __decrypt_Loop
__encrypt_Loop:
la $t0, String #holding address of String
__encode_Loop:
lb $t2, ($t0) #loading the byte to be manipulated into $t2
beqz $t2, __print_String # if string is equal to zero, jump to print new string
__continue:
add $t2, $t2, $t5 #adding the key to the string (manipulating chars)
li $t3, 'z' #loading the value of z, for checking purposes
bgt $t3, $t2, __encrypt_Check #if the char is "bigger" than z jump to fix
sb $t2, ($t0) #store the value
addi $t0, 1 #incrementing to next char
j __encode_Loop
__encrypt_Check:
li $s4, 25
sub $t2, $t2, $s4
sb $t2, ($t0) #store char in string
addi $t0, 1 #incrementing to next char
j __continue
__decrypt_Loop:
la $t0, String #holding address of String
# la $t5, key #loading the key into $t5
__decode_Loop:
lb $t2, ($t0) #making $t2 the address of the string
beqz $t2, __print_String # if string is equal to zero, jump to print new string
___continue:
sub $t2, $t2, $t5 #subtracting the key to the string (manipulating chars)
li $t3, 'a' #loading the value of z, for checking purposes
blt $t3, $t2, __decode_Check #if the char is "smaller" than a jump to fix
sb $t2, ($t0) #store the value
addi $t0, 1 #incfementing to next char
j __decode_Loop
__decode_Check:
li $s4, 25
add $t2, $t2, $s4
sb $t2, ($t0) #store char in string
addi $t0, 1 #incrementing to next char
j __continue
__print_String:
li $v0, 4
#move $a0, $t0
la $a0, String
syscall
__end_program:
li $v0, 10
syscall
A few problems here:
First is the line move $t5, $a0 #moving key to $t5 which should read move $t5, $v0 as the syscall returns the key in $v0 not $a0.
Secondly, I'm not sure what the purpose of the "check" functions is, so I commented out the branch to them and everything worked as I expected.
At any rate, the check for them is clearly wrong. You wrote:
blt $t3, $t2, __decode_Check #if the char is "smaller" than a jump to fix
But the arguments are reversed from the comment. It should read
blt $t2, $t3, __decode_Check #if the char is "smaller" than a jump to fix
Likewise for the __encrypt_Check function.

Complications in MIPS code of Recursive Reversal of a string

I have written this MIPS code of recursive reversal of a string. However, the output is coming out to be the same that has been input by the user. Can someone please help me out and indicate where am I going wrong? Please reply as soon as possible.
# Program to reverse a string input by the user
.data
.align 2
array: .space 50
input: .asciiz "Enter a string: "
output: .asciiz "\nThe reversed string is: "
.text
.globl main
main:
addi $s0, $zero, 50
addi $t0, $zero, 0
la $a0, input
li $v0, 4
syscall
la $a0, array
li $v0, 8
syscall
initiate:
add $t0, $a0, $zero # initial address
add $t1, $zero, $zero # count=0
add $t2, $zero, $zero # i=0
la $t0, array # base address of the array
add $t3, $t0, $t2 # & array[i]
loop:
lb $t3, 0($t3) # fetch array[i]
beqz $t3, EndOfString # loop exits if it is a null character;array[i] !='\0'
bne $t3, $0, continue # otherwise loop continues
add $t1, $t1, 1 # count++
continue:
add $t2, $t2, 1 # i++
j loop
addi $a1, $zero, 50
jal StringReversal
EndOfString:
la $a0, output
li $v0, 4
syscall
la $a0, array
li $v0, 4
syscall
li $v0, 10
syscall
StringReversal:
add $t0, $a0, $zero # initial address
add $t4, $zero, $zero # j = start = 0
addi $t5, $a1, -1 # k = end-1
SwapLoop:
add $t6, $t0, $t4
lb $t7, 0($t6) # load byte array[start]
add $t8, $t0, $t5
lb $t9, 0($t8) # load byte array[end-1]
sb $t7, 0($t8) # array[end-1] = array[start]
sb $t9, 0($t6) # array[start] = array[end-1]
addi $t4, $t4, 1 # j++
addi $t5, $t5, -1 # k--
slt $t9, $t5, $t4
beqz $t9, SwapLoop
jr $ra
# Program to reverse a string input by the user
.data
.align 2
array: .space 50
input: .asciiz "Enter a string: "
output: .space 50
size: .word 49
.text
j main
length:
# return length of the input string
# $a0 - address of string
# $v0 - length of the string
move $v0, $zero # set $v0 to 0
length_loop:
lb $t0, 0($a0)
beqz $t0, length_end
addi $v0, $v0, 1
addi $a0, $a0, 1
b length_loop
length_end:
subi $v0, $v0, 1
jr $ra
reverse:
# $a0 - address of string to reverse
# a1 - length of the string
# a2 - address of string where to store the reverse
addi $sp, $sp, -4
sw $ra, 0($sp)
bltz $a1, reverse_end
lb $t0, 0($a0)
subi $a1, $a1, 1
subi $a0, $a0, 1
sb $t0, 0($a2)
addi $a2, $a2, 1
jal reverse
reverse_end:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
main:
addi $s0, $zero, 50
addi $t0, $zero, 0
la $a0, input
li $v0, 4
syscall
# read string from the user into $a0
la $a0, array
lw $a1, size
li $v0, 8
syscall
# reverse the string
jal length # $v0 contains length of string
la $a0, array
move $a1, $v0
add $a0, $a0, $a1 # pointer to the last character
la $a2, output
jal reverse
# print the reverse string
la $a0, output
li $v0, 4
syscall
Output:
Enter a string: hello
olleh
-- program is finished running (dropped off bottom) --
In your continue label, it looks as though j loop is called too early, and hence
addi $a1, $zero, 50
jal StringReversal
is never reached, hence the instructions in the StringReversal label are not performed.

Resources