2013-04-08 110 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