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

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

Related

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 " "

My assembler code has this error: "invalid program counter value: 0x00000000 Go: execution terminated with errors"

.data
prompt2: .asciiz "please enter the elements one by one:"
prompt3: .asciiz "the array is ordered as follows: "
prompt4: .asciiz "-- Program is finished running --"
str1: .asciiz ","
newLine: .asciiz "\n"
.text
addi $sp,$sp,-24
sw $ra,0($sp)
sw $s4,4($sp)
sw $a2,8($sp)
sw $s3,12($sp)
sw $s0,16($sp)
sw $s2,20($sp)
main:
# read n from console
li $v0 5
syscall
move $a2 $v0 #tagghiir t0 be a2 (n)
# allocate dynamic memory
sll $a0 $v0 2 # sll performs $a0 = $v0 x 2^2
li $v0 9 #9 is the system code for service(sbrk) whoes work is
syscall #to allocate dynamic memory
move $a1 $v0 #t2 saved address of heap #taghiir t2 to a1
#saved for printing
li $v0, 4 # $system call code for print_str
la $a0, prompt2 # $address of string to print
syscall
move $t1 $zero
move $t4,$a1
inputLoop:
bge $t1 $a2 exit1
# read in and store int
li $v0 5
syscall
sw $v0 0($t4)
addi $t1 $t1 1
addi $t4 $t4 4
li $v0, 4 # $system call code for print_str
la $a0, newLine # $address of string to print
syscall
j inputLoop
exit1:
jal sort
li $v0, 4 # $system call code for print_str
la $a0, prompt3 # $address of string to print
syscall
move $t3,$zero
move $t4 ,$a1
outputLoop:
bge $t3 $a2 exit2
#inaro azoon yki copy krdm
lw $t6, 0($t4)
li $v0, 1
move $a0, $t6
syscall
# read in and store int
addi $t3 $t3 1
addi $t4 $t4 4
blt $t4,$a1 ,comma
# bne $t5,$zero,comma
comma:
li $v0, 4 # $system call code for print_str
la $a0, str1 # $address of string to print
syscall
j outputLoop
exit2:
li $v0, 10
syscall #for finishing the program
swap:
move $a3,$a3
move $a1,$s4
# move $t5,$zero # its for our temp variable in c code
move $t4 ,$a1 #t4 is now the base address of heap
sll $t6 , $a3,2 # s1 is the given k
add $t6,$t4,$t6 # heap address + k*4
lw $t3,0($t6) #khoone k e heap
lw $t5,4($t6) #khoone k+1 heap
sw $t5,0($t6)
sw $t3,4($t6)
jr $ra
sort:
# move $s0,$zero #i=t0
move $s4,$a1 #base address of heap
addi $t5,$a2,-1 # meghdare i
move $s2,$zero #its the flag
while:
addi $s2,$s2,1
move $a3,$zero #j #taghiir s1 be a3 (haman j va k
forLoop:
bge $a3,$t5,outOfLoop
sll $t6,$a3,2 #j
add $t1,$t6,$a1 #v+j
lw $t7,0($t1) #t7=heap[j]
lw $t8,4($t1) #t8=heap[j+1]
blt $t7,$t8,outOfIf
move $s4,$a1
move $s3,$a3
jal swap
addi $s2,$s2,-1
outOfIf:
addi $a3,$a3,1 #j++
outOfLoop:
addi $t5,$t5-1 #i--
bgt $s2,$zero,exit_outter #if a>=b break
ble $t5,$zero,exit_outter #if b=< c break
j while
exit_outter:
lw $ra,0($sp)
lw $s4,4($sp)
lw $a2,8($sp)
lw $s3,12($sp)
lw $s0,16($sp)
lw $s2,20($sp)
addi $sp,$sp,24
jr $ra
Where am I going wrong?

MIPS: Printing Out a Histogram

I'm writing a MIPS program (assembly language) that takes in 10 integers and prints out a histogram represented by asterisks.
E.g.:
User input of 1, 2, 3, 4
Output:
*
**
***
****
I have most of this code written already in MIPS. The problem I am running into is printing out the correct length of asterisks. As of now it is simply printing out the a histogram all of the same length; the FIRST user inputed integer.
# program functionality:
.data
menu: .asciiz "\n1. New Histogram\n2. Print Histogram\n3. Quit\n"
prompt: .asciiz "\nEnter 10 numbers between 0 and 50 (inclusive):\n"
prompt1: .asciiz "\nEnter a valid number:\n"
asterisk: .asciiz "*"
space: .asciiz "\n"
array: .word 0:10
.text
main:
do:
jal print_menu
li $v0, 5
syscall
beq $v0, 1, new
beq $v0, 2, print
beq $v0, 3, quit
j do # end do
new:
jal new_user
j do
print:
jal print_user
j do
j quit
print_menu:
la $a0, menu
li $v0, 4
syscall
jr $ra
new_user:
la $a0, prompt
li $v0, 4
syscall
enter_loop:
la $t0, array
li $t1, 10
enter_loop_2:
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($t0)
addi $t1, $t1, -1
beqz $t1, end_loop_2
addi $t0, $t0, 4
j enter_loop_2
end_loop_2:
jr $ra
print_user:
la $t0, array
li $t1, 10
pLoop:
la $a0, space
li $v0, 4
syscall
asterisk_fun:
li $v0, 1
lw $a0, ($t0)
syscall
counter:
la $a0, asterisk
li $v0, 4
syscall
addi $a0, $a0, -1
beqz $a0, asterisk_end
j counter
asterisk_end:
jr $ra
addi $t1, $t1, -1
beqz $t1, endpLoop
addi $t0, $t0, 4
j pLoop
endpLoop:
jr $ra
quit:
li $v0, 10
syscall
The problems is that you are overwriting register $a0 in counter with the address of the asterisk, and you also used $a0 to count the number of items in that bucket.
Easy solution is to use other register (e.g. $a1) to count the number of items:
That would be:
#... your code
asterisk_fun:
li $v0, 1
lw $a1, ($t0) # Load number in $a1
move $a0, $a1 # move to $a0 just to print it
syscall
la $a0, asterisk
counter:
li $v0, 4
syscall
addi $a1, $a1, -1 # we use $a1 to keep the counter
beqz $a1, asterisk_end
j counter
asterisk_end:
# ... more of your code

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

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