跳轉到內容

360 彙編/分支指令

來自華夏公益教科書

360 系列大型機計算機的分支指令分為兩種型別:提供返回地址的分支指令(如子程式呼叫)和不提供返回地址的單向分支指令。

所有分支指令都採用 3 種形式:完全不分支,也稱為無操作或 NO-OP,條件分支和無條件分支。

在下面的示例中,假設 R0、R1、R14、R15 是分別等於這些示例中等效暫存器(0、1、14 和 15)的標籤,以提供交叉引用列表。示例在沒有 **R** 字首的情況下也能正常執行,但程式設計師通常使用等於每個數字的標籤,因為對暫存器的引用不會出現在交叉引用列表中,而對符號的引用則會。

提供返回地址的分支

[編輯 | 編輯原始碼]

以下指令提供指向另一個地址的分支,其中提供一個暫存器來儲存分支後一條指令的地址,以提供返回呼叫點的機制。

以下指令適用於 360 系列的所有型號:360、370、390、390/ESA 和 z/System。
* BAL - 分支並連結 * BALR - 分支並連結暫存器
以下指令僅在 370 及更高版本上可用:370、390、390/ESA 和 z/System。
* BAS - 分支並儲存 * BASR - 分支並儲存暫存器

無返回分支

[編輯 | 編輯原始碼]

以下指令適用於 360 系列的所有型號,包括 360、370、390 和 z/System。

* BC - 根據條件分支[1] * BCR - 根據條件暫存器分支[1] * BCT - 根據計數分支 * BCTR - 根據計數暫存器分支

[1] 擴充套件助記符提供條件測試,包括 BR 和 B 用於無條件分支;BNO、BE、BNE、BH、BL 和 BM 等

這些指令的一般使用方法解釋如下。

無分支

[編輯 | 編輯原始碼]

某些型別的分支指令被視為無分支或無操作(NO-OP)。通常它們涉及使用暫存器 0 或掩碼值為 0。這些是

  • 目標暫存器為暫存器 0 的暫存器分支
  • 暫存器到儲存器分支(無論掩碼如何),其中索引暫存器為 0
  • 掩碼為 0 的暫存器到儲存器分支
Label1    BR    R0            No branch
          BC    0,Label1      Branch not taken; mask is 0
          BALR  R14,R0        Branch not taken; this is used at the start of a 
*                             CSECT/START module to load the left register with the 
*                             address of the next instruction to establish addressing
          BC   15,100(R1,R0)  Because R0 is the Index Register, the branch is not taken
*                             even though the mask indicates an unconditional branch
          BCT  R1,16(R0)      Subtract 1 from the contents of R1, but since the base 
*                             register of the branch address is 0, no branch will occur
          BCTR R1,R0          Subtract 1 from the contents of R1, but since the target
*                             branch register is 0, do not branch

無條件分支

[編輯 | 編輯原始碼]

無條件分支指令會導致程式位置計數器 (PSW) 設定為暫存器中指定的地址,或暫存器加上 12 位偏移量,或暫存器和偏移量加上額外的“索引”暫存器的值。

如果 BR 指令使用暫存器 0,則分支不會發生,並且被視為無操作。如果 BALR 指令中的右暫存器為 0,則分支不會發生,並且被視為將下一條指令的地址載入到左暫存器中的指令。如果索引暫存器為 0,則不會發生任何條件分支 - 包括無條件分支。

這些指令可能屬於以下型別之一:-

  • 暫存器到暫存器 (RR)
Example1  BR    R15              Branch to the location whose address is in Register 15
Example2  BR    R0               No Branch - Acts like no-op
Example3  BALR  R14,R15          Branch to the location whose address is in Register 15, put
*                                return address in R14
Example4  BALR  R12,R0           Load Register 12 with the address of the next instruction, 
*                                but do not branch
  • 儲存器 (RS)
