Mips assembly language on MARS - mips

I'm trying to create a program that will take a FP number (5.2) and output in increments of 1.0 until the highest value (20.2) is reached.
So the smallest value is 5.2 and the biggest is 20.2 there should be a loop.
The output should be:
5.2 6.2 7.2 and so on until 20.2

Try this code :
.data
spaces: .asciiz " "
fp1: .double 5.2
fp2: .double 20.2
fp3: .double 1.0
.text
main:
l.d $f0,fp1 # $f0 register contains 5.2
l.d $f2,fp2 # $f2 register contains 20.2
l.d $f4,fp3 # $f4 register contains 1.0
loop:
li $v0, 3
mov.d $f12,$f0
syscall # syscall print floating point number
li $v0 , 4
la $a0 , spaces
syscall # syscall print space
c.eq.d $f0,$f2 # if $f0 == $f2 jump to EXIT
bc1t EXIT
add.d $f0,$f0,$f4 # else $f0 = $f0+$f4
j loop
EXIT:
li $a0, 0
li $v0, 17 #exit
syscall

Related

How to store the input correctly in MIPS?

Although I have try to input different value ,but the Qtspim still print out the value"0" and the input is also not saved in the register ,I try to find the answer in internet,but it seem like no one have face this problem before.I do it work for 4hrs, I FEEL very frustrated with this.
here is my code ,I try to define whether the input is prime number or not.
# Power program
# ----------------------------------
# Data Segment
.data
request: .asciiz "Enter an integer:"
Yes: .asciiz " is a prime number.\n"
No: .asciiz " is not a prime number\n."
End: .asciiz"End!\n"
Key: .word -1
One: .word 1
# ----------------------------------
# Text/Code Segment
.text
.globl main
main:
lw $t1,One
add $s2,$zero,$zero
lw $s3,One
lw $s0,Key
add $s5,$zero,$zero
j power # call the function power
li $v0, 10 # call code for exit
syscall # system call
.end main
.globl power
power:
j power_loop
power_loop:
la $a0, request # load string request in memory to $a0
li $v0, 4 # print out the string
syscall #prevent Memory address out of bounds
li $v0, 5
syscall
move $s7,$v0
beq $s7, $s0, exit_L #when input =-1 exit
j Prime #otherwise go to define the input whether prime num
Prime:
beq $s7,$t1,result
div $s7,$t1
mthi $t4
beq $t4,$zero,count
addi $t1,$t1,1
j Prime
count: #counting the number of factors
addi $s2,1
addi $t1,$t1,1
result: addi $t1,$zero,1
bne $s2,$s3,resultNo
beq $s2,$s3,resultYes
resultYes: li $v0,1
move $a0,$s7
syscall
add $s2,$zero,$zero
la $a0,Yes
li $v0,4
syscall
j power_loop
resultNo: li $v0,1
move $a0,$s7
syscall
add $s2,$zero,$zero
la $a0,No
li $v0,4
syscall
j power_loop
exit_L:
la $a0,End
li $v0,4
syscall
jr $ra
.end power

There is something wrong with the output of this program MIPS(double)

I am writing a program in MIPS to convert inches into centimeters, but the result is always evaluating to zero. I don't know where I did wrong. I have written the program below. It's compiling, but not evaluating the correct result, always giving 0.
#declaring some things
.data
inchesText: .asciiz "Enter the number in inches: "
resultText: .asciiz " Centimeters are ==> "
inches: .double 0
inchesToCenti: .double 2.54
centi: .double 0
zero: .word 0
result: .double 0
.text
main:
jal getInches
jal inches_To_Centi
jal finalResult
jal Exit
getInches:
# printing string
la $a0,inchesText
li $v0, 4
syscall
# get inches
li $v0, 7
syscall
s.d $f2, inches
jr $ra
inches_To_Centi:
# loading the formula contstant as it is
l.d $f0, inchesToCenti
#actual inches gained through argument
l.d $f2, inches
# mul both of these to get the centimeters
mul.d $f6, $f0, $f2
s.d $f6, centi
jr $ra
finalResult:
# printing text
la $a0, resultText
li $v0, 4
syscall
# now printing the actual value
l.d $f12, centi
li $v0, 3
syscall
Exit:
li $v0, 10
syscall
Is it a lot of time I do not do assemble but I think the solution of your problem is all about the system call you use to read the double value.
The syscall 7 does not store the input value in the $f2 register but into $f0 one.
Change the line #26 to
s.d $f0 inches
To give a little more context, as the line number are not present, the getInches subroutine needs the fix:
getInches:
# printing string
la $a0,inchesText
li $v0, 4
syscall
# get inches
li $v0, 7
syscall
s.d $f0, inches
jr $ra

A mess up with printing text in mips

