📂
build a OS
  • Learn OS with me
  • OS Interfaces
    • OS interfaces
    • I/O and File descriptors
    • Process and Memory
    • Pipes
    • File
  • OS Organization
    • OS Organization
    • Challenge yourself
  • Memory Management
    • XV6 Virtual Memory
    • Page Table
      • Part 1: How to translate address
      • Part 2: Create an Address Space
      • Part 3: How Page Table is used
      • Part 4: Page Fault and Swap
      • Part 5: How to operate on page tables
    • xv6 buddy allocator
      • How to display physical memory
    • Memory Management Walk Through
  • Traps and Interrupts
    • Trap Home Page
      • 系统调用的核心原理
    • What is trapframe
    • What is trampoline
    • Traps from kernel space
    • How fork() works
    • How system calls get into/out of the kernel
    • How exec() works
  • Scheduling
    • XV6 CPU Scheduling
    • How unix pipes work?
    • How does wait(), exit(), kill() work?
  • File System
    • Overview and Disk Layout
    • Buffer Cache
    • Design Inode Layer
    • Inode Content
    • Block Allocator
    • Design a log system for crash recovery
    • Directory Layer
    • Path names
    • File Descriptor Layer
    • FS System Calls
    • XV6 VS Real World
    • Make Xv6 File disk management system
    • Write FS simulator in python
    • How Redirect Shell command works
  • Concurrency
    • Spinlock
    • How linux select work
    • Hardware Support Locking
    • Exercise: Implement atomic counter
    • Locking in Xv6
    • Concurrency in Xv6
    • Exercise: Socket Programming with Event loop
  • Labs
    • Lab 1 Xv6 and Unix utilities
    • Lab 2 Shell
    • Lab 3 Buddy Allocator
    • Lab 4 Lazy
    • Lab 5 Copy-on-Write Fork for xv6
    • Lab 6 RISC-V assembly
    • Lab 6 Uthread: switching between threads
    • Lab 6 Alarm
    • Lab 7 Lock
    • Lab 8 File System: Large Files
    • Lab 8 File System: Symbolic links
    • Lab 9 mmap
    • Lab 10 Networking Part 1
    • Lab 10 Networking Part 2
  • Hardware, Device, Assembly
    • RISC-V assembly
    • Assembly: Access and Store information in Memory
    • Start xv6 and the first process
    • Why first user process loads another program?
    • What does kernel.ld do in XV6?
    • XV6 Device Driver
Powered by GitBook
On this page
  • Warmup: RISC-V assembly
  • Questions and Answers
  • Reference
  • Assembly Details
  • 心得

Was this helpful?

  1. Labs

Lab 6 RISC-V assembly

warmup and learn assembly

PreviousLab 5 Copy-on-Write Fork for xv6NextLab 6 Uthread: switching between threads

Last updated 5 years ago

Was this helpful?

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:

  30:    00000097              auipc    ra,0x0
  34:    620080e7              jalr    1568(ra) # 650 <printf>

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

int g(int x) {
   0:    1141                    addi    sp,sp,-16
   2:    e422                    sd    s0,8(sp)
   4:    0800                    addi    s0,sp,16
  return x+3;
}
   6:    250d                    addiw    a0,a0,3
   8:    6422                    ld    s0,8(sp)
   a:    0141                    addi    sp,sp,16
   c:    8082                    ret

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
  26:    45b1                    li    a1,12
  28:    00000517              auipc    a0,0x0
  2c:    7d050513              addi    a0,a0,2000 # 7f8 <malloc+0xea>
  30:    00000097              auipc    ra,0x0
  34:    620080e7              jalr    1568(ra) # 650 <printf>
  exit(0);
  38:    4501                    li    a0,0
  3a:    00000097              auipc    ra,0x0
  3e:    27e080e7              jalr    638(ra) # 2b8 <exit>

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

心得

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