Skip to content

Instantly share code, notes, and snippets.

@cheery
Created February 6, 2019 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheery/5f7426fa4957907d8a04d5419a9aae90 to your computer and use it in GitHub Desktop.
Save cheery/5f7426fa4957907d8a04d5419a9aae90 to your computer and use it in GitHub Desktop.
Tiny executables in RISC-V.
.section .text
.org 0
load_address = 0x010000 # can also use: 0x08048000
ehdr:
.byte 0x7f, 0x45, 0x4c, 0x46, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
.half 2 # e_type
.half 0xf3 # e_machine
.word 1 # e_version
.quad load_address + _start - ehdr # e_entry
.quad phdr - ehdr # e_phoff
.quad 0 # e_shoff
.word 0 # e_flags
.half ehdrsize # e_ehsize (64)
.half phdrsize # e_phentsize (56)
.half 1 # e_phnum
.half 0 # e_shentsize (64)
.half 0 # e_shnum
.half 0 # e_shstrndx
ehdrsize = . - ehdr
phdr:
.word 1 # p_type
.word 5 # p_flags
.quad 0 # p_offset
.quad load_address # p_vaddr
.quad load_address # p_paddr
.quad filesize # p_filesz
.quad filesize # p_memsz
.quad 0x1000 # p_align
phdrsize = . - phdr
# Linux system call numbers we are using.
sys_read = 63
sys_write = 64
sys_exit = 93
# standard file numbers
stdin = 0
stdout = 1
stderr = 2
_start:
li a0, stdout
lla a1, greeting
li a2, 6
li a7, sys_write
ecall
li a0, 0
li a7, sys_exit
ecall
loop: j loop
greeting:
.string "Hello\n"
filesize = . - ehdr
tiny: listing.elf
riscv64-unknown-elf-objcopy -O binary $^ $@
chmod +x $@
listing.elf: listing.s
riscv64-unknown-elf-gcc -nostartfiles $^ -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment