计算机系统I-汇编语言

RISC-V Assembly Language

Some basic concepts

1
2
3
4
5
6
7
void main(){
int y;
y = sum(42,7);
}
int sum(int a,int b){
return (a + b);
}

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

  1. Place the arguments where the function can access them.
  2. Jump to the function (using RV32I’s jal).
  3. Acquire local storage resources the function needs, saving registers as required.
  4. Perform the desired task of the function.
  5. Place the function result value where the calling program can access it, restore any registers, and release any local storage resources.
  6. 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
2
3
4
5
entry_label:
addi sp,sp,-framesize #Allocate space for stack frame
#by adjusting stack pointer (sp register)
sw ra,framesize-4(sp) #Save return address (ra register)
#save other registers to stack if needed

The function epilogue

Undoes the stack frame and returns to the point of origin

1
2
3
4
#restore registers from stack if needed
lw ra,framesize-4(sp) #Restore return address register
addi sp,sp, framesize #De-allocate space for stack frame
ret #Return to calling point

From source code to a running program

  1. Write source code
  2. Turn the source code into the executable program
  3. 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 instructionBase Instruction(s)Meaning
nopaddi x0, x0, 0No operation
neg re, rssub rd, x0, rsTwo’s complement
retjalr x0, x1, 0Return 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

计算机系统I-汇编语言
http://example.com/2024/05/06/Computer-Systems-I-Assembly-Language/
作者
Penner
发布于
2024年5月6日
许可协议