RISCV Reader Notes_3 RISCV Assembly

RISC-V assembly language

image-20230626080943809

The steps of function calling have also been involved in computer composition and design:

  • The specified register is stored in the parameter;
  • Jump to the function start position (jal);
  • Save registers on demand in callee;
  • execute function;
  • Restore the saved register; store the return value in the specified location;
  • Returns where the function was called (ret command, mips is jr).

Variables should be stored in registers as much as possible because of their speed. A register that does not need to save a value during a function call (the value is directly overwritten by the function) is called a temporary register; a register that needs to save a value is called a saved register. Functions that do not call other functions are called leaf functions. If the leaf function has fewer parameters and local variables, it can be solved directly with registers; if there are too many, it needs to be stored in memory (spilling overflow), but overflow is rare.

image-20230626082237242

It is mainly to save registers and some registers related to low-level operations, such as sp stack pointer and other values, which cannot be changed and need to be preserved.

Example function call:

entry_label:
	addi sp,sp,-framesize	# framesize存储的是要保存的ra,保存寄存器等个数*4,因为都是32位的寄存器
	sw ra,framesize-4(sp)	# 栈顶存ra
	# 按需保存其他寄存器

	# 函数体
	
	# 按需返回其他寄存器
	lw ra,framesize-4(sp)	# 栈顶取ra 
	addi sp,sp,framesize
	ret

assembler

In the unix system, it is a file with a .s suffix, while in the ms-dos system, it is a .asm file.

In the file, the beginning of . is an indicator. For example, let's analyze the assembly file generated by hello world compilation:

image-20230626150328359

1687745922013

.text marks the beginning of the code section.

.align 2 is to align the code by 32 bits, because the instructions are all 32 bits.

.globl main is the global main function.

Strings are unmodifiable data, so they are marked with .section .rodata read-only data. Also align the data.

Then use .string to identify \0-terminated strings.

The final file format generated by the assembler is as follows, which is a linkable ELF file.

1687763280786

The incoming address of all registers in lines 8 to 1c is 0, and the specific value is set when the subsequent link is made.

Linker

Each file is allowed to be compiled and assembled independently, so that some files are changed without recompiling all of them, and the new code can be spliced ​​with the existing machine language modules.

unix files are .o -> .out files, msdos files are .obj .lib -> .exe files.

The linker needs to allocate the corresponding address space in the figure to the program and data. This step can be omitted if the program is position-independent (PIC code). The relative transfer feature of RV32I (program jump is plus relative value) makes it easier to implement PIC.

Then it is necessary to translate the instruction of jumping directly to the label in the compiler into jumping to the immediate address, and the value of jalr is also corrected to jump to the immediate address jal, and the specific address values ​​of auipc jalr lui addi are modified.

image-20230626153655941

The floating-point number transmission of RV depends on which ABI interface specification is selected. ilp32 is a floating-point number passed in an integer register, ilp32f is a single-precision floating-point register, and ilp32d is a double-precision floating-point register.

RV32I does not support FD extension, so it can only use ilp32. Compilation instructions:GCC 选项-march=rv32i -mabi=lib32

However, it does not say that it must be used to support floating-point extensions, so RV32IFD can also use ilp32 or ilp32f.

The linker also checks for compatible extensions. The compiler may support multiple ABI ISAs, but it will be incompatible if those libraries are not installed on the computer itself.

Static linking and dynamic linking

When linking a library function, if the current version of the library function has already been linked, it will not be linked. Multiple calls do not repeat the link.

Moreover, the jump address of the dynamic link is stored in a table in the memory. The function address table is updated each time the link is made for the first time. When calling, only three instructions (stub instructions) are needed to look up the table to find the corresponding jump position and then jump.

epilogue

The 0 register makes some pseudocode easier to implement;

lui auipc is different from the complex instruction form combining two 16-bit addresses in the past, 20-bit left shift + 12-bit to get the address faster, reduce the number of instructions, and auipc is easy to handle PIC;

PIC reduces link complexity (position independent);

A large number of registers makes the operation speed up.

Guess you like

Origin blog.csdn.net/jtwqwq/article/details/131401555