CS61C | Lecture 9

Lecture 9 | Running a Program

Interpretation vs Translation

Interpreter

Directly executes a program in the source language.

Translator

Converts a program from the source language to an quaivalent program in another language.
也就是说,翻译器将语言翻译为上图靠右的语言。

Compiler

使用 GCC 的时候用 -s 参数,生成 .s 文件,也就是编译器的输出,也是汇编器的输入。 该阶段会包含一些伪指令。 ```assembly mv t1, t2 <= addi t1, t2, 0 ``` ## Assembler 汇编器的输入是 .s 文件,如果用 RISC-V 指令集架构,那就是 RISC-V 汇编语言。输出是 Object Code。 - Reads and Uses Directives - Replace Pseudo-instructions(伪指令) - Produce Machine Language - Creates Object File ![](https://img-blog.csdnimg.cn/img_convert/53520a1405da992a127e0516bee3e8d3.png) ### Pseudo-instruction Replacement
PseudoReal
mv t0, t1addi t0, t1, 0
neg t0, t1sub t0, zero, t1
li t0, immaddi t0, zero, imm
not t0, t1xori t0, t1, -1
beqz t0, loopbeq t0, zero, loop
la t0, strlui t0, str[31:12]
addi t0, t0, str[11:0]
OR
auipc t0, str[31:12]
addi t0, t0, str[11:0]

Producing Machine Language

在替换伪指令之后,进行 2 次扫描程序,第一次记下所有标签的位置,第二次扫描是,当在分支中看到标签时,此时我们知道所有标签的位置,use label positions to generate code。

Symbol Table

  • Labels: function calling
  • Data: anything in the . d a t a \textcolor{red}{.data} .data section; variables which may be accessed across files.

Linker

  • Input: Object code files, information tables.
  • Output: Executable code
  • Combines several object files into a single executable(“ l i n k i n g \textcolor{orange}{linking} linking”)
  • Enable separate compilation of files
    • Changes to one file do not require recompilation of the whole program.

Resolving References

  • Linker knows:
    • Length of each text and data segment
    • Ordering of text and data segments
  • Linker assumes first word of first text segment is at address 0 x 10000 \textcolor{orange}{0x10000} 0x10000 for RV32
  • Linker calculates absolute address of each label to be jumped to and each piece of data being referenced

Static vs Dynamically Linked Libraries

Statically-linked

Library is now part of the executable, so if the library updates, we don’t get the fix. Includes the entire library even if not all of it will be used.
函数和数据被编译进一个二进制文件(一般为 .lib),在编译链接可执行文件时,链接器从这些库中复制这些函数和数据并把他们和应用程序的其他模块组合起来生成可执行文件(.exe)

Dynamically Linked Libraries(DLL)
Space/time issues
  • + Storing a program requires less disk space
  • + Sending a program requires less time
  • + Executing two programs requires less memory(if they share a library)
  • - At runtime, there’s time overhead to do link
Upgrades
  • + Replacing one file (libXYZ.so) upgrades every program that uses library “XYZ
  • - Having the executable isn’t enough anymore. 也就是说如果我们破坏了某个库,而有 20 个程序在使用它,那么他们都不会运行。

Loader

  • Reads executable file’s header to determine size of text and data segments
  • Creates new address space for program large enough to hold text and data segments, along with a stack segment
  • Copies instructions + data from executable file into the new address space
  • Copies arguments passed to the program onto the stack
  • Initializes machine registers
  • Jumps to start-up routine that copies program’s arguments from stack to registers & sets the PC
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值