1. 程式人生 > >MIPS 常用暫存器

MIPS 常用暫存器

【1】MIPS中通用的32個處理器及功能如下:

          編號                                                            名稱                                                           功能

          0                                                                  zero                                                           恆為0的暫存器,一般用來初始化別的暫存器

          1                                                                  at                                                                被彙編器所保留,用來處理超過32位的long long型的資料

          2-3                                                              v0-v1                                                         用來存放函式的返回值

          4-7                                                              a0-a3                                                        存放函式的入參

          8-15                                                            t0-t7                                                          存放函式中需要保留的臨時變數

          16-23                                                         s0-s7                                                         存放需要保留的呼叫棧的資訊,當呼叫子函式的時候,存放需要保留的上下文的資訊

          24-25                                                         t8-t9                                                           額外供保留臨時變數的暫存器

          26-27                                                         k0-k1                                                         給作業系統核心使用的

          28                                                               gp                                                              存放全域性指標

          29                                                               sp                                                               存放棧指標

          30                                                               fp                                                                存放指向棧幀的指標

          31                                                               ra                                                                存放還數返回地址的的暫存器,一般bal和j指令後面的地址將會被保留到ra暫存器內


【2】mips彙編中常用的指令:

          (1)mips指令中欄位的命名:

                    op(6bit):指令的基本操作  稱為操作碼

                    rs(5bit):第一源運算元暫存器

                    rt(5bit):第二源運算元暫存器

                    rd(5bit):存放結果的目的暫存器

                    shamt(5bit):偏移量

                    funct(6bit):功能碼

         (2)mips指令型別:

                   R型: op  rs  rt rd funct         

                   I型: op rs  rt immediate(12bit)

                   J型:op  immediate(16bit)


                  R型指令:

                  add(求和)                                       add     $1,  $2,  $3                           暫存器定址方式 $1=$2+$3 暫存器2和暫存器3的值加起來賦值給暫存器1

                  sub(求差)                                       sub      $1,  $2,  $3                           $1=$2-$3

                  and(與)                                           and     $1,  $2,  $3                           $1=$2&$3

                  or(或)                                              or        $1,  $2,  $3                          $1=$2|$3

                  nor(異或)                                        nor      $1,  $2,  $3                          

                  slt(三目運算)                                  slt        $1,  $2,  $3                          if($2<$3) $1=1 else $1=0

                  sltu(無符號三目運算)                    sltu     $1,  $2,  $3                          三個暫存器內的數都是無符號的

                  sll(迴圈左移)                                  sll        $1,  $2,  10                         $1=$2<<10

                  srl(迴圈右移)                                  srl        $1,  $2,  10                         $1=$2>>10

                  sra(保留符號位的迴圈左移)        sra       $1,  $2,  10                         $1=$2<<10(符號位保留)

                  sllv                                                  sllv       $1,  $2,  $3                         $1=$2<<$3

                  srlv                                                  srlv      $1,  $2,  $3                         $1=$2>>$3


                  Ih                                                    Ih         $1,  10($2)                          基址定址

                  Sh                                                  Sh         $1,  10($2)                        基址定址       

                  lb                                                    Ib         $1,  10($2)                         基址定址

                  ll                                                      Il         $1,  10($2)                          基址定址

                  sc                                                    sc         $1,  10($2)                        基址定址  

                 以上五個指令的具體實現還不是很清楚,歡迎各位大神給出指點!


                div/divu                                           div          des,  src1,  src2                des=src1/src2(表示無符號的數)

                abs                                                  abs          des,  src                            des=|src|src的絕對值給des

                mul                                                  mul          des,  src1,  src2               des=src1*src2

                seq/seq(u)                                     seq          des,  src1,  src2               if(src1==src2) des=1 else des=0

                sgt                                                   sgt            des,  src1,  src2              if(src1>src2)    des=1 else des=0

                sle                                                   sle            des,  src1,src2                if(src1<=src2)  des=1 else des=0

                slt                                                    slt              des,  src1,src2                if(src1<src2)    des=1 else des=0

    

              I型指令:

               addi(求和)                                       addi         $1,  $2,  10                     $1=$2+10

               subi(求差)                                       subi          $1,  $2,  10                     $1=$2-10

               andi(與)                                           addi         $1,  $2,  10                     $1=$2&10

               ori(或)                                               ori             $1,  $2,  10                     $1=$2|10

               lui                                                      lui             $1,  100                           將100載入到$1暫存器的高16bit

               lw                                                      lw              $1,  10($2)                      載入$2暫存器開始偏移10位元組的地址的內容到$1暫存器

               sw                                                     sw             $1,  10($2)                      儲存$1的內容到$2暫存器開始偏移10位元組的地址上

               beq                                                   beq           $1,  $2,  10                      if($1==$2) goto 10(lable 10 的位置)

               bne                                                   bne           $1,  $2,  10                      if($1!=$2) goto 10

               slti                                                     slti             $1,  $2.,  10                     if($2<10) $1=1 else $1=0

               sltiu                                                   sltiu          $1,  $2,  10                       10為無符號的數

              


               J型指令:

                j                                                         j    lable                                              跳轉到lable處

                jar                                                     j     $1                                                  跳轉到$1暫存器儲存的地址處

                jal                                                     jal  lable1                                           跳轉到lable1標籤處


               合成指令:

                b                          跳轉

               

               b和 下面的其中一個或者下面一個以及z(zero)所構成的指令均為跳轉指令:例如beq/beqz

                eq                        相等

                ne                        不等

               ge                          大於等於

               gt                            大於

               le                            小於等於

               lt                             小於

 

              偽指令:

              .globl                                   __start                                                設定全域性標籤

              lable:                                                                                               設定區域性標籤lable

             .ascii s                                                                                              符合ascii碼組成的字串
             .asciiz s                                                                                            like .ascii, null-terminated
             .word w1, w2                                                                                  一個字長(32bit)的資料w1 w2
             .half h1, h2                                                                                      半位元組長(16bit)的資料
             .byte   b1,b2                                                                                     一個位元組的資料
             .float f1, f2                                                                                        32-bit single precision floating point numbers f1, f2, . . .
             .double d1, d2                                                                                 64-bit double precision floating point numbers d1, d2, . . .
             .space n                                                                                            n zero bytes

             .set push                                                                                           儲存所有設定

             .set pop                                                                                             重新儲存設定

             .set reorde                                                                                        允許編譯器優化指令,對程式碼進行優化排序

             .set noreorde                                                                                   不允許編譯器優化指令,對程式碼進行優化排序

             .set at/noat                                                                                        使用/不是用at暫存器

             .set mips 0                                                                                        支援原來的ISA級別(小心使用)

             .set mips 3                                                                                        下面的指令是mips64指令,相容mips32的指令

             .set volatile/novolatile                                                                     處在volatile區內的所有存取指令都不會被移動位置

             .align  7                                                                                             .align區內的程式碼必須使用2的7次方位元組大小的記憶體空間

  【3】MIPS架構特點

            (1)獨立的指令快取和資料快取

            (2)每個指令的執行都分為五個階段及五條流水線

            (3)所有指令都一樣長32bit這就是為什麼mips指令的二進位制檔案要比同樣x86的二進位制檔案大的原因

            (4)無條件碼

            (5)三運算元指令(算數/邏輯操作不想要指明記憶體地址)   

            (6)沒有位元組或者半位元組運算

            (7)沒有專門的棧支援

            (8)最小子程式支援,跳轉指令的限制(b上下64k內,J上下64M內)

【4】MIPS指令的五條流水線

          IF:取指,從指令快取I-cache中取出指令

          RD:讀暫存器,從指令中源暫存器或者cpu中的某一個暫存器中讀取內容

          ALU:算數邏輯單元,在一個時鐘週期內完成演算法或邏輯操作

          MEM:指令從資料快取中讀寫記憶體變數

          WB:寫回,將操作完成的值寫回到暫存器中

---------------------
原文:https://blog.csdn.net/lq19880521/article/details/78631641