RISC-V basic instructions lw and sw (including usage instructions and examples)

The LW and SW instructions are instructions for loading and storing words. A word is 32 bits (4 bytes) of data. The LW instruction reads a word from memory and stores it in a register. The SW instruction writes a word from a register into memory. Both instructions require a base register (rs1) and an offset (offset) to calculate the memory address. The format of the LW and SW instructions is as follows:

  • LW rd, offset(rs1): Read a word from the memory address x[rs1] + sext(offset) and store it in the rd register, where x[rs1] is the value in the rs1 register and sext(offset) is the symbol Extended 12-bit immediate number.
  • SW rs2, offset(rs1): Write the word in the rs2 register to the memory address x[rs1] + sext(offset), where x[rs1] is the value in the rs1 register, and sext(offset) is sign extended 12-bit immediate number.

Both LW and SW instructions belong to the I-Format type of instructions, and their formats are as follows:

type opcode rd funct3 rs1 imm
I-Format 0100011 5 people 010 5 people 12 bits
  • opcode: This is the opcode of the instruction, used to indicate the type and function of the instruction. The opcodes of LW and SW instructions are both 0100011, occupying 6 bits.
  • rs1: This is the first source register of the instruction and is used to store the base address. Both the LW and SW instructions require a base register to calculate the memory address. rs1 occupies 5 bits and can represent any one of the 32 registers.
  • rs2(rd): This is the second source register of the instruction and is used to store the data to be stored. The SW instruction requires a data register to write to memory. rs2 occupies 5 bits and can represent any one of the 32 registers.
  • funct3: This is the function code of the instruction, used to distinguish different operations. The funct3 of LW and SW instructions are both 010, occupying 3 bits.
  • imm: This is the immediate value of the instruction, used to represent the offset. Both the LW and SW instructions require an offset to calculate the memory address. imm occupies 12 bits and can represent any integer between -2048 and 2047.

For example, assume the following directive:

  • LW x5, 0(x2)
  • SW x5, 4(x2)

Their 32-bit representation is as follows:

  • LW x5, 0(x2):0000 0000 0000 0010 010 00101 0100011
  • SW x5, 4(x2):0000 0000 0000 0100 00101 010 00101 0100011

It can be seen that their opcode, funct3, rs1 and imm all have different values, and rs2 is only meaningful in the SW instruction.

For example, assuming the value in the x2 register is 0x1000, the value at memory address 0x1000 is 0x12345678, and the value at memory address 0x1004 is 0x87654321, then the effect of the following instruction is as follows:

  • LW x5, 0(x2): Read the value 0x12345678 at memory address 0x1000 into the x5 register.
  • LW x6, 4(x2): Read the value 0x87654321 at memory address 0x1004 into the x6 register.
  • SW x5, 4(x2): Write the value 0x12345678 in the x5 register to the memory address 0x1004, overwriting the original value 0x87654321.
  • SW x6, 0(x2): Write the value 0x87654321 in the x6 register to the memory address 0x1000, overwriting the original value 0x12345678.

There are many ways to use the LW and SW instructions, the main ones are as follows:

  • For access to array elements: If the array element is a word, the LW and SW instructions can be used to read or modify the array element. For example, assuming that array a is stored continuously in memory starting from address 0x2000, and each element occupies 4 bytes, then the following instructions can implement the function of a[i] = a[i+1]:

    • LW x5, 4(x6): Read a[i+1] into the x5 register, where the x6 register is the result of i multiplied by 4.
    • SW x5, 0(x6): Write a[i+1] to a[i].
  • For access to structure members: If the structure member is a word, you can use the LW and SW instructions to read or modify the structure members. For example, assuming that the structure student has two members, name and age, each occupying 4 bytes, and the structure variable s is stored continuously in memory starting from address 0x3000, then the following instructions can achieve s.age = s.age + 1 Functions:

    • LW x5, 4(x7): Read s.age into the x5 register, where the x7 register is the starting address 0x3000 of s.
    • ADDI x5, x5, 1: Increase s.age by one.
    • SW x5, 4(x7): Write s.age back to memory.
  • Used for the transfer of function parameters and return values: If the function parameters or return value are a word, then the LW and SW instructions can be used to transfer them. For example, suppose there is a function f(int x) whose function is to return the square of x. Then the following instruction can implement the function of calling f(10) and storing the result in the x5 register:

    • LI x10, 10: Load 10 into the x10 register as a function parameter.
    • SW x10, -4(x2): Save the value in the x10 register to memory for stack operations during function calls.
    • JAL x1, f: Jump to function f and save the return address in the x1 register.
    • LW x5, 0(x2): Read the function return value from memory into the x5 register.

Guess you like

Origin blog.csdn.net/qq_52505851/article/details/132086460