# f2c.s -- Convert from Fahrenheit to Celcius, using floating-point. # based on code given on p.209 in textbook. # # float f2c (float f) <======= This is what the code # { looks like in C. # return 5.0/9.0 * (f - 32.0); # } #---------------------------------------------------------------------- .data float_constants: .float 5.0 .float 9.0 .float 32.0 prompt: .asciiz "Please enter Fahrenheit temperature: " celcius_is: .asciiz "The equivalent Celcius temp is " degree_str: .asciiz " degrees.\n" #---------------------------------------------------------------------- .text main: la $gp, float_constants # set $gp = addr of value 5.0 li $v0, 4 # print prompt for user la $a0, prompt syscall li $v0, 6 # get number from user syscall # automatically goes in $f0, mov.s $f12, $f0 # so put it into $f12 jal f2c # call the convert function li $v0, 4 # get user ready for answer la $a0, celcius_is syscall li $v0, 2 # print answer mov.s $f12, $f0 # move return value into $f12 for printing syscall li $v0, 4 # "degrees.\n" la $a0, degree_str syscall li $v0, 10 # end of program syscall #---------------------------------------------------------------------- # The conversion function. We use $f12 as the Fahrenheit parameter. # $f16 and $f18 are the temporaries used to hold constants and calculated # values. We return the Celcius result in $f0. f2c: l.s $f16, 0($gp) # set $f16 = 5.0 l.s $f18, 4($gp) # set $f18 = 9.0 div.s $f16, $f16, $f18 # compute $f16 = 5.0/9.0 l.s $f18, 8($gp) # set $f18 = 32.0 sub.s $f18, $f12, $f18 # compute $f18 = (f - 32.0) mul.s $f0, $f16, $f18 # product of 5.0/9.0 and f - 32.0 jr $ra # return