MIPS Help. Fahrenheit to Celsius

  • Thread starter Thread starter trouty323
  • Start date Start date
  • Tags Tags
    celsius Mips
Click For Summary

Discussion Overview

The discussion revolves around a MIPS assembly language program intended to convert Fahrenheit to Celsius. Participants analyze the code, identify potential issues, and suggest corrections related to the handling of register values and system calls.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant notes that the program uses the variable $t0 for calculations but calls $v0 at the end, suggesting a potential error in value retrieval.
  • Another participant agrees and mentions that the syscall for printing integers should use $v0=1 instead of $v0=2, which is for printing floats.
  • Several participants discuss the flow of the program, indicating that the value in $t0 is not utilized correctly after calculations, leading to confusion about the output.
  • There is a suggestion that the instruction "move $a0, $t0" correctly transfers the calculated value to $a0 for the syscall, but there is uncertainty about whether previous syscalls overwrite $t0.
  • One participant emphasizes the importance of adding comments in assembly code to clarify the purpose of each block of statements, particularly for calculations and I/O operations.

Areas of Agreement / Disagreement

Participants express multiple competing views regarding the correct handling of register values and syscalls. The discussion remains unresolved, with no consensus on the exact solution to the problem.

Contextual Notes

There are limitations in understanding how the syscalls interact with the registers, and participants express uncertainty about the implications of overwriting register values during execution.

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 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 1 ·
Replies
1
Views
24K