Why is my program "Dropping off bottom"? - mips

I'm writing a MIPS program that is supposed to ask a user for two numbers, then add, subtract, multiply, and divide those two numbers. I am required to use functions. Whenever I execute my program my add/subtract functions work but for some reason when the program gets to my multiplication function it wont print the answer and MARS gives me the message "dropped off bottom".
.text
main:
la $a0,prompt1
li $v0,4
syscall #Asks for first integer
li $v0,5
syscall #stores first int in $v0
add $s0,$v0, $zero
la $a0,prompt2
li $v0,4
syscall #puts first int into $t0, asks for second int.
li $v0,5
syscall #stores second int in $v0
move $s1,$v0 #moves second int into $t1
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal add #jumps to add method
move $t2, $v0 #moves the result of add method into $t2
la $a0,ans1
li $v0,4
syscall # print string before result
move $a0,$t2
li $v0,1
syscall # print result of sum
la $a0,endl
li $v0,4
syscall #prints blank line
#--------------------------------------------SUB
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal sub #jumps to sub method
move $t0, $v0 #Moves answer from sub method to $t0
la $a0,ans2
li $v0,4
syscall # print string before result
move $a0, $t0 #moves answer from sub method $t0 into $a0 to be printed.
li $v0, 1
syscall#prints result of difference
la $a0,endl
li $v0,4
syscall #prints blank line
#------------------------------------------mult
la $a0,ans3
li $v0,4
syscall # print string before result
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal mult
move $t0, $v0 #moves answer into $a0
move $a0, $t0 #moves answer from sub method $t0 into $a0 to be printed.
li $v0, 1
syscall#prints result of difference
la $a0,endl
li $v0,4
syscall #prints blank line
#------------------------------------------
li $v0,10
syscall #ENDS PROGRAM
add:
move $t0, $a0
move $t1, $a1
add $v0, $t0, $t1
jr $ra #jumps back
sub:
move $t0, $a0
move $t1, $a1
sub $v0, $t0, $t1
jr $ra # jumps back
mult:
move $t0, $a0
move $t1, $a1
mult $t0, $t1 #multiplies $t0,$t1
mfhi $a0 #overflow
mflo $v0 #answer
.text
main:
la $a0,prompt1
li $v0,4
syscall #Asks for first integer
li $v0,5
syscall #stores first int in $v0
add $s0,$v0, $zero
la $a0,prompt2
li $v0,4
syscall #puts first int into $t0, asks for second int.
li $v0,5
syscall #stores second int in $v0
move $s1,$v0 #moves second int into $t1
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal add #jumps to add method
move $t2, $v0 #moves the result of add method into $t2
la $a0,ans1
li $v0,4
syscall # print string before result
move $a0,$t2
li $v0,1
syscall # print result of sum
la $a0,endl
li $v0,4
syscall #prints blank line
#--------------------------------------------SUB
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal sub #jumps to sub method
move $t0, $v0 #Moves answer from sub method to $t0
la $a0,ans2
li $v0,4
syscall # print string before result
move $a0, $t0 #moves answer from sub method $t0 into $a0 to be printed.
li $v0, 1
syscall#prints result of difference
la $a0,endl
li $v0,4
syscall #prints blank line
#------------------------------------------mult
la $a0,ans3
li $v0,4
syscall # print string before result
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal mult
move $t0, $v0 #moves answer into $a0
move $a0, $t0 #moves answer from sub method $t0 into $a0 to be printed.
li $v0, 1
syscall#prints result of difference
la $a0,endl
li $v0,4
syscall #prints blank line
#------------------------------------------
li $v0,10
syscall #ENDS PROGRAM
add:
move $t0, $a0
move $t1, $a1
add $v0, $t0, $t1
jr $ra #jumps back
sub:
move $t0, $a0
move $t1, $a1
sub $v0, $t0, $t1
jr $ra # jumps back
mult:
move $t0, $a0
move $t1, $a1
mult $t0, $t1 #multiplies $t0,$t1
mfhi $a0 #overflow
mflo $v0 #answer
li $v0,10
syscall
.data
prompt1:.asciiz "Enter the first integer: "
prompt2:.asciiz "Enter the second integer: "
ans1: .asciiz "The sum is "
ans2: .asciiz "The difference is "
ans3: .asciiz "The product is "
ans4: .asciiz "The quotient is "
endl:.asciiz "\n"
.data
prompt1:.asciiz "Enter the first integer: "
prompt2:.asciiz "Enter the second integer: "
ans1: .asciiz "The sum is "
ans2: .asciiz "The difference is "
ans3: .asciiz "The product is "
ans4: .asciiz "The quotient is "
endl:.asciiz "\n"

