Reading — step 1 of 5
Read
~1 min readProduction
Syscalls & I/O
To do anything useful (print, exit), the simulated program needs to talk to the host.
ECALL is the bridge:
li a7, 64 # syscall number: write
li a0, 1 # fd: stdout
la a1, msg # buffer
li a2, 14 # length
ecall
li a7, 93 # syscall: exit
li a0, 0 # exit code
ecall
Linux RISC-V syscalls (a7):
- 64: write(fd, buf, count)
- 63: read(fd, buf, count)
- 93: exit(status)
- 56: openat(...)
- 57: close(fd)
- 80: fstat(fd, statbuf)
- 214: brk(addr)
Simulator implements a subset:
python
For embedded targets:
- No OS. ECALL might trap to firmware.
- Memory-mapped I/O: writing to specific address sends byte to UART.
For toy debugging:
- EBREAK: pause + dump state.
- Custom syscall like print_int: easier than buffered write.
Memory-mapped UART (alternative to syscalls):
python
Now sb a0, 0x10000000(x0) prints a character.
Real systems combine both: syscalls for OS, MMIO for hardware control.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…