Lab 6 RISC-V assembly

warmup and learn assembly

Warmup: RISC-V assembly

There is a file user/call.c in your xv6 repo. make fs.img builds a user program call and a readable assembly version of the program in user/call.asm.

Read the code in call.asm for the functions g, f, and main. The instruction manual for RISC-V is in the doc directory (doc/riscv-spec-v2.2.pdf).

Questions and Answers

Which registers contain arguments to functions? For example, which register holds 13 in main’s call to printf?

a0–a7 and fa0-fa7contains function arguments.

a0 is also return values.

13 is stored in register a2

Where is the function call to f from main? Where is the call to g? (Hint: the compiler may inline functions.)

Call to f from main 26: 45b1 li a1,12

Call to g from f 14: 250d addiw a0,a0,3

At what address is the function printf located?

0000000000000650 <printf>

Or see the following code in main:

program counter(pc) is 0x30.

auipc is to add 0x0 to pc, and store the result in ra. so ra is 0x30

jalr is jump and link register so 1568(ra) is:

hex(1568) + 0x30 => 0x620 + 0x30 = 0x650

What value is in the register ra just after the jalr to printf in main?

pc+4 is written to register ra. In our example asm, ra stores 0x38

Reference

Assembly Details

ADDI

Add immediate (with overflow)

Description: Adds a register and a sign-extended immediate value and stores the result in a register Operation: $t = $s + imm; advance_pc (4);

Syntax: addi $t, $s, imm

sd

store a double world. Store 64 bits from s0 register to sp+8

心得

汇编在现代社会不再是必需品。太多高级语言包装了一切,蒙蔽了最初的最原始的思想,可是无论装饰再如何精彩,都遮挡不了最初最美的风景。那里才是我的追求。

Last updated

Was this helpful?