在没有转换指令的情况下将浮点数转换为整数

时间:2019-03-09 02:37:50

标签: mips

我正在开发一个程序,该程序应该将用户输入的浮点数转换为整数,我可以使它工作,但仅适用于分数部分不超过2 ^ -1的浮点数,显然是1/2。适用于75.5或2.5之类的数字,但不适用于75.625之类的数字。我需要将代码保持这种格式,但是我不确定应该如何使程序左右移动(在主题标签周围)以取消小数部分,而不管小数部分是什么。

.data
 Prompt: .asciiz "\n Please Input a value for the float N = "
 result1: .asciiz " The converted N is "
.text
 li $v0, 4 # system call code for Print String
 la $a0, Prompt # loads address of prompt into $a0
 syscall # print the message in Prompt
 li $v0, 6 # System call code for Read Float
 syscall # read whatever you input float as 
 mfc1 $t1, $f0 # Stores the float value into the $t1
 srl $t2, $t1, 23  # srl by 23 to leave out the biased exponent and store it 
 in $t2
 add $s3, $t2, -127 # Subtract 127 to get the exponent
 sll $t4, $t1, 9 # Shift left and right by 9 to remove the exponent
 srl $t5, $t4, 9 
 addi $t6, $t5, 8388608 # Add the implied bit (2^24)
 add $t7, $s3, 9 # Add 9 to the exponent value
 sllv $s4, $t6, $t7 # Shifts the implied bit to 2^32 (32nd bit)
 ###########################
 rol $s5, $t6, $t7 #rotate to the left by whatever the exponent + 9 is to 
 get 
 the integer part of the number to the first few bits
 sll $s5, $s5, 1
 srl $s5, $s5, 1 # sll and srl cancels out the fractional part #should be 
 1001011 #s5 is the newly converted integer
 ######################
 li $v0, 4 # Tells computer to get ready to print a .asciiz number
 la $a0, result1
 syscall
 li $v0, 1 # Tells computer to get ready to print the converted integer 
 number
 move $a0, $s5 # Moves the contents of $s5 to %a0 so it can be called
 syscall # Returns the integer

0 个答案:

没有答案