MIPS Help. Fahrenheit to Celsius

In summary: Doing so will make it much easier for other people trying to understand your code, and might even prevent them from making the same mistakes.In summary, the program does the following:- Prints "Please input degrees in fahrenheit: "- Prompts user for input- Stores the input in $v0- Calculates $t0=($v0-32)*5/9- Prints "Degrees in celsius is:"- Prints value in $t0- Prints "Program terminated"
  • #1
trouty323
24
0
Code:
# Fahrenheit to Celsius

.data	# following are data
prompt: .asciiz "\nPlease input degrees in fahrenheit: "	# prompt user for input
result: .asciiz "\nDegrees in celsius is: "			# display degrees in celsius
bye: .asciiz "\nProgram terminated"				# display when program terminates


.globl main


.text	# following are instructions
main:	# exection begins

li $v0, 4	# system call to print string
la $a0, prompt	# put prompt address into register a0
syscall

li $v0, 5	# system call to read integer
syscall

addi $t0, $v0, -32
mul $t0, $t0, 5
div $t0, $t0, 9

la $a0, result
li $v0, 4
syscall

move $a0, $t0
li $v0, 2
syscall

End:

la $a0, bye
li $v0, 4
syscall

li $v0, 10
syscall

No matter what I type in, I am getting a result of 0. Can anyone locate what I am doing wrong?
 
Physics news on Phys.org
  • #2
I don't know this language, but looking at your calcs, they use the variable t0 but you finish calling v0.

So it looks like you've got:

t0 = v0 - 32
t0 = t0 / 5
t0 = t0 * 9

and then you call v0 at the end instead of t0.
 
  • #3
Assuming all those syscalls act like a DOS interrupt, I agree with jared in that you are passing the wrong value from the wrong register.
 
  • #4
I don't know MIPS either, but a quick http://courses.missouristate.edu/KenVollmar/Mars/Help/SyscallHelp.html" showed that a syscall with $v0=2 is "print float" with the value in $f12, but you seem to be working with integers. syscall $v0=1 seems to be for integers with the value in $a0.

Code:
move $a0, $t0
li $v0, 2     [i]<--- Should be 1 instead of 2?[/i]
syscall
 
Last edited by a moderator:
  • #5
Either way, after all the calculations are done with t0, it's not used again. Only v0 and a0.

So the value for t0 - the conversion - is missed out completely.
 
  • #6
Did you scroll down a bit in the code section of the OP? The instruction "move $a0, $t0" moves the value from register $t0 to $a0, which is used by the syscall routine (or whatever syscall is). I don't know if the previous syscall overwrites the value in $t0 or not.
 
  • #7
caffenta said:
Did you scroll down a bit in the code section of the OP? The instruction "move $a0, $t0" moves the value from register $t0 to $a0, which is used by the syscall routine (or whatever syscall is).

Yes, but the 'result' function is used before that.

The result is called using a0 and v0. Then after that a0 is made t0.
 
  • #8
Thanks for all of the suggestions everybody. Luckily, I was able to get help from a close friend. Thanks again.
 
  • #9
trouty323 said:
Thanks for all of the suggestions everybody. Luckily, I was able to get help from a close friend. Thanks again.

And the solution was? (I don't know the language so am somewhat curious.)
 
  • #10
result is the string "Degrees in celsius is:"

From what I can tell, the program does this:

1) Prints "Please input degrees in fahrenheit:"
2) Waits for user to enter an integer, stores value in $v0
3) Calculates $t0=($v0-32)*5/9
4) Prints "Degrees in celsius is:"
5) Prints value in $t0 (maybe the bug since the wrong value is stored in $v0)
6) Prints "Program terminated"
 
  • #11
jarednjames said:
And the solution was? (I don't know the language so am somewhat curious.)

I'm also curious. Don't leave us hanging, trouty323... :smile:
 
Last edited:
  • #12
caffenta said:
result is the string "Degrees in celsius is:"

From what I can tell, the program does this:

1) Prints "Please input degrees in fahrenheit:"
2) Waits for user to enter an integer, stores value in $v0
3) Calculates $t0=($v0-32)*5/9
4) Prints "Degrees in celsius is:"
5) Prints value in $t0 (maybe the bug since the wrong value is stored in $v0)
6) Prints "Program terminated"

Ah I see, I was looking at it printing 4 and 5 together.

Still curious why you wouldn't print t0 directly though.
 
  • #13
trouty323,
When you're writing assembly code it is especially crucial to add comments indicating what you're doing. Each block of statements should be commented, especially the block where you're calculating the Celsius temp and the blocks where you're doing input or output.
 

1. How do I convert Fahrenheit to Celsius using MIPS?

To convert Fahrenheit to Celsius in MIPS, you can use the following formula:
Celsius = (5/9) * (Fahrenheit - 32)
This can be done using the multiply (mul) and subtract (sub) instructions in MIPS.

2. Can I convert a range of temperatures from Fahrenheit to Celsius at once using MIPS?

Yes, you can use a loop to convert a range of temperatures from Fahrenheit to Celsius in MIPS. You can use the loop counter to keep track of the number of iterations and use the loop index as the input Fahrenheit temperature for each iteration.

3. Is there a MIPS instruction for converting Fahrenheit to Celsius?

No, there is no specific instruction in MIPS for converting Fahrenheit to Celsius. However, you can use a combination of instructions such as mul and sub to perform the conversion.

4. How do I input the Fahrenheit temperature in MIPS for conversion to Celsius?

You can use the MIPS syscall instruction to prompt the user for input and store the input in a register. Then, you can use that register as the input Fahrenheit temperature for the conversion formula.

5. Can I convert Celsius to Fahrenheit using MIPS?

Yes, you can convert Celsius to Fahrenheit using MIPS by using a similar formula:
Fahrenheit = (9/5) * Celsius + 32
Simply replace the Fahrenheit temperature with the Celsius temperature in the formula and use the appropriate MIPS instructions to perform the calculation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
9K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top