Example1  B     4(R15)           Branch to the location in R15 plus the (12 bit)
*                                    decimal displacement of 4
Example2  B     X'010'(R15)      Branch to the location in R15 plus the (12 bit) hex
*                                displacement of decimal 16
Example3   B     LABEL1           Branch to the location with the specified address (Base & 
*                                displacement set by the Assembler)
Example4   BAL   R14,X'010'(R15)  Branch to location in (R15 plus displacement), put 
*                                return address in R14.
LABEL1   EQU   *               A location (within the range of the base register
*                              for the program) 
*          B     106(R0)          Do not branch; treat as NO-OP
  • 索引 (RX)
Example1   B     4(R15,R1)        Branch to location whose address is calculated from R15
*                                 plus 4 plus R1
Example2   B     10(R12,R0)       No branch because index register is 0; treated as NO-OP
Example3   B     10(R0,R12)       This is standard, and will branch to the address at the 
*                                 location in Register 12 + a displacement of 10

條件分支

[編輯 | 編輯原始碼]

條件分支指令會導致 PSW 中的位置計數器設定到暫存器中指定的地址,或暫存器加上 12 位偏移量,前提是滿足條件(並且暫存器不為 0)。有兩種型別,掩碼條件和索引條件。

條件分支可能屬於以下型別之一:-

  • 掩碼條件,儲存器 (RS)
Example   BE     4(R15)          Branch to the location in (R15 plus 4), if previous 
*                                comparison gave "Equal" condition (8)
Example   BE     12(R0)          As specified earlier, branch on R0 is treated as a NO-OP
*                                and does not branch
Example   BC     8,4(R15)        Branch to the location in (R15 plus 4), if previous 
*                                comparison gave "Equal" condition (8)
*                                (same as above but specifying actual condition code 
*                                 value = 8)
Example   BC     7,X'010'(R15)   Branch to the location in (R15 plus X'010') if previous
*                                comparison gave "Unequal" (Branch Not Equal) condition (7)
  • 索引條件,儲存器 (RX)
Example   BCT   R1,4(R15)        Reduce value in R1 by 1 and, if it is not zero, branch
*                                to location in (R15 plus 4 )
Example   BCT   R1,12(R0)        The register is reduced by 1, but the branch will never occur
  • 索引條件,暫存器 (RR)
Example   BCTR   R1,R15          Reduce value in R1 by 1 and, if it is not zero, branch
*                                to address in R15
Example   BCTR   R1,R0           Reduce R1 by 1 but do not branch.  This instruction is often
*                                used to subtract 1 from a register

分支表(技術)

[編輯 | 編輯原始碼]

分支表實際上是一組連續的無條件分支指令,它們長度相同(通常為 4 位元組),用於使用索引非常有效地直接分支到這組指令中的一個。該索引通常從一些源輸入值生成,該值本身可能是非順序的,如下面的示例所示。該方法比使用二分搜尋或順序表查詢快得多。查詢涉及比較指令和隨後的條件分支。在示例中只使用了 5 條指令(其中 2 條是條件分支),它們執行以下操作:-

  • 1. 驗證輸入(將除 A、S、M、D 以外的任何輸入值轉換為空索引值)
  • 2. 使用索引轉換輸入值(使用插入字元 (IC) 指令)轉換為 0、4、8、12 或 16(索引)。該技術的早期版本使用了 (TR) 和 (IC) 指令,速度比使用兩個 (IC) 指令慢 4 到 5 倍。
  • 3. 使用索引無條件地分支到適當的無條件分支指令 - 零值會導致分支到錯誤例程。(無條件分支不依賴於之前的字元或數字比較指令)
示例 考慮一個範圍為 A-Z 的單個位元組字元的輸入變數,其中特定值(如 A、S、M、D)決定程式中的處理邏輯。在本例中,A=加法、S=減法、M=乘法和 D=除法。

在以下兩個示例中,執行有效性並轉到適當標籤的時間是固定的,與不同有效單位元組輸入字元的數量無關。

