Runtime exception at.... :address out of range - mips

I am converting a C code into MIPS that finds the location(s) of a sub-string in a string and i can't figure out what is causing the address out of range error at line "strloop:
lb $t2, ($t1)" (line 76). How can i fix the error??
My code is:
.data
Text : .space 28
Key : .space 5
msg1 : .asciiz "Please enter the text:"
msg2 : .asciiz "\nPlease enter the key:"
msg3 : .asciiz "\n NOT FOUND"
msg4 : .asciiz "The key found at:"
.text
main:
la $a0, msg1
li $v0,4
syscall
la $a0, Text
li $a1, 28
li $v0,8
syscall
la $t0, Text
la $a0, msg2
li, $v0,4
syscall
la $a0, Key
li $a1,5
li $v0,8
syscall
la $t1, Key
add $a0, $t1 ,$zero
jal mstrlen
add $t2, $v0, $t2
add $a0, $t0, $zero
add $a1, $t1, $zero
add $a2, $t2, $zero
jal findPos
add $t0, $t0, $v0
add $a0, $v0, $zero
beq $a0, $zero,K
li $v0, 4
la $a0, msg4
syscall
li $t0, 1
move $a0, $t0
syscall
K: la $a1, msg3
li $v0,4
syscall
li $v0, 10
syscall
mstrlen:
addi $sp, $sp, -8
sw $t0,4($sp)
sw $t1,($sp)
li $t0,0 #$t0=0
add $t1,$a0,$zero
strloop:
lb $t2, ($t1)
beq $t2,28,endloop #$t2\n
addi $t0,$t0,1 #$t0++
addiu $t1,$t1,1
b strloop #next i
endloop:
add $v0, $t0, $0
lw $t0, 4($sp)
lw $t1,($sp)
addi $sp,$sp,20 #stack pointer restored
jr $ra
######################## MAIN FUNCTION
# $a0 text, $a1 key, $a2 keyInt (strleng(key)
findPos:
addi $sp, $sp, -20
sw $t0, 16($sp)
sw $t1, 12($sp)
sw $t2, 8($sp)
sw $t3, 4($sp)
sw $t4, ($sp)
loop:
add $t2,$a2, $zero
lb $t1,($a1)
lb $t0,($a0)
beq $t0,28,endfP
beq $t0, $t1, L
add $t1, $zero, $zero
beq $t1,$t2,M
addi $t0,$t0,1
b loop
L: addi $t1,$t1,1
M: add $t3, $t3,$t0
addi $t3, $t3,1
sub $t3,$t3,$t2
endfP: add $v0, $t3, $0
lw $t0,16($sp)
lw $t1,12($sp)
lw $t2,8($sp)
lw $t3,4($sp)
lw $t4,($sp)
addi $sp,$sp,20
jr $ra

Related

Infinite [Bad instruction address] occurred and ignored error occurs. What can be changed to fix this problem?

I am working on a MIPS Fibonacci sequence. However, if I run the program, I input a number and all I get is infinite number of the error: [Bad instruction address] occurred and ignored infinite times. What should I do? And I am only allowed to change the code between the ##########s.
Below is my MIPS code.
.data
newline:
.asciiz "\n"
str0:
.asciiz "Enter a positive integer: "
str1:
.asciiz "ERROR: received a negative integer!\n"
str2:
.asciiz "INFO: fibonacci returned "
.text
# $a0: n (<1024)
################################################################################FIXME
fibonacci:
li $t2, 1
addiu $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
addiu $s0, $a0, 0
beq $zero, $s0, ResultOne
beq $t2, $s0, ResultOne
bltz $s0, Negative
addiu $a0, $s0, -1
jal fibonacci
addiu $s1, $v1, 0
addiu $a0, $s0, -2
jal fibonacci
addu $v1, $v1, $s1
j finish
ResultOne:
li $v0, 1
li $v1, 1
j Finish
Negative:
li $v0, 0
j Finish
Finish:
lw $s1, 8($sp)
lw $s0, 4($sp)
lw $ra, 0($ra)
addiu $sp, $sp,12
jr $ra
# FIXME
################################################################################
.globl main
main:
# print_string str0
li $v0, 4
la $a0, str0
syscall
# $t0 = read_int
li $v0, 5
syscall
move $t0, $v0
# $s0 = $ra; fibonacci($t0); $ra = $s0
move $s0, $ra
move $a0, $t0
jal fibonacci
move $ra, $s0
# $t0 = $v0; $t1 = $v1
move $t0, $v0
move $t1, $v1
# if ($t0 == 0) { goto main_failure }
beq $t0, $zero, main_failure
main_success:
# print_string str2
li $v0, 4
la $a0, str2
syscall
# print_int $t1
li $v0, 1
move $a0, $t1
syscall
# print_string newline
li $v0, 4
la $a0, newline
syscall
# goto main_return
b main_return
main_failure:
# print_string str1
li $v0, 4
la $a0, str1
syscall
main_return:
# return
jr $ra

