MIPS Floating Point Division Output - output

I am new in mips and i got this assignment that asks me to take 2 inputs from user and divide them and get a floating point output.
The problem is the output comes out like this 0.000000000, not the predicted output
this is my code
.data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz " a/b = "
.text
main: li $v0, 4
la $a0, str1
syscall
li $v0, 6
syscall
add $s0, $v0, $zero
li $v0, 4
la $a0, str2
syscall
li $v0, 6
syscall
move $s1, $v0
div $s0, $s1
li $v0, 4
la $a0, str3
syscall
li $v0, 2
move $a0, $t0
syscall
li $v0, 10
syscall
what should i do?

This code works for me.
.data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz " a/b = "
.text
.globl main
main:
#prompt for "a"
li $v0, 4
la $a0, str1
syscall
#User input for "a"
li $v0, 6 #The float value that is read is will be in $f0 register
syscall
mov.s $f3, $f0 #moving the value of "a" into f3 to reuse f0
#prompt for "b"
li $v0, 4
la $a0, str2
syscall
#user input for "b"
li $v0, 6 #The float value that is read is will be in $f0 register
syscall
mov.s $f4, $f0
#Dividing "a" and "b"
div.s $f12 $f3 $f4
#prompt for "a/b"
li $v0, 4
la $a0, str3
syscall
#Displaying contents of f12
li $v0, 2
syscall
li $v0, 10
syscall

Related

How would i get this program to show show instead of three?

I created an array in MIPS and this language is very new to me. Im suppose to show the number at position one instead i get position 2 which is 3
#create a simple idea of an array
.data
num1: .word 1, 2, 3 #allows input for slot one in array
label1: .asciiz "Input the first number:"
label2: .asciiz "Input the second number:"
label3: .asciiz "Input the third number:"
label4: .asciiz "The number in slot 1 of the array is:"
.text
main:
#intiate the first label
li $v0, 4
la $a0, label1
syscall
#input first number into the array
li $v0, 5
syscall
sw $v0, num1
#initiate the second label
li $v0,4
la $a0, label2
syscall
#input second number into the array
li $v0, 5
syscall
sw $v0, num1
#initiate the third label
li $v0, 4
la $a0, label3
syscall
#input third number into the array
li $v0, 5
syscall
sw $v0, num1
#intiate the fourth label
li $v0, 4
la $a0, label4
syscall
#output position 1
li $v0, 1
lw $a0, num1
syscall
#end program
li $v0,10
syscall

How do I get my code for addition of two numbers to work in QtSpim?

I'm new to MIPS and I've written a program to add two user-selected numbers but it's not working. It says Instruction references undefined symbol at 0x00400014. Can someone please go through my code and explain what's wrong? Thank you.
.data
msg1: .asciiz "Enter addend 1: "
msg2: .asciiz "Enter addend 2: "
msg3: .asciiz "Sum = "
.text
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
move $t0, $v0
li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
move $t1, $v0
add $t2, $t0, $t1
li $v0, 4
la $a0, msg3
syscall
li $v0, 1
move $a0, $t2
syscall
li $v0, 10
syscall

Asking user to input two numbers, store them in memory, doing addition and substraction and display both result

