xv6-lab/answers-traps.txt
2023-01-25 09:33:21 +00:00

34 lines
1.6 KiB
Plaintext

Q: Which registers contain arguments to functions? For example, which register holds 13 in main's call to printf?
A: The base integer calling convention provides eight argument registers, a0-a7, the first two of which are also used to return values. Register a2 holds 13.
Q: Where is the call to function f in the assembly code for main? Where is the call to g?
A: There is no call to function f for main, the value of f(8) (which is 12) is calculated at compile time and hard-coded into the executable.
The call to g is inlined into f, at address 0x14.
Q: At what address is the function printf located?
A: The function printf is located at address 0x642.
Q: What value is in the register ra just after the jalr to printf in main?
A: The address of the next instruction after return, aka 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?
---
A: The output is, He110 World(with no whitespace or new line).
If big endian, the new i should be 0x726c6400; and there is no need to change 56716.
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: The value that register a2 holds when the caller calls printf. By default, the third argument passed to a procedure is stored in a2.