0

I have to multiply and print two double precision floating point numbers, my code is the following:

li $v0, 7       # $v0 =7    
syscall         # run read_double
jal p4          # run println

mfc1 $v0, $f4       # $v0 = $f4

li.d $f2, 4.3934567
mul.d $f0, $f4, $f2
mov.d $f12, $f0     # $f12 = $f6


li $v0, 3       #  $v0 =3
syscall         # run print_double
jal p4          # run println
jr $ra

Anyone knows why this code returns always 0?

robertina
  • 64
  • 11
  • is `$f4` already assigned a value? `li.d $f2, 4.3934567` is set but I don't see `$f4`. – Felix Castor Aug 24 '16 at 12:18
  • yes,sorry, i didn't copy all the code cause it was too long, but $f4 is filled from input line in prompt – robertina Aug 24 '16 at 12:21
  • now i just copy all the code – robertina Aug 24 '16 at 12:40
  • I'm not an expert on assembly but have you tried `mtc1 $v0, $f4` instead of `mfc1 $v0, $f4`? – Felix Castor Aug 24 '16 at 12:48
  • I still don't see where you set `$f4` to anything prior to the multiplication. Syscall 7 puts the result in `$f0`. – Michael Aug 24 '16 at 12:56
  • @FelixCastor you are right , cause I have to move from $v0 TO coprocessor1. But now i have another problem, any number I digit , the program returns 7,90505033345994471e-323 ...so creepy – robertina Aug 24 '16 at 13:10
  • @Michael syscall 7 puts the result in $v0, and I move it to $f4 to make operations with floating point numbers – robertina Aug 24 '16 at 13:11
  • _"syscall 7 puts the result in $v0"_. [No, it doesn't](https://www.doc.ic.ac.uk/lab/secondyear/spim/node8.html). – Michael Aug 24 '16 at 13:12
  • Uh.. Why 7? The system call number (before `syscall`) still goes into `$v0`, if that's what you're trying to change. It's the _result_ (what you get back after `syscall`) that will be in `$f0`. – Michael Aug 24 '16 at 13:33

1 Answers1

1

As I mentioned in my comment, system call 7 returns the result in floating-point register $f0. So your code could be simplifed to:

li $v0, 7       # $v0 =7    
syscall         # run read_double

li.d $f2, 4.3934567
mul.d $f12, $f0, $f2   # multiply user input by $f2. put the result in $f12
                       # for easy printing.
Michael
  • 57,169
  • 9
  • 80
  • 125
  • everything is clear, but, for example, if I digit an int like 1, it returns 4.. or if I digit 1,1 it returns me 4,40000000000000036 instead of 4,83280237 . Do you know what could it be? – robertina Aug 24 '16 at 13:59
  • That doesn't match my results. If I enter 1 as the input I get 4.3934566999999998 as the output. I use PCSpim, not QtSpim, in case that matters. – Michael Aug 24 '16 at 14:02