Kaya Çelebi
Home About GitHub PyPI Contribute Contact

AssemblyConverter

Follow @kcelebi Star Watch

Let's check out how to use AssemblyConverter. First, let's import it into our Python file:

from riscv_assembler.convert import AssemblyConverter

We can now instantiate an object. The constructor's defaults are initialized as so:

AssemblyConverter(output_type = "b", nibble = False, hexMode = False)

output_type refers to whether a converted file should be outputted to a binary file ("b"), a text file ("t"), or to console ("p"). The three of these options can be used in any sort of combination. Here are acceptable usages:

cnv = AssemblyConverter(output_type = "btp") #binary and text and printing
cnv = AssemblyConverter(output_type = "pbt") #works same as above ^
cnv = AssemblyConverter(output_type = "b") #just binary
cnv = AssemblyConverter(output_type = "t") #just text
cnv = AssemblyConverter(output_type = "p") #just printing
cnv = AssemblyConverter() #binary by default

nibble refers to whether (for text file and console outputs) the 32-bit binary numbers should be split in nibbles or kept as an unbroken string. An example output for nibble = True would be:

1101 0110 0000 0000 ...

An example for the default nibble = False would be:

11010110000000...

hexMode gives the option (for text file and console outputs) to output in hexadecimal form instead of binary. By default, the outputs are in binary. Leading zeros are included to properly represent a 32-bit instruction.

Convert

With this object we can apply our most powerful function : convert(). This function takes in a file name (with .s extension) from the local directory and converts it to the output file of your choice, specified by the object construction. Let's convert the file simple.s:

cnv = AssemblyConverter()
cnv.convert('simple.s')

Any empty files, fully commented files, or files without .s extension will not be accepted. The function creates a directory by truncating the extension of the file along with two subdirectories called bin and txt for the respective output files (for simple.s the directory would be simple). Output files will be stored there for usage and/or printed to console.

/simple
  /bin
    simple.bin
  /txt
    simple.txt

Helper Functions

Here are a few functions that might be useful.

Output Type

It is possible to get and set the output type of your AssemblyConverter object. Observe:

cnv = AssemblyConverter("bt")
output_type = cnv.getOutputType()

print(output_type) #prints bt

cnv.setOutputType("p") #now only prints to console

Instructions

It is possible to check whether a certain instruction is supported by this package. The function instructionExists(instr) returns a boolean for whether instr exists. Observe:
cnv = AssemblyConverter()
cnv.instructionExists("add") #returns true
cnv.instructionExists("hello world") #returns false