I'm writing a code that print through a loop "Enter a number" 20 times, and it happens that the text that I want to print after "The Average is" gets messed up like this "ó???ff¦?". Here's the code:
.data
array: .space 20
outputA: .asciiz "The Average is:\n" #prints the average
input: .asciiz "Enter a number:\n" #prints the statement
avgNum: .float 0.0
lengthFloat: .float 3.0
const: .float 0.0 #I did this because you can't use li for float numbers
zero: .float 0.0
one: .float 1.0
.text
la $t9, outputA
lwc1 $f2, lengthFloat
li $t0, 0 #counter i
li $s2, 0 #counter j
la $s1, array #base address of the array
la $k1, input #Displaying the message (The else part)
li $t5, 0 #currentCount for mode
l.s $f3, const #mode value
li $t6, 0 #count for mode
l.s $f14, zero #Just a 0
l.s $f16, one
loop:
slti $s0, $t0, 20 #Checking if the counter is less than 20
beq $s0, $zero, print #if it's greater or equal to 20 exit the loop
move $a0, $k1
li $v0, 4
syscall
li $v0, 6 #Reading a float number
syscall
sll $t1, $t0, 2 #Storing the value in the appropriate place in memory
add $t1, $s1, $t1
swc1 $f0, 0($t1)
add.s $f1, $f1, $f0
addi $t0, $t0, 1 #Adding the counter 1
j loop
print:
#printing avg
move $a0, $t9
li $v0, 4
syscall
li $v0, 2
div.s $f12, $f1, $f2
syscall
mov.s $f10, $f12 #Storig the value of average for later use
A single-precision floating-point number is 4 bytes, so 20 floats require 80 bytes of space. You've only reserved 20 bytes of space, so you'll end up overwriting something else. You'll have to change the declaration of array to array: .space 20*4
Also, you're dividing the sum by 3.0, so you're not calculating the average since that would require dividing by 20.0.

Mips div.s syntax error

I am using QtSpim 9.9.1 to write my Computer Architecture course homework
i had a syntax error when use div.s operator but it's OK when I use div. Another error appears when try to get float from user but it also disappears when get integer. Here is my code :
.text
main:
# Print "This program solves an equation of style a*x + c = 0, print out the result on console."
la $a0, hello_msg # load the address of hello_msg into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall # do the syscall.
# Print "Enter (a) value please :"
la $a0, enter_a_msg # load the address of enter_a_msg into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall # do the syscall.
## Get (a) from user, put into $t0.
li $v0, 6 # load syscall read_float into $v0.
syscall # make the syscall.
move $t0, $f0 # move the number read into $t0.////////here got error//////
# Print "Enter (c) value please :"
la $a0, enter_c_msg # load the address of enter_c_msg into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall # do the syscall.
## Get (c) from user, put into $t1.
li $v0, 6 # load syscall read_float into $v0.
syscall # make the syscall.
move $t1, $f0 # move the number read into $t1.///////also here
# Compute (x), put into $t2.
neg $t3, $t1 # get -c into $t3.
div.s $t2, $t3, $t0 # get x = -c / a into $t2.//// also here Error
# Print "Value of (x) is :"
la $a0, result_msg # load the address of result_msg into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall # do the syscall.
# Print (x) value .
move $a0, $t2 # move (x) value into $a0.
li $v0, 1 # load syscall print_int into $v0.
syscall # make the syscall.
li $v0, 10 # 10 is the exit syscall.
syscall # do the syscall.
# Data for the program:
.data
hello_msg: .asciiz "This program solves an equation of style a*x + c = 0,\nprints out the result on console.\n"
enter_a_msg: .asciiz "Enter (a) value please :"
enter_c_msg: .asciiz "Enter (c) value please :"
result_msg: .asciiz "Value of (x) is :"
# end Equation.asm
move $t0, $f0
To convert a floating-point register to an integer and move it to a general-purpose register you should use something like this:
cvt.w.s $f0, $f0
mfc1 $t0, $f0
If you wanted to move $f0 to another floating-point register you should use e.g.:
mov.s $f1,$f0
div.s $t2, $t3, $t0
div.s works with floating-point registers ($f0-$f31), not with the general-purpose registers ($an, $tn, $vn and so on). Refer to the MIPS floating-point instruction set list.

how to solve exception 5 (Store Word) in MIPS?

.data
string1:.asciiz "Enter number\n"
matrix:.space 100
i: .word 0
k: .word 0
.text
main:
lw $t1,i
lw $t2,k
la $s0,matrix
jal Build_matrix
j exit
Build_matrix:
li $t3,25
li $t4,0
li $a1,0
Scanning:
bge $t1,$t3,Return #if i>=25,stop
li $v0,4
la $a0,string1
syscall
add $t4,$t2,$t2 #2j
add $t4,$t4,$t4 #4j
add $a1,$s0,$t4
li $v0,5
syscall
sw $v0,0($a1)
addi $t1,$t1,1
addi $t2,$t2,1
j Scanning
Return:
jr $ra
#**** End of Build_matrix method ****
exit:
This is the code to take 25 numbers as input from user(asks a number 25 times) and store them in memory.There are no compile time errors.But during runtime,this message-"Exception 5 [Address error in store] occurred and ignored" is displayed everytime after Enter number is displayed?what is my mistake.....Thanks
The problem you are facing is that the matrix is not word-aligned, which is needed by sw instructions.
You have to add a .align 2 directive after the matrix label:
matrix:
.align 2 # ensure matrix is properly aligned
.space 100