Mips 浮点加法

发布于 2024-10-25 22:59:53 字数 1955 浏览 12 评论 0原文

因此,我尝试用 MIPS 汇编代码编写一个程序,以帮助我更好地理解浮点加法的工作原理。我了解浮点数如何分解为 1 位符号、8 位指数和 23 位分数。我编写了一个程序,它从用户那里获取两个输入,并将它们相加,而不使用除 mtc1 和 mfc1 (用于输入和输出)之外的任何浮点指令。我的代码有错误,因为当我添加 1 + 2 时,我得到 2.74804688。我仍在尝试调试代码,但似乎无法理解问题所在。如果有人可以提供帮助,我将不胜感激。

这是我的代码(不包括用户输入...第一个浮点值位于 $s0 中,第二个浮点值位于 $s1 中)

#Integer implementation of floating-point addition
#Initialize variables
add $s0,$t0,$zero #first integer value
add $s1,$t1,$zero #second integer value
add $s2,$zero,$zero #initialize sum variable to 0
add $t3,$zero,$zero #initialize SUM OF SIGNIFICANDS value to 0

#get EXPONENT from values
sll $s5,$s0,1 #getting the exponent value
srl $s5,$s5,24 #$s5 = first value EXPONENT

sll $s6,$s1,1 #getting the exponent value
srl $s6,$s6,24 #$s6 = second value EXPONENT

#get SIGN from values
srl $s3,$s0,31 #$s3 = first value SIGN
srl $s4,$s1,31 #$s4 = second value SIGN

#get FRACTION from values
sll $s7,$s0,9
srl $s7,$s0,9 #$s7 = first value FRACTION
sll $t8,$s1,9
srl $t8,$s1,9 #$t8 = second value FRACTION

#compare the exponents of the two numbers
compareExp: ######################

beq $s5,$s6, addSig
blt $s5,$s6, shift1 #if first < second, go to shift1
blt $s6,$s5, shift2 #if second < first, go to shift2
j compareExp

shift1: #shift the smaller number to the right
srl $s7,$s7,1 #shift to the right 1
addi $s5,$s5,1
j compareExp

shift2: #shift the smaller number to the right
#srl $s0,$s0,1 #shift to the right 1
#j compareExp
srl $t8,$t8,1 #shift to the right 1
addi $s6,$s6,1
j compareExp

addSig:

add $t3,$s7,$t8 #Add the SIGNIFICANDS

li $v0, 4
la $a0, sum
syscall

li $v0, 1
move $a0, $t3
syscall

j result

result:
li $v0, 4
la $a0, newline
syscall

sll $t4,$s3,31 #SIGN
#FRACTION
sll $t5,$s6,23 #EXPONENT
add $t6,$t4,$t5
add $t6,$t6,$t3

li $v0, 4
la $a0, sum2
syscall

li $v0, 1
move $a0, $t6
syscall

li $v0, 4
la $a0, newline
syscall

li $v0, 4
la $a0, sum2
syscall

li $v0,2
mtc1 $t6,$f12
syscall
jr $31
# END OF PROGRAM

So I'm trying to write a program in MIPS assembly code to help me better understand how floating point addition works. I understand how a floating point number is broken up into a 1 bit sign, 8 bit exponent, and 23 bit fraction. I've written a program that gets two inputs from a user, and adds them WITHOUT using any floating point instructions except mtc1 and mfc1 (for input and output). My code has bugs because when I add 1 + 2 I get 2.74804688. I'm still trying to debug the code but can't seem to grasp the problem. If anyone can help, I would greatly appreciate it.

THIS IS MY CODE (excluding the user input...the first floating point value is in $s0, and the second in $s1)

#Integer implementation of floating-point addition
#Initialize variables
add $s0,$t0,$zero #first integer value
add $s1,$t1,$zero #second integer value
add $s2,$zero,$zero #initialize sum variable to 0
add $t3,$zero,$zero #initialize SUM OF SIGNIFICANDS value to 0

#get EXPONENT from values
sll $s5,$s0,1 #getting the exponent value
srl $s5,$s5,24 #$s5 = first value EXPONENT

sll $s6,$s1,1 #getting the exponent value
srl $s6,$s6,24 #$s6 = second value EXPONENT

#get SIGN from values
srl $s3,$s0,31 #$s3 = first value SIGN
srl $s4,$s1,31 #$s4 = second value SIGN

#get FRACTION from values
sll $s7,$s0,9
srl $s7,$s0,9 #$s7 = first value FRACTION
sll $t8,$s1,9
srl $t8,$s1,9 #$t8 = second value FRACTION

#compare the exponents of the two numbers
compareExp: ######################

beq $s5,$s6, addSig
blt $s5,$s6, shift1 #if first < second, go to shift1
blt $s6,$s5, shift2 #if second < first, go to shift2
j compareExp

shift1: #shift the smaller number to the right
srl $s7,$s7,1 #shift to the right 1
addi $s5,$s5,1
j compareExp

shift2: #shift the smaller number to the right
#srl $s0,$s0,1 #shift to the right 1
#j compareExp
srl $t8,$t8,1 #shift to the right 1
addi $s6,$s6,1
j compareExp

addSig:

add $t3,$s7,$t8 #Add the SIGNIFICANDS

li $v0, 4
la $a0, sum
syscall

li $v0, 1
move $a0, $t3
syscall

j result

result:
li $v0, 4
la $a0, newline
syscall

sll $t4,$s3,31 #SIGN
#FRACTION
sll $t5,$s6,23 #EXPONENT
add $t6,$t4,$t5
add $t6,$t6,$t3

li $v0, 4
la $a0, sum2
syscall

li $v0, 1
move $a0, $t6
syscall

li $v0, 4
la $a0, newline
syscall

li $v0, 4
la $a0, sum2
syscall

li $v0,2
mtc1 $t6,$f12
syscall
jr $31
# END OF PROGRAM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2024-11-01 22:59:53

特别不确定 MIPS,但通常浮点值在分数的最高位之前会有一个隐含的“1”位。零是一种特殊情况,没有隐含位。

Not sure about MIPS in particular, but often floating point values will have an implied "1" bit just before the top bit of the fraction. Zero is a special case which does not have the implied bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文