如何提取数字的小数部分并将其输出到控制台?
我对汇编语言相当陌生,并且对如何截断浮点数然后使用该整数来运行程序感到困惑。我目前能够很好地运行该程序,但是,我无法让程序截断输入的浮点数并将其转换为整数。这是我到目前为止所拥有的。
.data
Prompt: .asciiz "\n Please Input a value for N = "
Result: .asciiz "\nThe sum of the integers from 1 to N is "
FP_Run: .asciiz "\nYou have entered a floating point number. The number you entered will be truncated. "
Bye: .asciiz "\n **** Adios Amigo - Have a good day****"
zeroAsFloat: .float 0.0
.globl main
.text
main:
li $v0, 4 #system call code for Print String
la $a0, Prompt #load address of prompt into $a0
syscall #print the prompt message
li $v0, 6 #system call code for Read Integer
syscall #reads the value of N into $v0
blez $v0, End #branch to end if $v0 < = 0
li $t0, 0 #clear register $t0 to 0
#Display message
lwc1 $f0, zeroAsFloat
li $v0, 4
la $a0, FP_Run
syscall
#Read user input
li $v0, 6
syscall
#Display value
li $v0, 2
add.s $f12, $f0, $f4
syscall
#Move the FP number into an integer register, NO CONVERSION
mfc1 $t1, $f0
srl $t2, $t1, 23
#Subtract 127 to get the exponent
addi $s3, $t2, -127 #This is the exponent
#Get the fraction
sll $t4, $t1, 9 #Shift out exponent and sign bit
srl $t5, $t4, 9 #Shift back to original position
addi $t6, $t5, 8388608 #add the implied bit to bit (2^23)
addi $t7, $s3, 9 #add 9 to the exponent
sllv $s4, $t6, $t7 #By Shifting to the left 9 + exponent
#Now get the integer
rol $t4, $t6, $t7 #Rotate the bits left by $t7 to get the integer
#Shift left 31 -exp to zero out the other bits
li $t5,31
sub $t2, $t5, $s3 #Shift value
sllv $t5, $t4, $t2 #Zero out the fraction part
srlv $s5, $t5, $t2 #reset integer bits Integer is in $s5
move $v0, $s5 #move the integer into v0
li $t0, 0 #zero out of t0
blez $t1, End
Loop:
add $t0, $t0, $v0 #sum of integers in register $t0
addi $v0, $v0, -1 #summing integers in reverse order
bnez $v0, Loop #branch to loop is $v0 is != 0
li $v0, 4 #system call code for Print String
la $a0, Result #load address of message into $a0
syscall #print the string
li $v0, 1 #system call code for Print Integer
move $a0, $t0 #move value to be printed to $a0
syscall #print sum of integers
b main #branch to main
End: li $v0, 4 #system call code for Print String
la $a0, Bye #load address of msg. into $a0
syscall #print the string
li $v0, 10 #terminate program run and
syscall #return control to system
I'm fairly new to Assembly language and was confused on how I can truncate a floating point number and then use that integer to run the program. I currently am able to run the program just fine however, I can not get the program to truncate the floating point number entered and convert it into an integer. This is what I have so far.
.data
Prompt: .asciiz "\n Please Input a value for N = "
Result: .asciiz "\nThe sum of the integers from 1 to N is "
FP_Run: .asciiz "\nYou have entered a floating point number. The number you entered will be truncated. "
Bye: .asciiz "\n **** Adios Amigo - Have a good day****"
zeroAsFloat: .float 0.0
.globl main
.text
main:
li $v0, 4 #system call code for Print String
la $a0, Prompt #load address of prompt into $a0
syscall #print the prompt message
li $v0, 6 #system call code for Read Integer
syscall #reads the value of N into $v0
blez $v0, End #branch to end if $v0 < = 0
li $t0, 0 #clear register $t0 to 0
#Display message
lwc1 $f0, zeroAsFloat
li $v0, 4
la $a0, FP_Run
syscall
#Read user input
li $v0, 6
syscall
#Display value
li $v0, 2
add.s $f12, $f0, $f4
syscall
#Move the FP number into an integer register, NO CONVERSION
mfc1 $t1, $f0
srl $t2, $t1, 23
#Subtract 127 to get the exponent
addi $s3, $t2, -127 #This is the exponent
#Get the fraction
sll $t4, $t1, 9 #Shift out exponent and sign bit
srl $t5, $t4, 9 #Shift back to original position
addi $t6, $t5, 8388608 #add the implied bit to bit (2^23)
addi $t7, $s3, 9 #add 9 to the exponent
sllv $s4, $t6, $t7 #By Shifting to the left 9 + exponent
#Now get the integer
rol $t4, $t6, $t7 #Rotate the bits left by $t7 to get the integer
#Shift left 31 -exp to zero out the other bits
li $t5,31
sub $t2, $t5, $s3 #Shift value
sllv $t5, $t4, $t2 #Zero out the fraction part
srlv $s5, $t5, $t2 #reset integer bits Integer is in $s5
move $v0, $s5 #move the integer into v0
li $t0, 0 #zero out of t0
blez $t1, End
Loop:
add $t0, $t0, $v0 #sum of integers in register $t0
addi $v0, $v0, -1 #summing integers in reverse order
bnez $v0, Loop #branch to loop is $v0 is != 0
li $v0, 4 #system call code for Print String
la $a0, Result #load address of message into $a0
syscall #print the string
li $v0, 1 #system call code for Print Integer
move $a0, $t0 #move value to be printed to $a0
syscall #print sum of integers
b main #branch to main
End: li $v0, 4 #system call code for Print String
la $a0, Bye #load address of msg. into $a0
syscall #print the string
li $v0, 10 #terminate program run and
syscall #return control to system
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论