• Support PF! Buy your school textbooks, materials and every day products Here!

MIPS Help. Fahrenheit to Celsius

  • Thread starter trouty323
  • Start date
  • #1
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?
 

Answers and Replies

  • #2
2,685
20
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
chiro
Science Advisor
4,790
131
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
131
0
I don't know MIPS either, but a quick http://courses.missouristate.edu/KenVollmar/Mars/Help/SyscallHelp.html" [Broken] 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
2,685
20
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
131
0
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
2,685
20
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
24
0
Thanks for all of the suggestions everybody. Luckily, I was able to get help from a close friend. Thanks again.
 
  • #9
2,685
20
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
131
0
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
131
0
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
2,685
20
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
33,519
5,196
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.
 

Related Threads on MIPS Help. Fahrenheit to Celsius

Replies
25
Views
11K
  • Last Post
Replies
1
Views
667
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
701
  • Last Post
Replies
5
Views
14K
  • Last Post
Replies
5
Views
8K
Replies
1
Views
634
  • Last Post
Replies
13
Views
32K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
9
Views
5K
Top