0

I am very new to MIPS programing and I was wondering if you could help me out. I need to write a program that converts a temperature (input by the user) from either Fahrenheit to Celsius, or Celsius to Fahrenheit depending on what the user defines. For example, if the user inputs 100 C the output would be 100 C is the same as 212 F.

I am able to find out if the user puts a C or an F after the temp and jump to the appropriate code to do the conversions, the problem is the way I have it now, the program treats the input as a String, not an integer. It won't do any math on a String. But for some reason, it will output a ridiculously large number and I dont know why. Is there a way to search an integer for C or F? Any help is greatly appreciated!

Here's what I have so far:

.text
main:

# Print String 1
la $a0, str1        # put the address of the string to display in register $a0
li $v0, 4           # This is the system service to display the string
syscall

la $a0, memAddr 
li $v0, 8           # Loads System service to read string
syscall

la $s0, ($a0)       # Copies the user input to $s0

la $t1, memAddr 

loop:
    lb $t2, ($t1)
    beq $t2, 'C', Fahrenheit
    beq $t2, 'F', Celsius
    add $t1, $t1, 1 
    j loop

Fahrenheit:
    # Print user input
    li $v0, 4           # Loads System service to print integer
    move $a0, $s0
    syscall

    # Print String 2
    la $a0, str2        
    li $v0, 4           
    syscall

    # Celsius to Fahrenheit:  F = C*(9/5)+32
    li $t0, 32
    mtc1 $t0, $f0
    cvt.s.w $f0, $f0
    mtc1 $s0, $f3
    cvt.s.w $f3, $f3
    li.s $f1, 1.8
    mul.s $f2, $f3, $f1
    add.s $f4, $f2, $f0

    # Convert float to int
    cvt.w.s $f4, $f4
    mfc1 $t0, $f4

    # Print result
    li $v0, 1           # Loads System service to print integer
    move $a0, $t0
    syscall

    # Print String 5
    la $a0, str5    
    li $v0, 4           
    syscall

    j Exit

Celsius:
    # Print user input
    li $v0, 4           # Loads System service to print integer
    move $a0, $s0
    syscall

    # Print String 2
    la $a0, str2        
    li $v0, 4           
    syscall

    # Fahrenheit to Celsius:  C = (F-32)*(5/9)
    li.s $f1, 0.55555
    sub.s $f2, $f3, $f0
    mul.s $f4, $f2, $f1

    # Convert float to int
    cvt.w.s $f4, $f4
    mfc1 $t0, $f4

    # Print result
    li $v0, 1           # Loads System service to print integer
    move $a0, $t0
    syscall

    # Print String 4
    la $a0, str4        
    li $v0, 4           
    syscall

    j Exit

Exit:
    li $v0, 10
    syscall

.data
str1: .asciiz "Enter a number to convert: \n"
str2: .asciiz " is the same as: "
str4: .asciiz " C"
str5: .asciiz " F"

memAddr: .space 40
Welcor
  • 2,431
  • 21
  • 32
Kyle2595
  • 193
  • 1
  • 6
  • 15
  • You need to convert the string to an integer, otherwise you're operating with the ASCII representation of the string. – m0skit0 Feb 12 '15 at 19:57
  • Also `# Copies the user input to $s0` is a wrong comment. You're **pointing** to the user input, not **copying** it. – m0skit0 Feb 12 '15 at 19:59
  • Okay, I've fixed the comment and I've been searching Google on how to convert the String to an integer and I don't understand much of it. Do I need to iterate through the user input and convert it to ASCII then subtract 48 (because the ASCII code for int 0 is 48)? – Kyle2595 Feb 12 '15 at 21:18
  • Correct, but position also matters as you surely now, e.g. 2 in first position is not 2 in third position (because 3rd position has a 10^2 = 100 weight). – m0skit0 Feb 12 '15 at 21:25

0 Answers0