MIT 6.828 - 6. Lab 06: User-level Threads and Alarm

Experimental summary

0. experimental preparation

Experimental guidance link

Up direct:

$ cd xv6-riscv-fall19
$ git checkout syscall

1. Warmup: RISC-V assembly

To this answer a few questions, open user/call.cand user/call.asmwith the following code fragment:

int g(int x) {
  return x+3;
}

int f(int x) {
  return g(x);
}

void main(void) {
  printf("%d %d\n", f(8)+1, 13);
  exit(0);
}
000000000000001c <main>:

void main(void) {
  1c:   1141                    addi    sp,sp,-16
  1e:   e406                    sd  ra,8(sp)
  20:   e022                    sd  s0,0(sp)
  22:   0800                    addi    s0,sp,16
  printf("%d %d\n", f(8)+1, 13);
  24:   4635                    li  a2,13                              ; printf("%d %d\n", f(8)+1, 13);
  26:   45b1                    li  a1,12
  28:   00000517            auipc   a0,0x0
  2c:   75050513            addi    a0,a0,1872 # 778 <malloc+0xea>
  30:   00000097            auipc   ra,0x0
  34:   5a0080e7            jalr    1440(ra) # 5d0 <printf>
  exit(0);
  38:   4501                    li  a0,0
  3a:   00000097            auipc   ra,0x0
  3e:   1fe080e7            jalr    510(ra) # 238 <exit>


... lines omitted

00000000000005d0 <printf>:

void
printf(const char *fmt, ...)
{

....

Let's answer the question:

  1. Which registers contain arguments to functions? For example, which register holds 13 in main's call to printf?
    • The riscv user-level isa (in doc/riscv-calling.pdf), a0-a7 and fa0-fa7 total of 16 registers are used to pass parameters
    • Specifically, according to the above code, that can register for storing the parameter a2 is passed printf 13
  2. Where is the function call to f from main? Where is the call to g? (Hint: the compiler may inline functions.)
    • Note the words in parentheses. We know that according to written calculation f(8)+1 = 12, and is also found in the main function call f, f instructions are optimized at compile directly into a constant 12stuffed the a1 register.
  3. At what address is the function printf located?
    • Observation main function used when calling code auipc ra,0x0and jalr 1440(ra)the former current instruction fetch pc plus 0x0 stored ra, which jump to ra + 1440. Calculation known 0x0000000000000030 + 1440 = 0x00000000000005d0. Proven is the entry address of printf.
  4. What value is in the register ra just after the jalr to printf in main?
    • This question needs to look rv spec. jalrAfter the completion of the instruction, rathe register will store the return position (i.e., pc + 4)

Guess you like

Origin www.cnblogs.com/nlp-in-shell/p/12178015.html