使用分支指令表的示例

[編輯 | 編輯原始碼]
             SR    R15,R15             Clear index register to zero  (32 bits)
             IC    R15,INPUT           Insert input byte into low order bits of R15
*                                      (bits 24-31 forming X'000000C1' for "A")
             IC    R15,TABLE2(R15)     Use R15 as Index to extract a 0,4,8,12 or 16 from 
*                                       the 2nd Table
             B     TABLE1(R15)         Branch using the index now in R15
TABLE1       EQU   *                ---Start of Branch table--- (each branch instruction
*                                       is 4 bytes long and unconditional)
             B     ERROR               00 = Invalid input value         (that is, any byte 
*                                                                        not = A,S,M or D)
             B     ADD                 04 = Input value was "A"
             B     SUBTRACT            08 = Input value was "S"
             B     MULTIPLY            12 = Input value was "M"
             B     DIVIDE              16 = Input value was "D"
*                                   ---End of Branch table
ERROR        EQU   *
* print or display error message or similar
ADD          EQU   *
* perform addition and continue with rest of program
             B    NEXT
SUBTRACT     EQU   * 
* etc. 
INPUT        DS   C                     The input character is in this byte.
TABLE2       DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00)    X'00'-X'0F'
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00)    X'10'...
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) 
             DC   Al1(00,04,00,00,16,00,00,00,00,00,00,00,00,12,00,00)
*                        x'C0' - X'CF' (04 is at offset X'C1')
             DC   Al1(00,00,00,08,00,00,00,00,00,00)00,00,00,00,00,00)    x'D0' - X'DF' 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00)
* the above table can be automated if the Assembler is allowed to calculate & place the
* index values but it is not shown here for simplicity. If validation is not required, the
* size of this translate table need only be the size of the range of all possible input
* values - in this case A through to M (18 bytes).

使用 2 位元組偏移量的示例

[編輯 | 編輯原始碼]

可以使用另一種與上述分支表非常類似的技術。彙編程式可以構建絕對或相對地址(偏移量)表,而不是分支指令表。這隻需要一個額外的指令,但將分支範圍擴充套件到 64K,而無需額外的基址暫存器覆蓋,並將表的大小減少一半。

             SR    R15,R15             Clear index register to zero  (32 bits)
             IC    R15,INPUT           Insert input byte into low order bits of R15 
*                                      (bits 24-31 - forming X'000000C1' for "A")
             IC    R15,TABLE2(R15)     Use R15 as Index to extract a 0,2,4,6 or 8 from
*                                      the 2nd Table
             LH    R15,TABLE1(R15)     extract two byte offset into low order bits of R15
             B     TABLE1(R15)         Branch using the table address plus offset now in R15
TABLE1       DS    0H               ---Start of Offset table--- (each is 2 bytes long) 
             DC    Al2(ERROR-TABLE1)               00 = Invalid input value         
             DC    AL2(ADD-TABLE1)                 02 = Input value was "A"
             DC    AL2(SUBTRACT-TABLE1)            04 = Input value was "S"
             DC    AL2(MULTIPLY-TABLE1)            06 = Input value was "M"
             DC    AL2(DIVIDE-TABLE1)              08 = Input value was "D"
*                                   ---End of Branch table
ERROR        EQU   *
* print or display error message or similar
ADD          EQU   *
* perform addition and continue with rest of program
             B    NEXT
SUBTRACT     EQU   * 
* etc. 
INPUT        DS   C                    The input character is in this byte.
TABLE2       DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00)    X'00'-X'0F'
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00)    X'10'... 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,02,00,00,08,00,00,00,00,00,00)00,00,06,00,00)
*                  x'C0' - X'CF' (02 is at offset X'c1')
             DC   Al1(00,00,00,04,00,00,00,00,00,00)00,00,00,00,00,00)    x'D0' - X'DF' 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00)