I added
li $v0, 10 #10 is the system call to exit
sys call
and this fixed the "drop off bottom" error for me

Related

MIPS32, Using the Stack for recursive multiplication, there is no output

So my test case is 7 for the multiplicand and 9 for the multiplier, I am trying to use Recursive Multiplication by using repeated addition, recursively. I'm a beginner in stacks in regards to MIPS so what could be the reason why nothing is wanting to output?
I am required to use $a0 -> Multiplicand, $a1 -> Multiplier, and $v0 -> Product
.text
main:
#Printing out prompt1
li $v0, 4
la $a0, prompt1
syscall
#Getting user input -> Multiplicand
li $v0, 5
syscall
move $a0, $v0
#Printing out prompt2
li $v0, 4
la $a0, prompt2
syscall
#Getting user input -> Multiplier
li $v0, 5
syscall
move $a1, $v0
#Loop
mult:
addiu $sp,$sp, -8
sw $a0, 4($sp)
sw $ra, 0($sp)
bne $a0, 1, else
add $v0, $v0, 0
j exit
else:
sub $a1, $a1, 1
jal mult
exit:
lw $a0, 4($sp)
add $v0, $v0, $a0
sw $v0, 4($sp)
addiu $sp, $sp, 8
jr $ra
lw $v0, multiplicand
li $v0, 1
la $a0, multiplicand
syscall
#Ending Program
li $v0, 10
syscall
.data
prompt1: .asciiz "Enter the multiplicand: "
prompt2: .asciiz "Enter the multiplier: "
multiplicand: .space 101

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: how can I apply the integer which users input into arithmetic function?

This is my first time coding PCSPIM. I find that there is a little trouble with my code.
.data
user_input: .asciiz "\n\nEnter an Integer for the value of n: "
result_display: .asciiz "\nThe sum from 0 to n is "
Greeting: .asciiz "\n\nThank you!"
.text
main:
#user input
li $v0, 4
la $a0, user_input
syscall
#allow user input
li $v0, 5
syscall
#store the input value into t8
move $t8, $v0
#calculation
addi $s0, $zero, $t8
I wish to use the integer value ($t8) that users input into the #calculation section, but it ends up with error.
addi $t0, $zero, 0
loop1:
add $t0, $t0, $s0
addi $s0, $s0, -1
bne $s0, $zero, loop1
nop
nop
# Display the result
li $v0, 4
la $a0, result_display
syscall
# Print out the result
li $v0, 1
move $a0, $t0
syscall
# Greets the user
li $v0, 4
la $a0, Greeting
syscall
# Exit the program
li $v0, 10
syscall
Sorry for my broken English.
The error is in the way you are using the "addi" instruction. The instruction requires an immediate (number) value to be passed as the third operand and not an architectural register. If you update the "addi" instruction to "addu" the code should work.

MIPS multiplication using addition

sorry, I try to multiply two integers but it doesn't work. I can't find where the problem is.Maybe because of register' names but I don't know how to correct it. I correct many times but it is not successful. Could anyone give me some points?
.data
prompt1: .asciiz "Please enter the first signed (decimal) integer: "
prompt2: .asciiz "Please enter the second signed (decimal) integer: "
result_msg: .asciiz "The result of these two 16-bit integers\' multiplication is: "
.text
.globl main
main:
li $v0, 4 #print prompt
la $a0, prompt1
syscall
li $v0, 5 #read multiplicand
syscall
move $s0, $v0
li $v0, 4 #print prompt
la $a0, prompt2
syscall
li $v0, 5 #read multiplier
syscall
move $s1, $v0
Mult: ori $t0,$zero,1 #mask
move $s3, $0 #initialize the result register
move $t1, $0
loop: beq $s1, $zero, end #if the multiplier is 0 then finished
and $t1, $t0, $s1 #mask
beq $t1, 1, mult_add
beq $t1, 0, shift
mult_add: addu $s3, $s3, $s0 #add to get product
shift:
sll $s0, $s0, 1 #shift multiplicand left
srl $s1, $s1, 1 #shift multiplier right
j loop
end:
jr $ra
result: #input the print_string
li $v0, 4
la $a0, result_msg
syscall
exit:
li $v0, 1 #input result
move $a0, $s3
syscall
li $v0, 10 #exit
syscall
Inspecting your code I see that you jump to label end when you are done multiplying.
The instruction at that label issues a jr $ra which "returns from a function", but I guess you just want to print the result and exit.
Therefore I'd suggest you remove that instruction so as to print the result and exit and maybe remove label result as it is not used anywhere in your code.

find substring and indices in mips

