(undone) MIT6.S081 2023 学习笔记 (Day5: LAB4 traps)
LAB 网页:https://pdos.csail.mit.edu/6.S081/2023/labs/traps.html
任务1:RISC-V assembly (完成)
初步看问题要求,这是一道文科题(问答题)
在你的 xv6 仓库中有一个文件 user/call.c。执行 make fs.img 会对其进行编译,并生成一个可读的汇编版本文件 user/call.asm。
阅读 call.asm 中关于函数 g、f 和 main 的代码。RISC-V 的指令手册可以在参考页面中找到。请在 answers-traps.txt 文件中回答以下问题:
Q: Which registers contain arguments to functions? For example, which register holds 13 in main’s call to printf?
A: 根据题意,我们先运行 make fs.img
生成 call.asm
随后阅读 user/call.c 和 user/call.asm 的代码,如下:
可以看到,储存函数参数的寄存器是 a0, a1, a2。其中,储存整数 13 的寄存器是 a2
Q: Where is the call to function f in the assembly code for main? Where is the call to g? (Hint: the compiler may inline functions.)
A: main 函数的汇编代码中没有函数 f 和 g 的函数调用。它们在编译阶段被编译器优化掉了。
Q: At what address is the function printf located?
A:搜索 call.asm 中 printf 的位置,如下
可以看到是 0x64a
Q: What value is in the register ra just after the jalr to printf in main?
A: 运行 jalr 指令后,寄存器 ra 的值是 0x38。
方式:使用 make qemu-gdb
启动 xv6,随后对 0x34 (jalr指令的地址) 打断点。
启动 xv6 后,运行 call 程序,qemu-gdb 会停留在 0x34 处。
此时运行 si 命令,执行 0x34 的 jalr 指令,可以看到 pc 跳转到了 0x64a 处。
再运行 info registers ra
,即可看到 ra 寄存器的值为 0x38
Q:
Run the following code.unsigned int i = 0x00646c72;printf("H%x Wo%s", 57616, &i);What is the output? Here's an ASCII table that maps bytes to characters.
The output depends on that fact that the RISC-V is little-endian. If the RISC-V were instead big-endian what would you set i to in order to yield the same output? Would you need to change 57616 to a different value?Here's a description of little- and big-endian and a more whimsical description.
A:输出为 “He110 World”
如果 RISCV 是大端,那么 i 改为 0x726c6400
不需要修改 57616
(这里的回答不用太正确,反正不是代码)
Q:
In the following code, what is going to be printed after 'y='? (note: the answer is not a specific value.) Why does this happen?printf("x=%d y=%d", 3);
A: y= 后的数字是个垃圾数字 (junk value)。因为编译器仅仅给 a0 和 a1 寄存器赋值了,没有给 y=%d 的 %d 所对应的存储空间赋值
任务2:Backtrace (doing)
TODO: here