RSIC-V instruction and introduction (1)

Comment symbol: #

add rd, rs1, rs2 # rd = rs1 + rs2 for register operations

add rd, rs1, x0 # rd = rs1 + x0 x0 means zero register read-only means 0 This statement is to assign rs1 to rd    

sub rd, rs1, rs2 # rd = rs1 - rs2 for register subtraction

addi rd, rs1, 10 # rd = rs1 + 10 Add operation for register and immediate value

addi rd, rs1, -10 # rd = rs1 - 10 subtraction also uses addi

There are two ways to store memory addresses: big endian (big endian): the low byte is stored on the high byte address
and little endian (little endian): the low byte is stored on the low byte.
risc-v uses the little endian

lw rd, 12(rs) #lw stands for load word, load rs as the base address register plus the value of offset 12 to rd
load process is memory to register

sw rs, 40(rd) #sw stands for store word Store the value of the rs register in the memory address of rd plus offset 40 The
process of sw is register to memory

lb #load byte Sign-extend the 1-byte data in the byte address and load it into the corresponding register.
For example, the extension of 1000 0000 is to extend the highest bit 1 as the sign bit to form 0xffffff80

lbu #load byte unsigned is to fill all the filling with 0

sb #store byte Save the 1-byte data of the lowest byte in the register to the corresponding byte address

Jump instruction:

beq rs1, rs2, L1 #branch if equal If rs1 is equal to rs2, then jump to L1

bne #branch if not equal is not equal to jump

blt #branch if less than less than jump

bltu #branch if less than unsigned less than jump to treat the register as an unsigned number 

bge #banch if greater or equal is greater than or equal to jump

j L1 #jump L1 jump directly to L1

Logical operation instructions:

and rd, rs1, rs2        #rd = rs1 & rs2

andi rd, rs1, 3        #rd = rs1 & 3

sll #shift left logical logical left shift right 0 padding

slli rd, rs1, 2 #shift left logical immediate The logical immediate value is shifted to the left, and the rs1 logic is shifted to the left by 2 bits, and the right is filled with 0

sra #shift right arithmetic Arithmetic right shift instruction means to fill the left side with the highest bit of the original number after shifting

srai #immediate arithmetic right shift, the left side is filled with the highest bit of the original number after the shift

srl #logic right shift and fill the left side with 0

srli # Immediate logical right shift and fill the left side with 0

or #logical or

ori #immediate logical or

xor # logical exclusive or

xori #immediate logic exclusive or 

There are also some pseudo-instructions in risc-v such as mv rd, rs = addi rd, rs, 0
li rd, 13 = addi rd, x0, 13
They are used to simplify the syntax of writing

The instruction segment and data segment of the executable file obtained by compiling are respectively stored in the instruction space and data space in the memory. The
PC register stores the address of the next instruction to be executed. The PC value is continuously updated to allow the processor to execute continuously
. In the case of sequential execution, the PC value is increased by 4, but when a branch instruction is encountered, it needs to be updated to the jump position

There are 6 basic steps to call a function:

1. When a function call occurs, before executing the function function, first save the parameters used in this call for easy access

2. Hand over control to the function function called

3. Apply for a certain amount of local storage space for the function according to the situation to meet the storage requirements required during the execution of the function

4. Execute the operation of the function

5. After the execution of the function is completed, store the obtained result data for the main process to obtain, and at the same time restore the register value used during the execution of the function, and release the local storage space allocated to the function

6. Transfer control to the original process

 

Zero represents the x0 register.
Usually a0 - a7 (x10 - x17) registers are used to pass parameters to the called function. A0 and a1 registers are often used to pass the return value
ra, that is, the x1 register, which is used to save the return address value 
s0 - The registers numbered x8 - x9 and x18 - x27 corresponding to s11 are used as saving registers to save key data in the original process to avoid being damaged during the function call

jr #Function calls may occur in multiple positions in the program segment, and the return address of each call function is different and will change at any time. It is
necessary to record the return address before the function call and save it in the ra register. When returning, use the jr instruction to return to the ra register. The saved address guarantees the flexibility of multiple calls

 jal #Jump and link can form an address or link pointing to the call site, so that the function can return the correct address
Jump will make the PC jump to the address of the called function and return the address of the next instruction obtained by linking The address is stored in the ra register

Guess you like

Origin blog.csdn.net/weixin_43754049/article/details/126819600