0

I'm writing some risc-v assembly code and I want to read and execute some instructions stored in a file (a binary compiled with riscv-gcc). Ideally I'ld like something like the $readmem of verilog

Do you have an idea on how to do that?

L.DZ
  • 111
  • 3
  • 12
  • Fork/exec is the normal way to run other whole executables. Or `dlopen` a shared library. If you want to do that manually, you could make an `mmap` system call to map the file contents read+exec and `jalr` to a pointer into that memory. I'm assuming Linux or something with equivalent system-calls since you didn't specify. – Peter Cordes Apr 17 '23 at 09:34
  • Do you have a toy example or just a canvas no how it might looks like? I need to write this in pure assembly, not in C. (and yes It's on linux) – L.DZ Apr 17 '23 at 09:59
  • 2
    Anything you can do in C, you can do in asm. Just look at compiler output if you aren't sure how. (Or for system calls, check the ABI and the syscall man page.) Related: [How to exec mmaped binary/code right from the allocated memory](https://stackoverflow.com/q/40766249). And some hacky maybe-working code on [Invoking a function (main()) from a binary file in C](https://stackoverflow.com/q/12409908) – Peter Cordes Apr 17 '23 at 10:04
  • Also [Execute in memory binary with c](https://stackoverflow.com/q/66051960) - my answer covers many of the practical challenges of calling code in an ELF executable created by a compiler. For example, it'll be expecting to have the dynamic linker set up pointers to the library functions it wants to call, unless you avoid making library calls. – Peter Cordes Apr 17 '23 at 10:05

0 Answers0