Why is my array comparison not printing in MIPS?

Here is my code that calculates the sum of two arrays and then prints them in a message to the the console. I would now like to compare my two array sums and return the correct message prompt depending on which array is larger. I feel like I'm very close because I tried to follow the logic from the first printed statements but how can I compare register $s7 & $s8 and then declare one the larger array through my message prompt I have set up? I'm using this emulator: https://cpulator.01xz.net/?sys=mipsr5-spim
.data
x: .word 1,2,3,4,5,6,7,8,9,10
iterator: .word 0
size: .word 9
prompt: .asciiz "The total sum of the array is equal to: "
.text
main:
la $s0, prompt
la $t0, x
lw $t1, iterator
lw $t2, size
begin_loop:
bgt $t1, $t2, exit_loop
sll $t3, $t1, 2
addu $t3, $t3, $t0
lw $t6, 0($t3)
addu $s7, $s7, $t6
addi $t1, $t1, 1
j begin_loop
exit_loop:
li $v0, 4
la $a0, prompt
syscall
li $v0, 1
la $a0, ($s7)
syscall
.data
x2: .word 5,6,7,8,9,10,11,12,13,14
iterator2: .word 0
size2: .word 9
prompt2: .asciiz "The total sum of array 2 is equal to: "
prompt3: .asciiz "The larger array is array 1"
prompt4: .asciiz "The larger array is array 2"
.text
main2:
la $s0, prompt2
la $t0, x2
lw $t1, iterator2
lw $t2, size2
begin_loop2:
bgt $t1, $t2, exit_loop2
sll $t3, $t1, 2
addu $t3, $t3, $t0
lw $t6, 0($t3)
addu $s8, $s8, $t6
addi $t1, $t1, 1
j begin_loop2
exit_loop2:
li $v0, 4
la $a0, prompt2
syscall
li $v0, 1
la $a0, ($s8)
syscall
slt $t1,$s7,$s8 # checks if $s0 > $s1
beq $t1,1,prompt3 # if $s7 > $s8, goes to prompt3
beq $t1,$zero,prompt4 # if $s8 < $s7, goes to prompt4
li $v0, 4
la $a0, prompt3
syscall
li $v0, 4
la $a0, prompt4
syscall

my mips code does not print out correct answer

so I tried this code, took some parts of it from this forum. my answer is not coming okay though. I'm getting weird numbers for min and max. can anyone tell me what did I do wrong?
.text
.globl __start
__start: # execution starts here
la $t0,array # $t0 will point to the elements
lw $t2,($t0) # initialize min = a[0]
lw $t3,($t0) # initialize max = a[0]
addi $t4, $s0, 0
li $t1, 0
loop:
bge $t4, 8, EndLoop
lw $t2, array($t1)
bgt $s2, $s0, SetMax
blt $s2, $s1, SetMin
cont:
addi $s1, $s1, 4
addi $t4, $t4, 1
j loop
SetMax:
move $t3, $s2
j cont
SetMin:
move $t2, $s2
j cont
EndLoop:
li $v0 4
la $a0 ans2
syscall
li $v0 1
la $a0 ($s0)
syscall
li $v0 4
la $a0 Space
syscall
li $v0 4
la $a0 ans1
syscall
li $v0 1
la $a0 ($s1)
syscall
la $a0,endl # syscal to print out
li $v0,4 # a new line
syscall
li $v0,10 # Exit
syscall # Bye!
.data
array: .word 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13
ans1: .asciiz "min = "
ans2: .asciiz "\nmax = "
endl: .asciiz "\n"
Space: .asciiz " "