* An alternative approach here is to have the two-byte branch offsets within TABLE2 
* (it then also uses one less instruction and eliminates the need for TABLE1 but this
* may require twice the size of TABLE2 (depending upon the need for validation, and also on
* the range of the input character).

另一種定義 TABLE2 的方法,讓彙編程式自動放置索引位元組,如下所示(為簡潔起見,只顯示了“A”和“D”的示例)。

TABLE2       DC    256AL1(0)                   define 256 bytes of nulls
             ORG   TABLE2+C'A'                 repeat these two lines for each
*                                              valid input character
             DC    AL1(02)                     (The 'ORG' Assembler statement above
*                                              resets to correct position)
             ORG   TABLE2+C'D'                          
             DC    AL1(08)
             ORG                               At end, reset to earlier position after
*                                              end of 256 byte table

使用 4 位元組絕對地址的示例

[編輯 | 編輯原始碼]

彙編程式可以構建一個絕對地址表。這隻需要一個額外的指令,但將分支範圍擴充套件到 2 GB(即 31 位處理器的整個地址空間)。

             SR    R15,R15             Clear index register to zero  (32 bits)
             IC    R15,INPUT           Insert input byte into low order bits of R15 
*                                      (bits 24-31 - forming X'000000C1' for EBCDIC "A")
             IC    R15,TABLE2(R15)     Use R15 as Index to extract a 0,4,8,12 or 16
*                                      from the 2nd Table
             ICM   R15,15,TABLE1(R15)  extract absolute address into all 32 bits of R15 from the 2nd Table             
* The previous ICM instruction above could also be accomplished with
*            L     R15,TABLE1(R15)
             BR    R15                 Branch using the address in R15
TABLE1       DS    0H                ---Start of Offset table--- (each is 2 bytes long) 
             DC    A(ERROR)                        00 = Invalid input value         
             DC    A(ADD)                          04 = Input value was "A"
             DC    A(SUBTRACT)                     08 = Input value was "S"
             DC    A(MULTIPLY)                     12 = Input value was "M"
             DC    A(DIVIDE)                       16 = Input value was "D"
*                                   ---End of Branch table
ERROR        EQU   *                               Label for Errors - could be in a 
*                                                  different CSECT (R15=Entry point address)
*
* print or display error message or similar
ADD          EQU   *                               Label for 'Add'  - could be in a
*                                                  different CSECT through either 
*                                                  an EXTRN ADD statement, or changing 
*                                                  the A(ADD) to V(ADD) (or whatever name)
*
* perform addition and continue with rest of program
             B    NEXT
SUBTRACT     EQU   *                               Label for 'Subtract' - could be in different
*                                                  CSECT etc. 
INPUT        DS   C                    The input character is in this byte.
TABLE2       DC   Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00)    X'00'-X'0F'
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00)    X'10'... 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,04,00,00,16,00,00,00,00,00,00)00,00,12,00,00)
*                      x'C0' - X'CF' (04 is at offset X'c1')
             DC   Al1(00,00,00,08,00,00,00,00,00,00)00,00,00,00,00,00)    x'D0' - X'DF' 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) 
             DC   Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00)

370 和 zSystem 指令

[編輯 | 編輯原始碼]

以下分支指令在 370 和 zSeries 機器中可用——待補充--

360 彙編指令
分支資料傳輸控制流算術邏輯移位和旋轉其他

另請參見

[編輯 | 編輯原始碼]
  • [1] 維基百科索引 (資訊科技)
[編輯 | 編輯原始碼]
  • [2] System/360 指令計時資訊


360 組合語言
360 系列 介紹 · 基本常見問題解答 · 360 系列 · 360 架構
360 指令集 360 指令 · 分支指令 · 資料傳輸指令 · 控制流指令 · 算術指令 · 邏輯指令 · 移位和旋轉指令 · 特權指令 · 其他指令
語法和彙編器 360 彙編器· 偽指令
指令擴充套件 浮點數 · 高階語言
華夏公益教科書