im trying find out substring and first occurrence indices. but something wrong. im comparing each element of pattern array and each element of string array until pointer reach to '\0'. whats the problem. algorithm is totaly wrong ?
#Note: $v0 is a symbolic name used by the assember for $2.
# $a0 is a symbolic name used by the assember for $4.
.data
prompt_str: .asciiz "Please type a text string: "
prompt_ptr: .asciiz "Please type a pattern string: "
print_yes: .asciiz "Yes, there is a match."
print_no: .asciiz "No, there is no match."
text_str: .asciiz "Text string : "
pattern_str: .asciiz "Pattern string : "
print_out: .asciiz "Output to be produced :"
print_dash: .asciiz "----------------------"
print_index: .asciiz "Starting index :"
print_msg : .asciiz "Length of longest partial match = "
nl: .asciiz "\n"
print_outer: .asciiz "please enter string"
str : .space 81
ptr : .space 81
tmp : .space 81
.text
main: la $a0, prompt_str
li $v0, 4 #print_string command.
syscall
la $a0,str #read string
li $a1,81
li $v0,8
syscall
la $t0,str #move string to $t0
la $a0,prompt_ptr
li $v0,4 #print pattern command
syscall
la $a0,ptr #read pattern
li $a1,81
li $v0,8
syscall
la $t1,ptr #move pattern to $t1
lb $t2,0($t0) #pointer first element array of string
move $t4,$t2 #address pointer of $t2
lb $t3,0($t1) #pointer first element array of pattern
outer_loop : beq $t2,$0,end_outer_loop
j inner_loop
inner_loop : beq $t2,$0,end_inner_loop
beq $t3,$0,end_inner_loop
beq $t2,$t3,end_inner_loop
addiu $t2,$t2,1
addiu $t3,$t3,1
j inner_loop
end_inner_loop :bne $t3,$0,inc_ptr
j print_match
inc_ptr : add $t2,$t4,1
j outer_loop
end_outer_loop :la $a0,print_outer
li $v0,4
syscall
print_match : la $a0,text_str #print string
li $v0,4
syscall
move $a0,$t0
li $v0,4
syscall
la $a0,nl #print newline character
li $v0,4
syscall
la $a0,pattern_str #print pattern string
li $v0,4
syscall
move $a0,$t1
li $v0,4
syscall
la $a0,nl #print newline character
li $v0,4
syscall
la $a0,print_out #print output line and newline character
li $v0,4
syscall
la $a0,nl
li $v0,4
syscall
la $a0,print_dash
li $v0,4
syscall
la $a0,print_yes
li $v0,4
syscall
la $a0,print_index #print starting index
li $v0,4
syscall
li $v0,10
syscall
end_loop : li $v0,10
syscall
I used your code for a similar project, in inner_loop you don't have a proper bne
I just put one bne and now it print just the string whit the substring..
.text
.globl main
main:
li $v0, 4
la $a0, msg1
syscall
li $v0, 8
la $a0, strMain
li $a1, 99
syscall
li $v0, 4
la $a0, msg2
syscall
li $v0, 8
la $a0, strSub
li $a1, 99
syscall
la $a0,strMain
jal findLengthString
move $a2, $v0
la $a0, strSub
jal findLengthString
move $a3, $v0 # M
sub $a2, $a2, $a3 # N-M
la $a0, strMain
la $a1, strSub
jal subStringMatch
move $t1, $v0
li $v0, 1
move $a0, $t1
syscall
exit:
li $v0, 10
syscall
lb $t9, endline
findLengthString:
li $t0, -1
move $s0, $a0
loop_fls:
lb $t1, 0($s0)
beq $t1, $t9, foundLength
addi $t0, $t0, 1
addi $s0, $s0, 1
j loop_fls
foundLength:
move $v0, $t0
jr $ra
subStringMatch:
li $t0, 0 #i
loop1:
bgt $t0,$a2, loop1done
li $t1, 0 #j
loop2:
bge $t1, $a3, loop2done
add $t3, $t0, $t1
add $t4, $a0, $t3
lb $t3, 0($t4) # main[i+j]
add $t4, $a1, $t1
lb $t4, 0($t4) # sub[j]
# if a0[i + j] != a1[j]
bne $t3, $t4, break1
addi $t1, $t1, 1
j loop2
loop2done:
beq $t1, $a3, yesReturn
j break1
yesReturn:
move $v0, $t0
jr $ra
break1:
addi $t0, $t0, 1
j loop1
loop1done:
li $v0, -1
jr $ra
.data
msg1: .asciiz "Enter Main String: "
msg2: .asciiz "Enter String to Check SubString: "
strMain: .space 100
strSub: .space 100
endline: .asciiz "\n"