2013-04-08 66 views
0

还好只是为了测试我有这样的代码MIPS联想浮点

.data 
    # This shows you can use a .word and directly encode the value in hex 
    # if you so choose 
num1: .word 0x3F800000 
num2: .float 1234.567 


num3: .float 45.67834 
num4: .float 0.0004 
result: .word 0 
string: .asciiz "\n" 

    .text 
main: 
    la $t0, num1 
    lwc1 $f2, 4($t0) 
    lwc1 $f4, 8($t0) 
    lwc1 $f6, 12($t0) 


    # Print out the values of the summands 

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

    li $v0, 4 
    la $a0, string 
    syscall 

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

    li $v0, 4 
    la $a0, string 
    syscall 
    li $v0, 4 
    la $a0, string 
    syscall 

    # Do the actual addition 

    add.s $f12, $f2, $f6 
    add.s $f12, $f12, $f4 


    # Transfer the value from the floating point reg to the integer reg 

    swc1 $f12, 8($t0) 
    lw $s0, 8($t0) 

    # At this point, $f12 holds the sum, and $s0 holds the sum which can 
    # be read in hexadecimal 

    li $v0, 2 
    syscall 
    li $v0, 4 
    la $a0, string 
    syscall 

    # This jr crashes MARS 
    # jr $ra 

我有这个

add.s $f12, $f2, $f6 
    add.s $f12, $f12, $f4 

我试着调换顺序说

add.s $f12, $f4, $f6 
    add.s $f12, $f12, $f2 

但结果是相同

我检查维基百科浮动其中的arent相同点加法的例子,但这总是以1280.2457

http://en.wikipedia.org/wiki/Floating_point

他们有这种情况发生:

a = 1234.567, b = 45.67834, c = 0.0004 
(a + b) + c: 
    1234.567 (a) 
    + 45.67834 (b) 
    ____________ 
    1280.24534 rounds to 1280.245 
    1280.245 (a + b) 
    + 0.0004 (c) 
    ____________ 
    1280.2454 rounds to 1280.245 <--- (a + b) + c 
a + (b + c): 
    45.67834 (b) 
+ 0.0004 (c) 
____________ 
    45.67874 
    1234.567 (a) 
+ 45.67874 (b + c) 
____________ 
    1280.24574 rounds to 1280.246 <--- a + (b + c) 

没有hapepn对我来说这只是与我尝试

回答

0

范围内的任何值的工作,我得到它

a=0.00004 
b=45.67840 
c=1234.567