ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • RISC-V Fields
    Computer Science/Computer Architecture and Organization 2021. 4. 25. 13:26

    Field 

    https://slideplayer.com/slide/15513698/

    RISC-V Instruction Fields들에는 다음과 같은 이름이 붙어있다.

    • opcode (0 - 6, 7bits): basic operation of the instruction
    • rd (7 - 11, 5 bits): register destination operand. Gets result of the operation.
    • funct3 (12 - 14, 3 bits): Additional opcode field
    • rs1 (15 - 19, 5 bits): first register source operand
    • rs2 (20 - 24, 5 bits): second register source operand
    • funct7 (25 - 31, 7 bits): Additional opcode field

    앞글에서 예시문제를 풀때 보여줬듯, opcde, funct3, funct7은 연산과 관련된 정보가 저장되고, rd에는 결과값이, rs1과 rs2에는 연산에 쓰여질 operands가 저장되어있는 레지스터의 정보가 저장되어있다.

     

    하지만, load doubleword명령어같은 경우, 5bit에 저장하기에 ($2^5$만 사용할 수 있기때문에 ) 주소로 쓰기에 충분하지 않을 수 있다. 그래서 RISC-V 설계자들은 절충안을 택했다. 기존에 보여주었던 instruction은 R-type, 그리고 또다른 형식을 I-type으로 부르며, addi등의 명령을 수행할 때 사용하도록 했으며, 그 이외에도 다양한 포맷이 있다.

     

    https://www.eetimes.eu/creating-a-custom-processor-with-risc-v/

    위의 표에서 보여주듯, I-type에서, immediate는 12 bits가 저장되고, 이전 포스트에서 다루었던 two's complement로 표현되기때문에 $-2^{11} 에서 2^11 - 1$까지의 integer를 표현할 수 있다. load doubleword(ld)명령어를 예를들면, 레지스터 rd에 있는 베이스 주소에서부터, $\pm2^{11} bits = \pm2048 bytes$안에 있는 더블워드를 지정할 수 있다.

    immediate rs1 funct3 rd opcode
    12 bits 5 bits 3 bits 5 bits 7 bits

     

    예시를 들어보자 (이전 포스트에서 풀이했던 문제 참고)

    ld x9, 64(x22) // Temporary register x9 gets A[8]

    여기서 rs1 필드에는 22(레지스터 x22), rd 필드에는 9(register x9)가, imm(immediate)필드에는 64가 들어간다.

     

    비슷한 예시로, store doubleword(sd)를 위해서는, S-Type이 필요한데, 12비트의 수치값의 길이는 같다. 하지만 7bits, 5bits로 나뉘어져있는데, 이는 RISC-V 설계자들이, 다른 Type과 같이 rs1과 rs2의 위치를 전체 비트상에서 같은 위치에 놓기위해서 이러한 방식으로 설계했다. 명령어 형식을 비슷하게 유지해서 하드웨어가 복잡해지는것을 막기위함이었다. 

    immediate[11:5] rs2 rs1 funct3 immediate[4:0] opcode
    7 bits 5 bits 5 bits 3 bits 5 bits 7 bits

     

     

    RISC-V architecture revealed through

    그렇다면, 이 모든 정보를 조합해서 C문장을 RISC-V machine language로 변환해보자.

    Translating RISC-V Assembly Language into Machine Language


    We can now take an example all the way from what the programmer writes to what the computer executes. If x10 has the base of the array A and x21 corresponds to h, the assignment statement

    A[30] = h + A[30] + 1;

    is compiled into

    ld x9, 240(x10) // Temporary reg x9 gets A[30]
    add x9, x21, x9 // Temporary reg x9 gets h+A[30]
    addi x9, x9, 1 // Temporary reg x9 gets h+A[30]+1
    sd x9, 240(x10) // Stores h+A[30]+1 back into A[30]

    What is the RISC-V machine language code for these three instructions?

    먼저, 십진수로 표현해보자, ld, add, addi, sd의 차례로 아래와 같이 나타낼 수 있다.


    ld x9, 240(x10)

    immediate rs1 funct3 rd opcode
    240 10 3 9 3

     

    add x9, x21, x9

    funct7 rs2 rs1 funct3 rd opcode
    0 9 21 0 9 51

     

    addi x9, x9, 1

    immediate rs1 funct3 rd opcode
    1 9 0 9 19

     

    sd x9, 240(x10)

    immediate[11:5] rs2 rs1 funct3 immediate[4:0] opcode
    7 9 10 3 16 35

    십진수로 표현한 이 값을, 다시 binary로 표현해보자


    ld x9, 240(x10)

    immediate rs1 funct3 rd opcode
    0000 1111 0000 0 1010 011 0 1001 000 0011

     

    add x9, x21, x9

    funct7 rs2 rs1 funct3 rd opcode
    000 0000 0 1001 1 0101 000 0 1001 011 0011

     

    addi x9, x9, 1

    immediate rs1 funct3 rd opcode
    0000 0000 0001 0 1001 0 0 1001 001 0011

     

    sd x9, 240(x10)

    immediate[11:5] rs2 rs1 funct3 immediate[4:0] opcode
    000 0111 0 1001 0 1010 011 1 0000 010 0011

     

     

     

    Reference

    David A. Patterson and John L. Hennessy. 2013. Computer Organization and Design, Fifth Edition: The Hardware/Software Interface (5th. ed.). Morgan Kaufmann Publishers Inc., San Francisco, CA, USA.

     

    댓글

Designed by Tistory.