I'm new to MIPS, and I'm working on this question, my code wold assemble, but after I hit the run buttom and entered the first number, it would show a runtime exception: Runtime exception at 0x00400018: address out of range 0x00000000
I don't understand what's happening to my code, can anyone help me?
.data
str1: .asciiz "Enter number 1: "
str2: .asciiz "Enter number 2: "
str3: .asciiz "The sum of the two number is: "
str4: .asciiz "The difference of the two number is: "
num1: .word 4
num2: .word 4
.text
.globl main
main:
la $t0, str1
li $v0, 4 #syscall code for print_str
la $a0, ($t0) #address of string to print
syscall #print str1
la $s0, num1
li $v0, 5 #syscall code for read int
syscall #read int
sw $s0, ($v0) #store the enterd value in num1
la $t1, str2
li $v0, 4 #syscall for print_str
la $a0, ($t1) #address of string to print
syscall #print str2
la $s1, num2
li $v0, 5 #syscall code for read int
syscall #read int
sw $s1, ($v0) #store the entered value in num2
add $s2, $s0, $s1 #add num1 and num2
la $t2, str3
li $v0, 4 #syscall for print_str
la $a0, ($t2) #address of str3
la $a1, ($s2) #address of the sum
syscall #print str3 and the sum
sub $s3, $s0, $s1 #substract num1 and num2 ERROR COULD BE HERE
la $t3, str4
li $v0, 4 #syscall for print_str
la $a0, ($t3) #address of str4
la $a1, ($s3) #address of the difference
syscall #print str4 and the difference
li $v0, 10 #exit
syscall
Error in C:\Users\Desktop\exercise2.s line 18: Runtime exception at 0x00400018: address out of range 0x00000000
Go: execution terminated with errors.
Many problems in your code.
la $a0, ($t0) is incorrect. The assembler should warn you. la is a two instructions macro that` loads an address from a label, while $t0 is a register (that already holds the value of str1).
It should be replaced by la $a0, str1 or (much better because it is a a unique instruction) mov $a0, $t1 as you already did this computation for $t1. You have the same problem for str2, str3 and str4.
But there is no need to use a temporary register and you could directly do the computation in $a0.
There is another problem. According to documentation after syscall 5 "$v0 contains integer read". So the sw $s0, ($v0) after reading the ints are incorrect. What should be stored is $v0 and the store address is num1 and has already been loaded in $s0.
add $s2, $s1, $s0 is also incorrect, as $s0 and $s1 values are integer addresses, not integer values.
Last, you cannot print at once a string and an integer. It must be done in two syscalls : syscall 4 to print a string and syscall 1 to print the integer.
.data
str1: .asciiz "Enter number 1: "
str2: .asciiz "Enter number 2: "
str3: .asciiz "The sum of the two number is: "
str4: .asciiz "The difference of the two number is: "
num1: .word 4
num2: .word 4
.text
.globl main
main:
la $a0, str1
li $v0, 4 #syscall code for print_str
syscall #print str1
li $v0, 5 #syscall code for read int
syscall #read int
mov $s0, $v0 ## keep the value of int in a register
la $t1, num2
sw $v0, ($t1) #store the entered value in num2
li $v0, 4 #syscall for print_str
la $a0, str2 #address of string to print
syscall #print str2
li $v0, 5 #syscall code for read int
syscall #read int
mov $s1, $v0 ## keep the value of int in a register
la $t1, num2
sw $v0, ($t1) #store the entered value in num2
add $s2, $s0, $s1 #add num1 and num2
la $a0, str3
li $v0, 4 #syscall for print_str
syscall #print str3
li $v0, 1 #syscall for print integer
mov $a0, $s2 #value of the sum
syscall #print sum
sub $s3, $t0, $t1 #substract num1 and num2
la $a0, str4
li $v0, 4 #syscall for print_str
syscall #print str4
li $v0, 1 #syscall for print integer
mov $a0, $s3 #value of the diff
syscall #print diff value
BTW, storing the integers is useless. I kept them because it is in the title of the question, but it you just need the sum and diff, the sw instructions and corresponding la can be suppressed.

Masking an integer in MIPS (quick and easy)

I want to input an integer and have it be expressed in both binary and hex. Then I want to move bits 12, 13, 14, and 15 to the least significant bits of $a0, and have the output be expressed as a binary and hex. Here's my program:
.data
enter: .asciiz "Please enter your integer:\n"
binaryI: .asciiz "\nHere is the input in binary: "
hexI: .asciiz "\n\nHere is the input in hexadecimal: "
binaryO: .asciiz "\n\nHere is the output in binary: "
hexO: .asciiz "\n\nHere is the output in hexadecimal: "
.text
prompt:
li $v0, 4
la $a0, enter
syscall
li $v0, 5
syscall
add $s2, $0, $v0
li $v0, 4
la $a0, binaryI
syscall
li $v0, 35
move $a0, $s2
syscall
li $v0, 4
la $a0, hexI
syscall
li $v0, 34
move $a0, $s2
syscall
addi $t0, $0, 7
srl $s0, $s2, 12
and $s0, $s0, $t0
li $v0, 4
la $a0, hexO
syscall
li $v0, 35
move $a0, $s0
syscall
li $v0, 4
la $a0, binaryO
syscall
li $v0, 34
move $a0, $s0
syscall
li $v0, 1
add $a0, $0, $s0
syscall
li $v0, 10
syscall
For the integer 1006460, for example, the inputs and hex output work perfectly, but the binary output has an extra 5 at the end. The error I get is this:
Here is the output in binary: 0x000000055
What may have caused this extra 5 to be at the end of the output?
How silly of me. I should have deleted
li $v0, 1
add $a0, $0, $s0
syscall
from my 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"