MIPS Help. Fahrenheit to Celsius

  • Thread starter Thread starter trouty323
  • Start date Start date
  • Tags Tags
    celsius Mips
AI Thread Summary
The discussion revolves around a MIPS assembly program intended to convert Fahrenheit to Celsius. Users identified issues with the program, particularly that it always outputs zero due to incorrect register usage. The main problem lies in using the wrong syscall for printing the calculated Celsius value, suggesting that syscall 1 should be used for integers instead of syscall 2. Participants emphasized the importance of clear comments in assembly code to clarify each step of the process, especially during calculations and input/output operations. Ultimately, the conversation highlights common pitfalls in programming with MIPS and the need for careful attention to detail.
trouty323
Messages
23
Reaction score
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
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.
 
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.
 
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:
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.
 
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.
 
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.
 
Thanks for all of the suggestions everybody. Luckily, I was able to get help from a close friend. Thanks again.
 
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.
 

Similar threads

Replies
4
Views
5K
Replies
2
Views
5K
Replies
1
Views
2K
Replies
1
Views
5K
Replies
1
Views
4K
Replies
3
Views
13K
Replies
1
Views
24K
Back
Top