A simple RISC-V assembler written in Python
Project description
rvasm: A Python-Based RISC-V Assembler
rvasm aims to provide a simple and easy-to-use RISC-V assembler, as both a command-line tool and a Python package. rvasm makes it easy to define and use custom instructions, simply by providing a JSON file.
Getting started
You can install rvasm using pip, as follows:
pip install rvasm
rvasm can be used either as a command-line tool, or as a Python package which can be used in a Python program.
The command-line tool can be used to assemble an RV32I text file, as demonstrated below:
rvasm my_file.asm
This will produce an output file out.mem, containing your assembled RV32I in hexadecimal. For help on other command-line options, use:
rvasm --help
Here is an example on how you can use rvasm in a Python project:
import rvasm my_assembler = rvasm.RVAsm() # Create an Assembler object with open("my_input_file.asm", "r") as f: # Open the assembly file my_assembler.Assemble(f) # Generate the machine code
Assembling custom instructions
rvasm makes assembling custom instructions straightforward.
Firstly, create a JSON file detailing your custom instructions (use this reference). You can specify one or more RISC-V extensions in the same file, or use multiple files. An example would be like so:
{ "MY_CUSTOM_EXTENSION": [ { "instr": "addi", "format": "instr rd, rs1, imm", "width": 32, "encoding": "imm[11:0] & rs1[4:0] & funct3[2:0] & rd[4:0] & opcode[6:0]", "opcode": "0010011", "funct3": "000", "funct7": null } ] }
To include the new extension(s) from the command line, use the --include option, as below:
rvasm my_file.asm --include my_custom_extension.json
The following example shows how you can use rvasm to assemble custom RISC-V instructions in your Python code:
import rvasm asm = rvasm.RVAsm() with open("my_custom_extension.json", "r") as f: asm.IncludeFromJSON(f) with open("my_file.asm", "r") as f: asm.Assemble(f)
This project is a work-in-progress, so please keep checking in! Feel free to create issues and suggest improvements.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rvasm-0.0.1a2.tar.gz.
File metadata
- Download URL: rvasm-0.0.1a2.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a9ba35c945ab733639024ce788e42565d18c2d0df3701d986416de4925629dc
|
|
| MD5 |
431c54beffe294e63c8a3710859a3941
|
|
| BLAKE2b-256 |
cec33c4a85d1dc9391cbfc9813e655bf4b1bb1d794c613aa7041a089b01890e8
|
File details
Details for the file rvasm-0.0.1a2-py3-none-any.whl.
File metadata
- Download URL: rvasm-0.0.1a2-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
760064c1cb4daf8df1c9e16af11f86eba62c972875e8b2e3aad56a67bf0699ed
|
|
| MD5 |
0ad0386af7175458612c97d8e3268d23
|
|
| BLAKE2b-256 |
90b9207463ad527ad25177e4db17b98b07c328a972793c3ba16fd5ee0c21f588
|