输出浮点值

  • 本文关键字:输出 assembly mips
  • 更新时间 :
  • 英文 :


我只是想创建一个程序,从键盘上读取浮点值(即单精度数字(,然后输出它,没有警告或错误,但我无法得到正确的答案,相反,我得到了0.00000000。我的代码出了什么问题?这是我的代码:

.data 0x10000000
msg1: .asciiz "Please enter a float: "
.text
.globl main
main: addu $s0, $ra, $0  # save $31 in $16
li $v0, 4 # system call for print_str
la $a0, msg1 # address of string to print
syscall
li $v0, 6 # system call for read_float
syscall # the float placed in $f0
mtc1 $t0, $f0# move the number in $t0
sll $t0, $t0, 4 
# print the result
li $v0, 2 # system call for print_float
mfc1 $t0, $f12 # move number to print in $f12
syscall
# restore now the return address in $ra and return from main
addu $ra, $0, $s0 # return address back in $31
jr $ra # return from main

您的mtc1mfc1是向后的。

  • mtc1表示GPR -> FPR
  • mfc1表示GPR <- FPR

所以,如果我们看看这个代码的实际作用:

mtc1 $t0, $f0      # Sets $f0 = $t0. You haven't specified a value for $t0 at this point,
# but it doesn't really matter since you're not using $f0 any more
# after this point.
sll $t0, $t0, 4    # Shifts whatever was in $t0 4 bits to the left.
li $v0, 2          # System call for print_float.
mfc1 $t0, $f12     # Set $t0 = $f12.
syscall            # Calls print_float without having specified a value for $f12.

我不明白这个转变的意义,因为你说你只想打印读取的数字,所以上面的代码可以替换为:

mov.s $f12, $f0
li $v0, 2
syscall

如果您do出于某种原因仍想执行该转换,则需要将mtc1更改为mfc1,反之亦然。

最新更新