MIPS bubble sort algorithm not printing

I am currently beginning to study the MIPS language and I am trying to write a bubble sort code in MIPS, but I guess I have made a mistake somewhere and i can't seem to find it. I believe I am storing elements properly but I don't know why it is not working. Here is a picture of my code:
.data
array: .space 100
i: .word 0
j: .word 0
temp: .word
.text
la $s2, i
la $s3, j
la $s4, temp
li $v0,5
syscall
move $s0, $v0 #array size
li $v0, 8
la $a0, array
li $a1, 100
move $s1, $a0 #actual array(with elements)
syscall
move $a0, $s0 #size becomes argument
move $a1, $s1 #array becomes argument
jal BubbleSort
li $v0, 4
move $a0, $v0
syscall
li $v0, 10
syscall
BubbleSort:
move $s0, $a0
move $s1, $a1
j ExternalLoop
ExternalLoop:
slt $t0,$s2, $s0 #for(i=0;I<N;i++)
beq $t0, $zero, ExitLoop
j InternalLoop
addi $s2, $s2,1 #i++
li $s3, 0 #reset j after every iteration
InternalLoop:
slt $t1, $s3, $s0 #for(j=0;J<N;j++)
beq $t1, $zero, ExitLoop
#array[j]=s1+4*j :
#sll $t2, $s3, 2
#add $s1, $s1, $t2
lb $t3, 0($s1) #array[j]
lb $t4, 4($s1) #array[j+1]
#swapping:
move $s4, $t3
move $t3, $t4
move $t4, $s4
sb $t5, 0($t3)
addi $s3, $s3, 1 #j++
j InternalLoop
move $v0, $t5
ExitLoop:
jr $ra
Maybe my mistake is somewhere at the end where I am storing the return values but I don't know. Can anyone help?

Add some control instructions for a mips program

I wrote a very simple program for mips that counts how many times a default word (in this case the word is "esame") appears in a default phrase that is "Esame: Programma di riconoscimento parole, realizzato per l'esame orale di calcolatori elettronici".
Here is the code:
.data # 0x10000000 (indirizzo di partenza)
frase: .asciiz "Esame: Programma di riconoscimento parole, realizzato per l'esame orale di calcolatori elettronici"
param: .asciiz "esame"
out1: .asciiz "Programma per il riconoscimento di una parola in una frase \n\n"
out2: .asciiz "La parola inserita come parametro appare "
out3: .asciiz " volta/e \n"
.text # 0x00400000 (indirizzo di partenza)
la $s0, frase
la $s1, param
li $t0, 0x20 # backspace
li $t8, 0x27 # apostrophe
add $s5, $zero, $zero
li $v0, 4
syscall
la $a0, out1
syscall
car1:
lbu $t1, 0($s0)
lbu $t2, 0($s1)
ori $t1, $t1, 0x20
ori $t2, $t2, 0x20
beq $t1, $t2, ok
bne $t1, $t2, dive
ok:
addi $s0, $s0, 1
addi $s1, $s1, 1
lbu $t1, 0($s0)
lbu $t2, 0($s1)
beq $t1, $zero, stamp
ori $t1, $t1, 0x20
beq $t2, $zero, ctrl
ori $t2, $t2, 0x20
ctrl:
beq $t1, $t2, ok
beq $t1, $t0, risul
bgt $t1, 0x7a, risul
blt $t1, 0x61, risul
bne $t1, $t2, dive
#beq $t1, $zero, stamp
dive:
addi $s0, $s0, 1
lbu $t1, 0($s0)
beq $t1, $t0, nuova
beq $t1, $zero, stamp
beq $t1, $t8, nuova
j dive
nuova:
la $s1, param
addi $s0, $s0, 1
#beq $s0, $s1, stamp
j car1
risul:
addi $s5, $s5, 1
j nuova
stamp:
li $v0, 4
la $a0, out2
syscall
li $v0, 1
move $a0, $s5
syscall
li $v0, 4
la $a0, out3
syscall
j fine
fine:
j fine
In this case the word "esame" appears twice within the
sentence.
Now I would like to add a control like this: if the default word is formed by an apostrophe, as es'ame or esame', this word should not be counted. In this second case the word "esame" would appear only once in the sentence.
What instructions should I write to implement this control? Thank You.