计算机系统I-汇编语言
RISC-V Assembly Language
Some basic concepts
1 |
|
Caller: calling function
- main in this case
- Passes arguments to callee
- Jumps to callee
Callee: called function
- sum in this case
- Performs the function
- Returns result to caller
- Returns to point of call
Calling Convention
Six general stages in calling a function
- Place the arguments where the function can access them.
- Jump to the function (using RV32I’s jal).
- Acquire local storage resources the function needs, saving registers as required.
- Perform the desired task of the function.
- Place the function result value where the calling program can access it, restore any registers, and release any local storage resources.
- Since a function can be called from several points in a program, return control to the point of origin (using ret).
Function Entry and Exit
The function prologue
If there are too many function arguments and variables to fit in the registers, the prologue allocates space on the stack for the function frame, as it is called.
1 |
|
The function epilogue
Undoes the stack frame and returns to the point of origin
1 |
|
From source code to a running program
- Write source code
- Turn the source code into the executable program
- Run it!
Step of translation from C source code to a running program. These are the logical steps, although some steps are combined to accelerate translation.
Compiler: *.c->*.s
Preprocessor(*.c->*.i)
Expands all macro definitions and include statements (and anything else starting with a #) and passes the result to the actual compiler.
Compiler(*.i->*.s)
Assembler: *.s->*.o
Not simply to produce object code from the instructions that the processor understands. But to extend then to include operations useful for the assembly language programmer or the compiler writer.
Pseudo instruction | Base Instruction(s) | Meaning |
---|---|---|
nop | addi x0, x0, 0 | No operation |
neg re, rs | sub rd, x0, rs | Two’s complement |
ret | jalr x0, x1, 0 | Return from subroutine |
Output: in RISC-V Machine Language (*.o)
The assembler produces the object using the **Executable and Linkable Format **(ELF, formerly named Extensible Linking Format) standard format.
Executable and Linkable Format (ELF) is a common standard file format for executable files, object code, shared libraries, and core dumps.
The standard binary file format for Unix and Unix-like systems on x86 processors by the 86open project.
By design, the ELF format is flexible, extensible, and cross-platform. This has allowed it to be adopted by many different operating systems on many different hardware platforms.
Linker: (multiple) (*.o->a.out)
Rather than compile all the source code every time one file changes, the linker allows individual files to be compiled and assembled separately.
Why separate compile/assemble and linking steps?
Separately compiling modules and linking them together avoids the need to recompile the whole program every time something changes. Need to just recompile a small module. A linker coalesces object files together to create a complete program.
Linker Functions 1: Fixing Addresses
Addresses in an object file are usually relative to the start of the code or data segment in that file.
When different object files are combined, the same kind of segments from the different object files get merged. Addresses have to be “fixed up” to account for this merging. The fixing up is done by the linker, using information embedded in the executable for this purpose
Linker Functions 2: Symbol Resolution
Suppose: Module B defines a symbol x; Module A refers to x.
The linker must determine the location of x in the object module obtained from merging A and B and modify references to x to refer to this location.
Each linkable module contains a symbol table,whose contents include:
- Global symbols defined in the module.
- Global symbols referenced but not defined in the module (externals).
- Segment names (e.g., text,data, rodata).
- These are usually considered to be global symbols defined to be at the beginning of the segment.
- Non-global symbols and line number information, for debuggers.
Format of the (Executable) Object File
- Header
- Location of main entry point
- Text Segment
- Instructions
- Data Segment
- Static data (local/global vars, strings, constants)
- Relocation Information
- Instructions and data that depend on actual addresses
- Linker patches these bits after relocating segments
- Symbol Table
- Exported and imported references
- Debugging Information