跳轉到內容

SPARC 彙編/條件碼 & 分支

來自華夏公益教科書,自由的教科書

二進位制數系統

[編輯 | 編輯原始碼]

二進位制(以二為底)系統是計算機用來表示儲存在其中的資料的數字系統。與我們習慣的以十為底的系統形成對比,後者使用 0-9 的數字來表示所有可能的實數,而二進位制系統僅使用 0-1 的數字。例如,數字 2112 用二進位制表示為 100001000000,數字 3 為 11,正如你可能猜到的那樣,數字 0 仍然只是 0。

可能看起來以這種方式表示數字只會帶來不必要的麻煩和複雜性,從而阻礙了可用性,但實際上並非如此。計算機工程師之所以選擇使用這種相對陌生的數字系統,是因為它在使用計算機電路實現和表示時要簡單得多,因此與使用更高基數系統相比,建立更復雜的計算機元件要簡單得多。

二進位制算術

[編輯 | 編輯原始碼]

雖然可能看起來並非如此,但二進位制數可以像十進位制數一樣使用。你可以將它們加、減、乘、除、平方等等。目前,可以從 這裡 獲取更多資訊。

二進位制補碼

[編輯 | 編輯原始碼]

雖然在二進位制中加兩個數字可能很簡單,但即使是最小的值的減法也要複雜得多。幸運的是,二進位制補碼規則允許將二進位制減法轉換為加法運算,加法運算更容易執行。

以以下例子為例

 ...8421 (value in decimal system)
 ...1001 (9 in binary)

(9-15 in binary)
    1001
    1111 -
    -------
    
    To perform two's complement first you must find the one's complement which
    is simply the NAND (Negated AND) of the number that is being subtracted.
    
    To find the two's complement simply perform binary addition of 1 to the 
    one's complement.
    
                    NAND    1111
    one's complement:       0000
    
                            0000
                                1+
                            -------
     two's complement:      0001
     
(adding the first number and the two's complement)
                            1001
                            0001 +
                            -------
                            1010    
   
    The remainder should be -6, however here we have 10 in decimal, 
    the reason is because the number is a signed value. Here it should represent -6. 
    To confirm the value we have is correct we must perform an additional two's 
    complement on the result.
                            
                            1010
                            0101 
                               1 +
                            -------
                            0110 (6 in decimal)
    This proves that we have -6 as the result of the subtraction.

有符號數和無符號數

[編輯 | 編輯原始碼]

條件碼

[編輯 | 編輯原始碼]

條件碼暫存器是條件碼暫存器 (CCR) 中包含的特殊標誌(位),用於記錄有關條件碼 (<opcode>cc) 指令的資訊,以便程式可以做出分支決策。如上所述,數字是有符號還是無符號的上下文會在它用於計算時隱含非常不同的結果。因此,SPARC 使用不同的 CCR 集來管理有符號和無符號資料。


有符號條件碼

[編輯 | 編輯原始碼]

對於有符號數,SPARC 使用三個條件碼中的三個 - Z、N 和 V 位 - 來調節條件分支


  • Z:此標誌跟蹤條件碼指令的計算結果是否為零。如果結果為零,則將其設定為 1,否則將其設定為 0。例如,
mov 4, %l1         !move 4 into %l1
subcc %l1, 4, %g0  !subtract 4 from %l1
會將 Z 暫存器設定為 1,但
 
mov 4, %l1         !move 4 into %l1
subcc %l1, 3, %g0  !subtract 3 from %l1
會將其設定為零。


  • N:此標誌跟蹤有符號條件碼指令的計算結果是否為負。如果結果為負,則將其設定為 1,否則將其設定為 0。例如,
mov 4, %l1         !move 4 into %l1
subcc %l1, 5, %g0  !subtract 5 from %l1
會將 N 暫存器設定為 1,但
 
mov 4, %l1         !move 4 into %l1
subcc %l1, 3, %g0  !subtract 3 from %l1
會將其設定為零。


  • V:此標誌跟蹤有符號計算的結果是否太大而無法由 32 位(或可能 64 位)暫存器儲存。如果結果太大,則將其設定為 1,否則將其設定為 0。

無符號條件碼

[編輯 | 編輯原始碼]

分支指令

[編輯 | 編輯原始碼]
華夏公益教科書